• Advertisement
Sign in to follow this  

Passing iterator in a function call

This topic is 3958 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

Edited: My apologies, I was tied when I posted this and didn’t do a good job explaining my problem. When I past a pre-defined iterator into my parser function and try to compare it against one I created inside that parser function, the function would fail. But when I just past an offset value and created both iterators inside the function – it worked fine. So I’m not sure if I past the iterator correctly or not. I have another function that takes an iterator as a peramiter and that works fine, but it doesn’t compare the iterator to another iterator. So am I missing something about copies of iterators, or is this suppose to work and maybe I just did something dump in the original code and didn’t duplicate it in the example given? I just modified the code I had back to the way it was when it was failing, but didn't svn the failing code before modifying it =( Example:
typedef std::vector<char> ByteVector;
typedef std::vector<std::string> ResultSet;

ResultSet Parser::split(ByteList msg, ByteList::iterator current) {
	/////////////////////////////////////////
	// Configure the ResultSet
	ResultSet results;
	ByteList::iterator end = msg.end();
	ByteList::iterator next = current;
	end -= 4;	// lets skip over the '001*'
	next++;		// Because we want to see whats comming next with out moving
	
	std::string item;
	while (current != end) {
		if (((*current) == ' ' || (*current) == ':' ) || next == end) {
			if (item.length() > 0) {
				string copy = item;
				results.push_back(copy);
				item = "";
			}
		} else {
			item.push_back((*current));
		}
		current++;
		next++;
	}
	return results;
}

...

ByteVector msg = someFunc();  // returns a 92 char long vector
ByteVector::iterator start = msg.begin();
start += 51;
ResultSet rs = Parser::split(msg, start);

Edited: Fixed the problem by making ByteList::iterator ¤t; [Edited by - MrRage on March 24, 2007 1:20:08 AM]

Share this post


Link to post
Share on other sites
Advertisement
In your example code, you're trying to reach a iterator which is 40 elements before the end, in a vector which contains no elements at all. This is bound to cause problems.

If your example code is not representative of your actual code, please post the actual code between [source][/source] tags.

Share this post


Link to post
Share on other sites
The end() function returns an iterator to one past the end of the list/array - that means the data it points to is always invalid. Performing the -= operator on the iterator will make it go backwards 40 iterations, which may end up being invalid too.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement