How do I use a permuted number outside while loop like to make comparisons.. Because I have checked, vector outside while is same as defined earlier.
void display(vector<int> vec){
for (int i=0; i<vec.size();i++){
cout<<vec[i];
}
}
int main()
{
vector <int> vec;
vec.push_back(3);
vec.push_back(2);
vec.push_back(1);
vec.push_back(4);
sort(vec.begin(), vec.end());
while (next_permutation( vec.begin(), vec.end() )){
display(vec);
cout<<endl;
}
The following complete program works fine both with Visual C++ and with g++:
The main code is identical to your code (it was copied and pasted, and just edited a teeny tiny little bit to add the data).
So, given that the code works, what exactly is the problem?
EDIT: the OP has now updated the question with different code, a
whileloop instead of adoloop, and the order of the calls tonext_permutationanddisplaychanged so that it fails to display the original permutation.The new code given,
does not compile even after adding the requisite headers and a
using namespace std;.While this is only due to a missing right brace, it would have been avoided by copying and pasting the code from a working version.
That is also much less work to do. 🙂