C++ vector::clear

后端 未结 6 1081
死守一世寂寞
死守一世寂寞 2020-12-09 09:03
vector  decoy;

void clear_decoy() {  

    decoy.clear();   

    vector (decoy).swap(decoy);  
}

In the above method

6条回答
  •  遥遥无期
    2020-12-09 09:31

    I've never seen that form before.

    I have seen it written as:

    vector().swap(decoy);
    

    Which means "create a new empty vector, and swap it with the existing one.

    vector (decoy).swap(decoy);

    To understand that, break in to parts.

    vector(decoy) create a new vector (with it's contents copied from the now empty decoy). The new vector is an anonomous temporary, so let's pretent it's name is newvector.

    newVector.swap(decoy); swaps the new vector with decopy.

    (Updated per comments to fix bug)

提交回复
热议问题