如果我做了一个直接测试,那么我就没有问题了-数字的顺序是正确的:
map<int,int> testMap;
for(int i = 0; i < 100; ++i)
{
// everything is ordered correctly here
testMap.insert(testMap.end(), pair<int,int>(i,i));
}
但当我解析一个字符串,并尝试按读取值的顺序插入值时,结果就不是那么好了:
const string INPUT_TWO =
"=VAR STRING1 \"MYSTRING\"\n\
=VAR STRING2 \"EXAMPLE\"\n\
=VAR NUMBER1 12345\n\
=VAR NUMBER2 23456\n\
=VAR DUMMY 1111\n";
const string VAL_STRING = "VAR";
vector<pair<string, string>> parse_fields(const string & input)
{
map<string, string> fieldsMap;
vector<pair<string, string>> sequenceFields;
vector<string> lines = split(input, '\n');
for(size_t i = 0; i < lines.size(); ++i)
{
if(lines[i].find(VAL_STRING)!=string::npos)
{
vector<string> vals = split(lines[i], ' ');
if(vals.size()==3)
{
fieldsMap.insert(fieldsMap.end(), pair<string,string>(vals[1], remove_quotes(vals[2])));
sequenceFields.push_back(pair<string,string>(vals[1], remove_quotes(vals[2])));
}
}
}
// at the end the map looks like:
// "DUMMY", "1111"
// "NUMBER1", "12345"
// "NUMBER2", "23456"
// "STRING1", "MYSTRING"
// "STRING2", "EXAMPLE"
// the vector of pairs looks like:
// "STRING1", "MYSTRING"
// "STRING2", "EXAMPLE"
// "NUMBER1", "12345"
// "NUMBER2", "23456"
// "DUMMY", "1111"
return sequenceFields;
}
pastie
有人知道为什么会这样吗?