Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;输入流:Solaris与Linux中的操作顺序_C++_Linux_Stream_Solaris - Fatal编程技术网

C++ C++;输入流:Solaris与Linux中的操作顺序

C++ C++;输入流:Solaris与Linux中的操作顺序,c++,linux,stream,solaris,C++,Linux,Stream,Solaris,我有一个非常简单的测试程序,它使用istringstreams从std::string读取整数。代码是: std::map<int, int> imap; int idx, value; std::string str("1 2 3 4 5 6 7 8"); istringstream is(str); while(is >> idx >> imap[idx]){ cout << idx << " " << imap

我有一个非常简单的测试程序,它使用istringstreams从std::string读取整数。代码是:

std::map<int, int> imap;
int idx, value;
std::string str("1 2 3 4 5 6 7 8");
istringstream is(str);
while(is >> idx >> imap[idx]){
    cout << idx << " " << imap[idx] << endl;
}
cout << endl;

std::map<int, int>::iterator itr;
for(itr = imap.begin(); itr != imap.end(); itr++){
    cout << itr->first << " " << itr->second << endl;
}
但是,当我在CentOS 7下运行它时,我得到:

1 0
3 0
5 0
7 0

1 4
3 6
5 8
7 0
4204240 2
有人知道为什么Linux和Solaris下会有所不同吗?很明显,在读取映射的索引之前,它会先将值读入映射,但我不知道为什么。我可以通过稍微更改代码使其在Linux下工作:

std::map<int, int> imap;
int idx, value;
std::string str("1 2 3 4 5 6 7 8");
istringstream is(str);
while(is >> idx >> value){
    imap[idx] = value;
    cout << idx << " " << imap[idx] << endl;
}

std::map<int, int>::iterator itr;
for(itr = imap.begin(); itr != imap.end(); itr++){
    cout << itr->first << " " << itr->second << endl;
}
std::map-imap;
int-idx,值;
标准:字符串str(“12345678”);
istringstream为(str);
而(是>>idx>>值){
imap[idx]=值;
库特
这个表达式相当于

operator>>(operator>>(is, idx), imap.operator[](idx))
对同一函数的参数求值是相互不排序的;要么
操作符>>(is,idx)
要么
imap。操作符[](idx)
可以首先求值(也就是说,要么
is>>idx
要么
imap[idx]
可以首先求值)。如果先计算后者,则结果是一个左值,该左值指的是与地图中
idx
的旧值相对应的值;第二次读取将覆盖该值,而不是与
idx
的新值相对应的值


修改后的代码通过确保在访问
imap[idx]
之前读取
idx
来解决此问题。

它在一台机器上工作而不是在另一台机器上工作这一事实意味着它的行为未定义,类似于此处发布的类似操作顺序,表达式
为>idx>>imap[idx]
对变量求值多次,同时至少修改一次。请参阅
is >> idx >> imap[idx]
operator>>(operator>>(is, idx), imap.operator[](idx))