关于临时变量,下面的问题输出会是什么,为什么?

ithiker 2014-10-20 08:03:18

#include<iostream>
#include<set>
#include<vector>
using namespace std;

class VectorSetTest
{
public:
VectorSetTest()
{
m_set.insert("Set1");
m_set.insert("Set2");
m_set.insert("Set3");
m_vector.push_back("Vector1");
m_vector.push_back("Vector2");
m_vector.push_back("Vector3");
}

set<string> GetSet(){return m_set;}
vector<string> GetVector() { return m_vector;}
private:
set<string> m_set;
vector<string> m_vector;
};

int main()
{
VectorSetTest testObject;
set<string>::iterator setIter = testObject.GetSet().begin();
vector<string>::iterator vecIter = testObject.GetVector().begin();

cout<<(*setIter).c_str()<<endl;
cout<<(*vecIter).c_str()<<endl;

return 0;
}
...全文
220 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
starytx 2014-10-21
  • 打赏
  • 举报
回复
别人制定的东西,只需要知道怎么遵守就行了,过多的研究都没有意义。
ri_aje 2014-10-21
  • 打赏
  • 举报
回复
引用 6 楼 gigglesun 的回复:
[quote=引用 4 楼 lovesmiles 的回复:] [quote=引用 2 楼 gigglesun 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 临时变量会在 ; 处析构,后面对其迭代器的访问导致未定义行为,不要写这样的代码。
恩,是的,同样的是未定义行为,奇怪的是set的迭代器能够处理临时变量,但是vector的不可以 上面输出为

set1

[/quote] 未定义的意思是,可能出错,也可能不出错,可能达到你想要的,也可能达不到你想要的。 所以才叫做未定义。[/quote] 对于set,采用迭代器访问其零时变量,总是成功的。 可以说,对于set,其实是defined, 对于vector,是undefined 使用set的临时变量是defined 下面的代码,可以通过迭代器获取到全部的set元素;但是对于vector却不可以 使用set的临时变量是undefined 下面的代码,打开注释掉的两行,只会输出Hello,没有输出1,11,说明是undefined. 但又输出了Hello, undefined中的define? C++大牛们帮忙看看

#include<iostream>
#include<set>
#include<vector>
using namespace std;
template<typename T>
class VectorSetTest
{
    public:
        VectorSetTest()
        {
        }

        void AddSet(const T& a)
        {
            m_set.insert(a);
        }

        void AddVector(const T& a)
        {
            m_vector.push_back(a);
        }
            set<T>  GetSet(){return m_set;}
        vector<T>  GetVector() { return m_vector;}
    private:
        set<T> m_set;
        vector<T> m_vector;
};

int main()
{
    VectorSetTest<string> testObject;
    testObject.AddSet("Hello");
    testObject.AddVector("World");
    testObject.AddSet("Hello1");
    testObject.AddVector("World1");
    set<string>::iterator setIter = testObject.GetSet().begin();
    vector<string>::iterator vecIter = testObject.GetVector().begin();
//    cout<<(*setIter).c_str()<<endl;
//    cout<<(*vecIter).c_str()<<endl;

    VectorSetTest<int> testObject1;
    testObject1.AddSet(1);
    testObject1.AddVector(100);
    testObject1.AddSet(11);
    testObject1.AddVector(1100);
    set<int>::iterator setIter1 = testObject1.GetSet().begin();
    vector<int>::iterator vecIter1 = testObject1.GetVector().begin();

    cout<<*setIter1++<<endl;
    cout<<*setIter1<<endl;
    cout<<*vecIter1<<endl;

    return 0;
}


[/quote] 是否 defined/undefined 你说了不算,标准才是权威。标准说未定义行为就是未定义行为,你找多少个编译器或例子来支持也白搭。劝楼主把精力放在刀刃上,钻研技术也要看清楚方向才行,并不是每一个细节都需要死磕的。
天使爱撒谎 2014-10-21
  • 打赏
  • 举报
回复
VS2012试了 你说的情况set 都不行
ithiker 2014-10-21
  • 打赏
  • 举报
回复
引用 4 楼 lovesmiles 的回复:
[quote=引用 2 楼 gigglesun 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 临时变量会在 ; 处析构,后面对其迭代器的访问导致未定义行为,不要写这样的代码。
恩,是的,同样的是未定义行为,奇怪的是set的迭代器能够处理临时变量,但是vector的不可以 上面输出为

set1

[/quote] 未定义的意思是,可能出错,也可能不出错,可能达到你想要的,也可能达不到你想要的。 所以才叫做未定义。[/quote] 对于set,采用迭代器访问其零时变量,总是成功的。 可以说,对于set,其实是defined, 对于vector,是undefined 使用set的临时变量是defined 下面的代码,可以通过迭代器获取到全部的set元素;但是对于vector却不可以 使用set的临时变量是undefined 下面的代码,打开注释掉的两行,只会输出Hello,没有输出1,11,说明是undefined. 但又输出了Hello, undefined中的define? C++大牛们帮忙看看

#include<iostream>
#include<set>
#include<vector>
using namespace std;
template<typename T>
class VectorSetTest
{
    public:
        VectorSetTest()
        {
        }

        void AddSet(const T& a)
        {
            m_set.insert(a);
        }

        void AddVector(const T& a)
        {
            m_vector.push_back(a);
        }
            set<T>  GetSet(){return m_set;}
        vector<T>  GetVector() { return m_vector;}
    private:
        set<T> m_set;
        vector<T> m_vector;
};

int main()
{
    VectorSetTest<string> testObject;
    testObject.AddSet("Hello");
    testObject.AddVector("World");
    testObject.AddSet("Hello1");
    testObject.AddVector("World1");
    set<string>::iterator setIter = testObject.GetSet().begin();
    vector<string>::iterator vecIter = testObject.GetVector().begin();
//    cout<<(*setIter).c_str()<<endl;
//    cout<<(*vecIter).c_str()<<endl;

    VectorSetTest<int> testObject1;
    testObject1.AddSet(1);
    testObject1.AddVector(100);
    testObject1.AddSet(11);
    testObject1.AddVector(1100);
    set<int>::iterator setIter1 = testObject1.GetSet().begin();
    vector<int>::iterator vecIter1 = testObject1.GetVector().begin();

    cout<<*setIter1++<<endl;
    cout<<*setIter1<<endl;
    cout<<*vecIter1<<endl;

    return 0;
}


Uron 2014-10-21
  • 打赏
  • 举报
回复
勤奋的小游侠 2014-10-21
  • 打赏
  • 举报
回复
引用 2 楼 gigglesun 的回复:
[quote=引用 1 楼 ri_aje 的回复:] 临时变量会在 ; 处析构,后面对其迭代器的访问导致未定义行为,不要写这样的代码。
恩,是的,同样的是未定义行为,奇怪的是set的迭代器能够处理临时变量,但是vector的不可以 上面输出为

set1

[/quote] 未定义的意思是,可能出错,也可能不出错,可能达到你想要的,也可能达不到你想要的。 所以才叫做未定义。
幻夢之葉 2014-10-21
  • 打赏
  • 举报
回复
引用 2 楼 gigglesun 的回复:
[quote=引用 1 楼 ri_aje 的回复:] 临时变量会在 ; 处析构,后面对其迭代器的访问导致未定义行为,不要写这样的代码。
恩,是的,同样的是未定义行为,奇怪的是set的迭代器能够处理临时变量,但是vector的不可以 上面输出为

set1

[/quote] 未定义行为不表示它输出的不是期望值或者全部不是期望的值。
ithiker 2014-10-21
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
临时变量会在 ; 处析构,后面对其迭代器的访问导致未定义行为,不要写这样的代码。
恩,是的,同样的是未定义行为,奇怪的是set的迭代器能够处理临时变量,但是vector的不可以 上面输出为

set1

ithiker 2014-10-21
  • 打赏
  • 举报
回复
多谢各位,不究这个问题了
ri_aje 2014-10-21
  • 打赏
  • 举报
回复
临时变量会在 ; 处析构,后面对其迭代器的访问导致未定义行为,不要写这样的代码。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧