Generate strings with all permutation of characters

I have the following code

#include <iostream>
#include <string>
using namespace std;
string generate(){
     for (char c1='A';c1<='Z';c1++){
          for (char c2='A';c2 <='Z';c2++){
               for (char c3='A';c3<='Z';c3++){
                    for (char c4='A';c4<='Z';c4++){


                         return  (new string *)(c1) + (new string*)(c2)+(new string*)(c3)+(new string*)(c4);
                    }
               }
          }
     }


}
int main(){




     return 0;
}

I want to generate lines, but there is an error here

1>------ Build started: Project: string_combinations, Configuration: Debug Win32 ------
1>Build started 9/11/2010 12:42:08 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\string_combinations.unsuccessfulbuild".
1>ClCompile:
1>  string_combinations.cpp
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.82
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

please help, I got confused why I cannot directly convert from char to string using this method string (char)

0
source share
4 answers

The problem is your expressions of this form:

(new string *)(c1)

The left side is not a type, it is an expression. When you suffix with another expression in parentheses, it looks like a function call, but this only works if the left expression is the name of the function or the pointer to the function. In this case, the new expression has a type std::string**that is not a function pointer.

char, new, ; . , a char . 1 - , :

std::string(1, c1);

- .

return std::string(1, c1) + std::string(1, c2);

, , return for, , compination.

+3

, , std:: next_permutation. , .

:

#include <algorithm>
    using std::next_permutation;
#include <iostream>
    using std::cout;
    using std::endl;
#include <string>
    using std::string;

int main()
{
    string currentPermutation = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    size_t i=0;
    do
    {
        cout << "permutation " << i << ": " << currentPermutation << endl;
        ++i;
    } while( next_permutation(currentPermutation.begin(), currentPermutation.end()) );
    return 0;
}

.

+5

You must use stringstream to create your string as follows:

stringstream s;
s << c1 << c2 << c3 << c4 << ends;
return s.str();

+2
source

So, I see that you are in VC ++ 10:

#include <array>
#include <algorithm>
#include <list>
#include <string>

int main() {

    std::array<char, 24> tAlphabet = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', // and so on...
    };

    // you could store permutations here, but there will be really, really many of them
    std::list<std::string> tAllPermutations;
    do {
        std::string tCurrentPermutation;

        std::for_each(tAlphabet.begin(), tAlphabet.end(),
            [&tCurrentPermutation] (char tCurrentChar) -> void {
                tCurrentPermutation += tCurrentChar;
        });

        std::cout << tCurrentPermutation << std::endl;
    } while (std::next_permutation(tAlphabet.begin(), tAlphabet.end()));
}
-1
source

All Articles