473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Standard C++ Generic Algorithm 'next_permutation()'

542 512MB
The example of this algorithm is taken from the book I'm learning from.
It compiles but won't run. Can anyone see what's wrong?
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<algorithm>
  3. //have also included <cmath> and <cstdlib> to no affect.
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     char* s="ABCD";
  9.     for(int i=0;i<24;i++)
  10.     { next_permutation(s,s+4);
  11.     cout<<(i%8?'\t':'\n')<<s;
  12.     }
  13. system("pause");
  14. return 0;
  15.  
  16. }
//output is meant to be:-
ABDC ACBD ACDB etc ..to ABCD ie(.until all 24 combinations
are printed.)
Appreciate any thoughts.
Oct 1 '08 #1
7 3679
oler1s
671 Expert 512MB
Define, "won't run".
Oct 1 '08 #2
newb16
687 512MB
"abcd" is constant ( eg a string within executable code ) and is not supposed to be changed. Copy it to some other string beforehand.
Oct 1 '08 #3
whodgson
542 512MB
oler1s
'won't run' means (loosely) that when the IDE Bloodshed Dev C++(v 4.9.9.2) runs the .exe file the monitor screen changes to a black DOS screen and after a few second back to the IDE screen whereupon a Windows message box appears which says:
"next_permutation.exe has encountered a problem and needs to close. We are sorry for the inconvenience." It offers to tell Microsft about the problem.
newb16
I will grapple with your solution.
Oct 2 '08 #4
whodgson
542 512MB
Thanks -- this is how it ended up
//30/09/08 17:03
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. int main()
  6. {char A,B,C,D;
  7.     char s[4]={'A','B','C','D'};
  8.      for(int i=0;i<24;i++)
  9.      {next_permutation(s,s+4);
  10.      cout<<s[0]<<s[1]<<s[2]<<s[3]<<" ";
  11.      if(i==7||i==15)cout<<"\n";
  12.      }
  13. cout<<endl;
  14. system("pause");
  15. return 0;
}/*
output:
ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD
BCDA BDAC BDCA CABD CADB CBAD CBDA CDAB
CDBA DABC DACB DBAC DBCA DCAB DCBA ABCD*/
Oct 3 '08 #5
JosAH
11,448 Expert 8TB
You can also write line #7 like this:

Expand|Select|Wrap|Line Numbers
  1. char s[4]= "ABCD";
  2.  
It does exactly the same but is a bit more convenient to write.

kind regards,

Jos
Oct 3 '08 #6
whodgson
542 512MB
Thanks Jos and Newb16....i get the idea.
Oct 4 '08 #7
comment to JosAH's reply
You can also write line #7 like this ...
... or even more convenient (avoiding redundancy):
Expand|Select|Wrap|Line Numbers
  1. char s[]= "ABCD";
Nov 18 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.)...
4
by: jose luis fernandez diaz | last post by:
Hi, I want to write a function of four parameters. Each parameter can be of type long, double or string. For example: f(long, long, long, long); f(long, long, long, double); f(long, long,...
4
by: m sergei | last post by:
I am not asking for code but wanted help with understanding the algorithm to permute all characters of a string. say string is "ABCD" I want to know the algorithm for finding all permutations of...
2
by: deancoo | last post by:
I've seen an algorithm for permutations, but I need to find combinations. Is there an STL equivalent to next_permutation for combinations? The material I'm finding is kinda light, if anyone would...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
3
by: spakka | last post by:
I'm useless at templates, but, inspired by this post: <hinnant-074D8A.11281207032005@syrcnyrdrs-01-ge0.nyroc.rr.com>, I'm trying to write an algorithm template <class BidirectionalIterator,...
8
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more...
4
by: Harold Howe | last post by:
I am running into a situation where the compiler complains that it cannot infer the type parameters of a generic method when one of the function arguments is an anonymous method. Here is a...
6
by: StephQ | last post by:
I need to implement an algorithm that takes as input a container and write some output in another container. The containers involved are usually vectors, but I would like not to rule out the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.