jeudi 21 décembre 2017

C++ erase() in a function

It is expected that element '2' has been removed when the set elements are output in the 'helper' function. But the actual result is still '1 2 3 4 5'. However, the size of the set is 4. I wonder the underlying problem.Thank you!

#include <iostream>
#include <set>

using namespace std;

void helper(set<int> myset,set<int>::iterator it){
myset.erase(it);

cout<<"size:"<<myset.size()<<endl;
for(auto el:myset)
    cout<<el<<" ";
}


int main(int argc, const char * argv[]) {
set<int> myset={1,2,3,4,5};

auto it=myset.begin();
it++;
helper(myset,it);

return 0;
}

Aucun commentaire:

Enregistrer un commentaire