I'm trying out the code below; it runs and produces the output I'd expect, but could it lead to undefined behavior?
To test with, I show the key and wait for user input; if the input doesn't match the value, the iterator is stored in the vector.
I am also interested in knowing if I can use an iterator to walk through the vector? I tried
However, I couldn't figure out how to access "first" and "second" using this method. Is it possible to do so?
TIA
To test with, I show the key and wait for user input; if the input doesn't match the value, the iterator is stored in the vector.
Code:
map<string, string> test;
map<string, string>::iterator it;
vector<map<string, string>::iterator> bad_input;
string input;
test["key 1"] = "value 1";
test["key 2"] = "value 2";
test["key 3"] = "value 3";
for(it = test.begin(); it != test.end(); ++it)
{
cout << it->first;
cin >> input;
if(it->second != input)
bad_input.push_back(it);
}
for(unsigned i=0; i < bad_input.size(); ++i)
cout << bad_input[i]->first
<< " "
<< bad_input[i]->second
<< "\n";
Code:
vector<map<string, string>::iterator>::iterator v_it;
for(v_it = bad_input.begin(); v_it != bad_input.end(); ++v_it)
cout << v_it-> ??
TIA
Comment