Wednesday, 30 June 2010

Example of Permutations in C++

Example of how you can let the Algorithm class generate permutations of String



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace
std;

int
main()
{

string someString="ABC";
vector<string> someVector;

someVector.push_back(someString);

string::iterator itBegin = someString.begin();
string::iterator itEnd = someString.end();

while
(next_permutation(itBegin, itEnd)) //std::next_permutation defined in algorithm
{
someVector.push_back(string(itBegin, itEnd));
}

copy(someVector.begin(), someVector.end(), ostream_iterator<string>(cout, "\n"));

return
0;
}



The output is as follows:


3 comments:

  1. sir please have a look at my prog:

    #include
    using namespace std;

    int main()
    {
    int a=6,b=2;

    cout<<"original:"<<endl<<"a="<<a<<endl<<"b="<<b<<endl;

    a-=(b=(a+=b)-b);

    cout<<"new values:"<<endl<<"a="<<a<<endl<<"b="<<b;

    getchar();
    return 0;
    }

    ReplyDelete
  2. Not sure, what I should be looking at?

    Did you intend to leave this comment at the post here ?

    ReplyDelete
  3. yes sir...i mistakenly posted it here..

    ReplyDelete