How to detect first or last element iterating over a container?

Posted by Basilevs on Stack Overflow See other posts from Stack Overflow or by Basilevs
Published on 2010-04-14T11:19:31Z Indexed on 2010/04/14 11:23 UTC
Read the original article Hit count: 171

Filed under:
|

How to do the following in more stylish/short way?

for(i=container.begin(); i!=container.end(); ++i) {
    if (i!=container.begin()) {
        cout << ", ";
    }
    cout << *i;
    if (i==container.end()) {
        cout << "!" << endl; 
    }
}

Solutions like foreach are acceptable (actions on first and last elements need to be configurable, though).

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl