S
SpOiLeR
Hi!
Let's assume I have this
typedef std::list<std::string> string_list;
string_list sl;
If I want to traverse through this, I usually do this:
string_list::iterator i = sl.begin();
while (i != sl.end())
{
// do some work
}
This checks all strings in sl because end() gives iterator pointing to
position after the last element.
Now I want to do something a bit different:
string_list::iterator j = sl.end();
bool is_good = true;
while (j != sl.begin() && is_good)
{
is_good = IsGood (*j);
if (is_good) j--;
}
But, this leaves first string in sl unchecked (because begin() gives
iterator pointing to first element), so after looping I must do this:
if (is_good) is_good = IsGood (*j);
Is there a way to traverse backwards through string_list but to avoid this
additional check (do everything in loop)?
Let's assume I have this
typedef std::list<std::string> string_list;
string_list sl;
If I want to traverse through this, I usually do this:
string_list::iterator i = sl.begin();
while (i != sl.end())
{
// do some work
}
This checks all strings in sl because end() gives iterator pointing to
position after the last element.
Now I want to do something a bit different:
string_list::iterator j = sl.end();
bool is_good = true;
while (j != sl.begin() && is_good)
{
is_good = IsGood (*j);
if (is_good) j--;
}
But, this leaves first string in sl unchecked (because begin() gives
iterator pointing to first element), so after looping I must do this:
if (is_good) is_good = IsGood (*j);
Is there a way to traverse backwards through string_list but to avoid this
additional check (do everything in loop)?