Xara
Xara

Reputation: 9098

C++ : iterate through a vector of set of vectors

I want something like this:

{ {0,1},{0,2},{0,3},{0,4}...... {255,255} }  ---> (1)

After some processing I have to get something like this:

 { {0,1},{0,2},{0,3} }
 { {0,4} }
 { {5,6},{255,255} }

Thus, initially one big set consists of a number of subsets. Each subset consists of two values. After some processing the one big set in (1) decomposes like above.

Now I want to iterate through the set in (1) and get each value from each subset comprising of two elements.

Here is my code. Please guide me how would I extract subset values.

CrtBestPartitionDouble corresponds to a big set consisting of subsets. subgroup is a subset comprising of two elemets doublegroup is a set consisiting of all the subsets

    vector<set<vector<int> > > CrtBestPartitionDouble;
vector<set<vector<int> > > ::iterator itr;

set<vector<int> > doublegroup;
set<vector<int> >::iterator itr2;
    vector<int>::iterator itr3 ;
      for(int k=0; k <5; k++)
       {
        for(int j=0; j <5; j++)
           {       vector<int> subgroup;
            subgroup.push_back(k);
            subgroup.push_back(j);
            doublegroup.insert(subgroup);
        }


    }
    CrtBestPartitionDouble.push_back(doublegroup);

for ( itr = CrtBestPartitionDouble.begin(); itr != CrtBestPartitionDouble.end(); ++itr ) {

        // Iterate each set
        for ( itr2 = itr->begin(); itr2 != itr->end(); ++itr2 ) {

            // Iterate each subset
            for (  itr3 = itr2->begin(); itr3 != itr2->end(); ++itr3 ) {
                std::cout << *itr3;
            }
        }
    }

It gives me following error:

ST.cpp: In function ‘void StateTableGenerator()’:
ST.cpp:460:39: error: no match for ‘operator=’ in ‘itr3 = itr2.

Upvotes: 0

Views: 2313

Answers (2)

TNA
TNA

Reputation: 2745

With range based for:

for(const auto& a: CrtBestPartitionDouble))
{
  for(const auto& b: a)
  {
    for(const auto& c: b)
      std::cout << c;
  {
}

Upvotes: 1

marcinj
marcinj

Reputation: 49976

you can use this code to iterate over all values, I'am not sure if this is what you want

   typedef vector<set<set<int> > > CrtBestPartitionDoubleType;
    CrtBestPartitionDoubleType CrtBestPartitionDouble;

    // Iterate each vector element
    for ( CrtBestPartitionDoubleType::iterator itr = CrtBestPartitionDouble.begin(); itr != CrtBestPartitionDouble.end(); ++itr ) {

        // Iterate each set
        for ( set<set<int> >::iterator itr2 = itr->begin(); itr2 != itr->end(); ++itr2 ) {

            // Iterate each subset
            for ( set<int>::iterator itr3 = itr2->begin(); itr3 != itr2->end(); ++itr3 ) {
                std::cout << *itr3;
            }
        }
    }

Upvotes: 1

Related Questions