Hey, I've been having some trouble with getting reverse iterators to work along with the standard algorithms. Since they only accept a normal iterator I can't do any computation using a reverse iterator unless I can somehow "convert" them. I tried using .base() but it didn't work out quite well. So to try and "emulate" reverse iterators I assigned a normal iterator to the end of a vector and minus 1 (which is a bit hackish and not to my liking). Is there a way to make a reverse iterator work with the algorithms? The code below is trying to implement a "stack" LIFO structure that also allows me to pop by position.
I originally had something like:Code:std::string CompressStack::pop(int pos) { std::vector<std::string>::iterator iter = wordStack.end()-1; int cnt = 1; if (pos >= wordStack.size()) { std::cerr << "Invalid size."; std::string str("NULL"); return str; } while (cnt < pos) { iter--; cnt++; } std::string str(*iter); /* a bit hackish but still works */ std::cerr << "Deleting: " << *iter << std::endl; wordStack.erase(iter); // wordStack.erase(std::find(wordStack.rbegin(),wordStack.rend(),*iter).base()); return str; }
Obviously I used the increment operator instead of the decrement operator in the while loop. And I tried using the commented out statement which didn't seem to function as I intended. It would find the first instance from the "front" of the stack that matched the value. Having 2 values that are the same and wanting to delete the "second" similar value causes incorrect behaviour since it would delete the first instance instead of the second.Code:std::vector<std::string>::reverse_iterator iter = wordStack.rbegin();
Any feedback would be great! Thanks![]()



LinkBack URL
About LinkBacks



