Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am really confused how the do-while loop in this program works especially the insert operation of a vector into a set
C++
s.insert(v)
. Do anyone can explain what happens in this loop?

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 = 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+10) );
  
  cout << "number of permutations = " << s.size() << endl;

  for(auto c: s){
    for (auto i: c)
        cout << i << " ";
    cout << endl;
  }
  
  return 0; 
}
Posted
Updated 3-Aug-15 21:18pm
v3
Comments
Sergey Alexandrovich Kryukov 4-Aug-15 0:44am    
What do you mean "how"? What's the problem?
—SA
amin.j 4-Aug-15 0:47am    
Dear Sergey, I don't know how it works and what happens when program reaches this line? If you can give me an explanation by example, that would be great.
barneyman 4-Aug-15 0:53am    
it's an insert OF a vector into a set, not ON a vector
amin.j 4-Aug-15 0:56am    
I know but how it works? Please give me a kind of example by this program.
barneyman 4-Aug-15 1:46am    
i'm not sure i understand ... if you want to know what that code's doing, single step it and find out (it looks to be building 2 digit sets from all the permutations of the array 0..9)

if you want to know how a std::set works, look at the header, though knowing the specifics wouldn't be too useful IMHO

C++
do {
    vector<int> v;  // create a new empty vector called v
    for (int i = 0; i < N; i++) // start a loop of N iterations
        v.push_back(myints[i]);  // add the integer at myints[i] to the vector v
    s.insert(v);  // on completion of the inner loop, add the vector v to the set s
  } while ( std::next_permutation(myints,myints+10) ); // repeat until this expression is false
</int>
 
Share this answer
 
set::insert is well documented, see, for instance "std::set::insert"[^]. In your case is inserting a vector of (2) integers into s. Nothing more, nothing less.
 
Share this answer
 
v2
Comments
amin.j 4-Aug-15 3:04am    
Thanks. But I mean how set is filled; in fac, if it is possible, please give me the explanation of the first itheration of the do-while loop when i = 0; what happens in the loop and what is the output?
Your problem might be that you don't know what a set is. A set is container class like vector, except that you cannot insert the same element twice. In your case s is a set of objects of type vector<int>. Hence, s.insert(v) inserts the vector v into the set s.

To understand how a set works you might want to read:
http://www.cplusplus.com/reference/set/set/[^]

One of the important things is that a set keeps its elements in sorted order. When you insert a new element, a binary search is used to find the place where the element has to be inserted.

See http://www.cplusplus.com/reference/set/set/insert/[^] to read more about the insert operation.

Hope that made it a little clearer.

[AMENDED]

Here is a line by line description of what happens:
C++
do {
    vector<int> v; // declares a vector of ints

    // this loop copies the values of the myints array
    // into v (note that the call to next_permutation will rearrange
    // the values in myints at the end of each iteration)
    for (int i = 0; i < N; i++)
       v.push_back(myints[i]);

    // inserts the vector v into the set s; as a set keeps its elements
    // in an ordered sequence, the insert function has to compare the new
    // element (v) with the elements already stored in the set. This comparison
    // is performed by the compare function specified in the sets
    // template parameters, and as no such parameter is specified, the
    // operation< member function of vector<int> is used, which compares two
    // vectors according to their lexicographical order. (Perhaps this is
    // the part you didn't understand)
    s.insert(v);

// calls std::next_permutations, which rearranges the values in
// the myints array; once the original permutation (0, ...9) has been reached,
// next_permutation will return false and the loop ends
} while ( std::next_permutation(myints,myints+10) );


[2nd AMENDMENT]

C++
// loop over all elements of set s and assign each one in turn to a variable c
// (note: the auto keyword lets c assume the type that is the element type of
// set s, which is vector<int>)
for(auto c: s){

  // so now we have in c one of the vector<int> elements stored in s; in
  // the inner loop, we walk over all elements of that vector<int> and each
  // is in turn assigned to variable i
  for (auto i: c)
      // we output that element i and a space
      cout << i << " ";

  // after the inner loop has output all elements of c, we terminate
  // the output line
  cout << endl;
}


I hope that makes it clear what the nested loops do.
 
Share this answer
 
v4
Comments
amin.j 4-Aug-15 3:04am    
Thanks. But I mean how set is filled; in fact, if it is possible, please give me the explanation of the first iteration of the do-while loop when i = 0; what happens in the loop and what is the output?
nv3 4-Aug-15 3:55am    
See the amendment to my solution.
amin.j 4-Aug-15 4:09am    
Dear nv3 many thanks for your valuable comments in your solution.
nv3 4-Aug-15 4:14am    
You are welcome. Hope it helped.
amin.j 4-Aug-15 7:02am    
Dear nv3, I have another problem. I don't understand the nested for-loop at the end of program (auto i: c); if it is possible to write it more simply? In fact, I want to use the elements of the set s and how I can do this?

for(auto c: s){
for (auto i: c)
cout << 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