I wonder weather it is possible to use the the c++11 ranged-based-for-loop syntax to search for an insertion point that is later used in i.e. a list. Can I compact the for (auto i = l.begin(); i != l.end(); i++)
in some way by using the c++11 ranged-based-for-loop syntax instead?
#include <iostream>
#include <list>
int main(int argc, char **argv) {
std::list<int> l = { 1, 2, 3, 5};
for (auto i = l.begin(); i != l.end(); i++) {
if (*i == 3) {
l.insert(i, 4);
break;
}
}
for (auto &i : l) {
std::cout << " " << i;
}
};
In pseudocode something like:
for (auto &i : l) {
if (i == 3) {
l.insert(some_magic_opertor(i), 4);
break;
}
}
or in pseudocode:
typedef std::list<int>::iterator it;
for (it i : l) {
if (*i == 3) {
l.insert(i, 4);
break;
}
}