std::list example:: iterate list

Two ways to iterate list

1. use this one when need to remove item from the list.

1
2
3
4
5
6
7
8
9
std::list<int>::iterator it = m_clientList.begin();
 
    while (it != m_clientList.end()) {
        if (xxxx) {
            it = m_clientList.erase(it);
        } else {
            ++it;
        }
    }

2. normal iterate

1
2
3
4
5
6
list<int> copylist = GetClients();
        for (std::list<int>::iterator it = copylist.begin();
                it != copylist.end(); ++it) {
            int n = *it;
//do something with n
        }