If I have a linked list which is empty can I do something like this?
so I can test iter for l.end() later on?Code:std::list<int> l; iter = l.end();
it seems to work but it feels like its not good
If I have a linked list which is empty can I do something like this?
so I can test iter for l.end() later on?Code:std::list<int> l; iter = l.end();
it seems to work but it feels like its not good
Yes, you can, but following the principle of declaring variables near first use, I would try and only declare later on when I actually want to test for the end.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If the list is modified, is it guaranteed that end() won't change? I thought it's defined to point to one past the end of the container.
Doesn't work for vectors -
For OP: why not just compare what you need to compare against l.end()?Code:#include <iostream> #include <vector> int main() { std::vector<int> a; std::vector<int>::iterator end_it = a.end(); a.push_back(5); std::cout << (a.end() == end_it) << std::endl; }
Insertion to a std::list does not invalidate iterators to elements of the list, and deletion from a std::list only invalidates iterators to the deleted elements.Originally Posted by cyberfish
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ah I see. Thanks.