Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11925 > unrolled thread
| Started by | sanket <sanketsharma09@gmail.com> |
|---|---|
| First post | 2011-11-28 17:46 -0800 |
| Last post | 2011-11-29 20:55 -0800 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.c++
Best way to print all possible permutations of a string sanket <sanketsharma09@gmail.com> - 2011-11-28 17:46 -0800
Re: Best way to print all possible permutations of a string Ian Collins <ian-news@hotmail.com> - 2011-11-29 15:01 +1300
Re: Best way to print all possible permutations of a string sanket <sanketsharma09@gmail.com> - 2011-11-29 00:24 -0800
Re: Best way to print all possible permutations of a string DDD <1983ddd@gmail.com> - 2011-11-29 20:55 -0800
| From | sanket <sanketsharma09@gmail.com> |
|---|---|
| Date | 2011-11-28 17:46 -0800 |
| Subject | Best way to print all possible permutations of a string |
| Message-ID | <be537869-f936-4cce-abe0-9eab53b548fd@gl2g2000vbb.googlegroups.com> |
Hi all.
I wrote a program to print all permutations of the letters of a
string. Here are my two implementations
1.
include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char ** argv)
{
if(argc < 2) {cout << "Usage:\na.out <string>"<<
endl;abort();}
string s(argv[1]);
for(string::iterator i = s.begin(); i != s.end(); i++)
{
for(string::iterator j = s.begin(); j != s.end(); j++)
{
if(j == i) continue;
for(string::iterator k = s.begin(); k !=
s.end(); k++)
{
if(k == j || k == i) continue;
for(string::iterator p = s.begin(); p !
= s.end(); p++)
{
if(p == k||p == j||p == i)
continue;
cout << *i << *j << *k << *p
<< endl;
}
}
}
}
}
2.
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main(int argc, char ** argv)
{
if(argc < 2) {cout << "Usage:\na.out <string>"<<
endl;abort();}
string s(argv[1]);
sort(s.begin(), s.end());
cout << s << endl;
while(next_permutation(s.begin(), s.end())){cout << s <<
endl;}
return 0;
}
They seem to be working well, but I was wondering if there was a
better way to implement the same without using the STL function
next_permutation.
Thanks,
Sanket
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-11-29 15:01 +1300 |
| Message-ID | <9jisn9F4d0U29@mid.individual.net> |
| In reply to | #11925 |
On 11/29/11 02:46 PM, sanket wrote:
> Hi all.
>
> I wrote a program to print all permutations of the letters of a
> string. Here are my two implementations
<snip>
> 2.
>
> #include<iostream>
> #include<algorithm>
> #include<cstdlib>
> using namespace std;
>
> int main(int argc, char ** argv)
> {
> if(argc< 2) {cout<< "Usage:\na.out<string>"<<
> endl;abort();}
Calling abort is rather drastic, why not just "return EXIT_FAILURE"?
> string s(argv[1]);
> sort(s.begin(), s.end());
> cout<< s<< endl;
> while(next_permutation(s.begin(), s.end())){cout<< s<<
> endl;}
> return 0;
> }
>
> They seem to be working well, but I was wondering if there was a
> better way to implement the same without using the STL function
> next_permutation.
s/STL/standard library/
Why would you want to avoid a library function that does what you want
to do?
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | sanket <sanketsharma09@gmail.com> |
|---|---|
| Date | 2011-11-29 00:24 -0800 |
| Message-ID | <1f01f8a4-d251-4b21-aaf7-df32b71072a7@h3g2000yqa.googlegroups.com> |
| In reply to | #11926 |
On Nov 28, 8:01 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 11/29/11 02:46 PM, sanket wrote:
>
> > Hi all.
>
> > I wrote a program to print all permutations of the letters of a
> > string. Here are my two implementations
>
> <snip>
>
> > 2.
>
> > #include<iostream>
> > #include<algorithm>
> > #include<cstdlib>
> > using namespace std;
>
> > int main(int argc, char ** argv)
> > {
> > if(argc< 2) {cout<< "Usage:\na.out<string>"<<
> > endl;abort();}
>
> Calling abort is rather drastic, why not just "return EXIT_FAILURE"?
>
> > string s(argv[1]);
> > sort(s.begin(), s.end());
> > cout<< s<< endl;
> > while(next_permutation(s.begin(), s.end())){cout<< s<<
> > endl;}
> > return 0;
> > }
>
> > They seem to be working well, but I was wondering if there was a
> > better way to implement the same without using the STL function
> > next_permutation.
>
> s/STL/standard library/
>
> Why would you want to avoid a library function that does what you want
> to do?
>
> --
> Ian Collins
I am a newbie and wanted to see if my first implementation is optimal.
I should have asked this,
Is it possible to further optimize my first implementation?
--Sanket
[toc] | [prev] | [next] | [standalone]
| From | DDD <1983ddd@gmail.com> |
|---|---|
| Date | 2011-11-29 20:55 -0800 |
| Message-ID | <eef10b6e-1be0-4870-97be-1c1babe2c8ab@y12g2000vba.googlegroups.com> |
| In reply to | #11935 |
On Nov 29, 4:24 pm, sanket <sanketsharm...@gmail.com> wrote:
> On Nov 28, 8:01 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On 11/29/11 02:46 PM, sanket wrote:
>
> > > Hi all.
>
> > > I wrote a program to print all permutations of the letters of a
> > > string. Here are my two implementations
>
> > <snip>
>
> > > 2.
>
> > > #include<iostream>
> > > #include<algorithm>
> > > #include<cstdlib>
> > > using namespace std;
>
> > > int main(int argc, char ** argv)
> > > {
> > > if(argc< 2) {cout<< "Usage:\na.out<string>"<<
> > > endl;abort();}
>
> > Calling abort is rather drastic, why not just "return EXIT_FAILURE"?
>
> > > string s(argv[1]);
> > > sort(s.begin(), s.end());
> > > cout<< s<< endl;
> > > while(next_permutation(s.begin(), s.end())){cout<< s<<
> > > endl;}
> > > return 0;
> > > }
>
> > > They seem to be working well, but I was wondering if there was a
> > > better way to implement the same without using the STL function
> > > next_permutation.
>
> > s/STL/standard library/
>
> > Why would you want to avoid a library function that does what you want
> > to do?
>
> > --
> > Ian Collins
>
> I am a newbie and wanted to see if my first implementation is optimal.
> I should have asked this,
> Is it possible to further optimize my first implementation?
>
> --Sanket
You need an algorithm. And you can choose recursion or not.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web