Click here to Skip to main content
15,887,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't understand the nested for-loop at the end of this code. How we can write it more simply? And how I can reach the elements of set s to use in this program (in arrays we use sth like this a[i], here how it is possible)?


C++
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort
#include <vector>
#include <set>

using namespace std;

int main () {
  int myints[] = {0,1,2,3,4,5,6,7,8,9};

    int N;
    cout << "insert the number of bits:";
    cin >> N;
    cout << endl;
    
    vector<int> v;
    v.push_back(myints[4]);
    cout << v[0];
    cout << v[1];
    cout << v[2];


  set<vector<int>> s;
  do {
      vector<int> v;
      
      for (int i = 0; i < N; i++) 
        v.push_back(myints[i]);
        
      s.insert(v);
  } while (std::next_permutation(myints, myints+4));

  cout << "number of permutations = " << s.size() << endl;
    
  for(auto c: s){
    for (auto i: c)
        cout << i << " ";
    cout << endl;
  }

  return 0;
}
Posted
Updated 4-Aug-15 1:36am
v4
Comments
nv3 4-Aug-15 7:38am    
See my answer to your previous question, in which you have asked the same. I think the nested loop is already pretty simple and can't see how this could be done with even less code.

If you don't like the range based for[^], you could explicitely use an iterator, however I doubt the code would be more understandable.

C++
// it iterates over set s items (each item is a vector):
//   at start it = s.begin(), that is it 'points' to the first vector in the set
//   each time it is incremented, it 'points' to the following vector in the set
//  (*it) is a reference to the current vector
for( set<vector<int>>::iterator it = s.being(); it != s.end(); ++it)
{
   // here we iterate over the items (int) of the current vector
   // using the 'usual' for and the [] operator
   for ( int n=0; n < it->size(); ++n)
   {
      cout << (*it)[n] << " ";
   }
}
 
Share this answer
 
v4
Comments
amin.j 4-Aug-15 8:07am    
Thanks. I got that but not completely. If you can give comments about that, it would be great.
It seems that 's' is a matrix which is created with all the possible permutations between 2 different numbers in the vector 'myints'.
's' is a matrix of size 2 x 90, where 90 is the number for permutations which you can create.
The matrix 's' will look as follows:
{0, 1}
{0, 2}
:
:
{0, 9}
{1, 0} // notice that there is no permutation of {1, 1}, {2, 2} ... {9, 9}
{1, 2}
:
:
{9, 8}


Looking at the nested-loop code:
for(auto c: s){
    for (auto i: c)
        cout << i << " ";
    cout << endl;


'auto' simply iterate over the elements of a vector.

The first for-loop will iterate through all the permutation pairs in s. And the nested for-loop will iterate through the permutation pair items, 'c' is a the parameter which represents a single permutation pair and 'i' represents one of the (two) items in 'c'.
 
Share this answer
 
v4
Comments
amin.j 4-Aug-15 7:59am    
Thanks. This kind of for loop, with auto keyword, is the only way to reach elements? I mean isn't there the old fashioned for loop style: for example,
for(int i = 0; i < max; i++)
Merav Kochavi 4-Aug-15 8:15am    
I believe the auto is the simplest and most readable way to iterate through this type of data structure.
Merav Kochavi 4-Aug-15 8:40am    
You can also use an iterator to iterator over 's' and use the old fashion loop style to iterate over 'c' as such:

for (set<vector<int>>::iterator c = s.begin(); c != s.end(); c++)
{
for (int i = 0; i < c->size(); i++)
cout << (*c)[i] << " ";
cout << endl;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900