Archived

This topic is now archived and is closed to further replies.

iterator problems

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay i have a little problem when its comes to using iterators. I got a vector
typedef vector StringVector;
 
And a map
map< string, string >
 
The thing i want to do is to iterate thrught the vector. And check if the string is equal to any _key in the map. if it is, then replace it with the _value that the _key reprents. I get some not to weird compiler errors doing this and im generaly unsecure about how to do this. Here is what i have come up with so far: *note: picket this out of a larger method of a class, so I had to modify it so it could be more easely read*
void MyReplace( StringVector& vs, map<string,string>& KeyList )
{
	StringVector::iterator iter = vs.begin();
	StringVector::iterator  end = vs.end();
	map<string,string>::iterator	  cmp;
	
	while( iter != end )
	{
		cmp = KeyList[ (*iter) ];
		if( cmp != KeyList.end() ) //match

		{
			iter = cmp->second;
		}

		iter++;
	}
}

Share this post


Link to post
Share on other sites
Try using std::map::find() instead. Even if you got the code using std::map::operator[] to compiler, it wouldn''t do what you wanted because std::map::operator[] auto-inserts into the map.

Share this post


Link to post
Share on other sites