user63898 user63898 - 1 year ago 108
C++ Question

Interleaving std::map insertion and iteration

If I iterate

like this:

typedef std::map<connection, connectionData> clist;
clist m_connections;

for (const auto itt : m_connections)
connectionData outerPlayerData = itt .second;
// Do stuff

and somewhere in the code the
getting filled constantly, does it mean the iterating loop will run forever? Is it growing

or it will run the size of the
in this point of time and the relevant size which was in that point of time?

Answer Source

It will iterate forever if you keep on adding constantly at position greater than the current iteration position. The elements keep on adding and the loop iteration considers the current status of map.


int main() {
    // your code goes here
    map<int, int> m;
    m.insert(make_pair(10, 11));
    int i = 0;
    for(auto it : m){
        cout << it.second << endl;
        if(i++ == 0){ 
            m.insert(make_pair(15, 12));
    return 0;

Gives output as:



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download