简体   繁体   中英

Unable to print the elements of a set of vector in STL

I am new to STL maps and vector. I am trying to print the elements present inside the set of vector. The last for-loops are used for printing the elements. The code is given below :

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int arr[]={2,0,2,1,4,3,1,0};

    vector<int> v;
    set< vector<int> > st;
    //set<int> temp;
    int init=0,fin=0;
    for(int i=0;i<8;++i){
        if(find(v.begin(),v.end(),arr[i])==v.end()){//if(temp.find(arr[i])==temp.end()){
            v.push_back(arr[i]);//temp.insert(arr[i]);

        }
        else{
            st.insert(v);
            v.clear();//temp.clear();
            v.push_back(arr[i]);//temp.insert(arr[i]);
        }

    }
    set<vector<int> >::iterator itr;
    vector<int>::iterator str;
    for(itr=st.begin();itr!=st.end();++itr){
        for(str=itr->begin();str!=itr->end();++str){
            cout<<*str<<" ";
        }
        cout<<endl;
    }
    return 0;

}

The error is:

a.cpp:26:11: error: no viable overloaded '='
                        for(str=itr->begin();str!=itr->end();++str){
                            ~~~^~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1258:7: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from '__wrap_iter<const_pointer>'
      to 'const __wrap_iter<int *>' for 1st argument
class __wrap_iter
      ^
a.cpp:34:2: error: unknown type name 'a'
        a.cpp:26:10: error: no viable overloaded '='
        ^
a.cpp:34:3: error: cannot use dot operator on a type
        a.cpp:26:10: error: no viable overloaded '='

Your str iterator needs to be const because it is not possible to mutate the elements of the set.

vector<int>::const_iterator str;

I'm not the biggest fan of auto , but this is one occasion where if you use it, you don't need to know what the iterator type is

for (auto str = itr->begin(); str != itr->end(); ++str)

If your compiler is C++ 11 compliant, instead of set<vector<int> >::iterator itr;... , try this - it is much cleaner:

  for ( const auto& v : st )
    for ( const auto& i : v )
      cout << i << " ";

Read here about auto and here about rage-for.

[Edit]

"So, does that make the elements as const for the time being inside the loop and not later?"

The set element is constant at any time, otherwise the uniqueness will not hold.

* operator of a set iterator is defined as a function returning a constant reference to iterator's current element; this is why you cannot modify the element. And it is protected in this way for otherwise the uniqueness constraint cannot be guarantee.

Take set {1, 2, 3} and imagine you are changing 1 to 2 . The resulting set, {2, 2, 3} is no longer a set for 2 is not unique.

Instead, if you first remove the element {1, 2, 3} - {1} = {2, 3} and then you try to insert the modified element {2, 3} + {2} , you will end with a correct set: {2, 3} for 2 is not inserted since it is already present.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM