How to walk through a set with inserted data?

1

I have a program that inserts some integers into a set, and removes when I find an equal, after doing so, I need to bring in the output all integers that are still inside the set, however I am not able to do set and bring the information I want. Here is the code:

#include <stdlib.h>
#include <iostream> 
#include <set>

using namespace std;

int main (){

int n,k,x;
set <int> di;
set <int>:: iterator it;

cin>>n;
for(int i=0; i<n; i++){
    cin>>k;
    for (int j=0; j<k; j++){
        if (i!=0){
            cin>>x;
            it = di.find(x);
            if (it!=di.end())
                di.erase(it);
        }
        else{
            cin>>x;
            di.insert(x);
        }
    }
}
it = di.begin();
while(it!=di.end()){
    cout<<&it;
    di.erase(it);
    it++;
}
}
    
asked by anonymous 14.10.2018 / 01:16

1 answer

1

To cycle through any container in the default C ++ library, you can use #:

for (auto elemento : contêiner)
    faz_algo(elemento);

In your case, the container in question is a std::set . The idea is the same:

std::set<int> di{1, 2, 3};
for (int x : di)
    std::cout << x << '\n'; // imprime 1, 2 e 3.
    
14.10.2018 / 04:34