tags:

views:

95

answers:

4

i have 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 strings but here is error

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 am confused why i can't directly convert from char to string by this method string(char)

+3  A: 

The problem is with your expressions of this form:

(new string *)(c1)

The left hand side isn't a type, it's an expression. When you suffix it with another parenthesized expression it looks like a function call but that only works if the left expression is a function name or function pointer. In this case the new expression has type std::string** which isn't a function pointer.

To construct a temporary string from a single char, you shouldn't use new which dynamically allocates an object; instead you can use a constructor. A suitable one is the one which takes a count and a char to repeat for that count. In your case a count of 1 is what you want:

std::string(1, c1);

You can do something like.

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

Note that you also don't call generate anywhere and if you do return from the first iteration of a for loop you aren't going to be iterating through all the combinations, you will only every generate the first compination.

Charles Bailey
+2  A: 

You should use a stringstream to create your string as follows:

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

DanJ
A: 

So, I see you are on 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()));
}
paul_71
Please be aware of the fact that there are 24! = 620448401733239439360000 possible permutations, so running the above programm (the version which stores each permutation) will explode with 'Out of memory' exception. Run at own risc and not on a computer which is in control of a nuclear opwerplant or something similar...!
paul_71
the original code (as I interpret it) was to write all 4 character strings - only 26^4 possibilities.
DanJ
-1: this is misuse of everything you used in your code.
rubenvb
@rubenvb Misuse? That you will have to explain... @DanJ oh, that's fine, then it's only 4! possibilities.
paul_71
I actually misread the OP's intent, and now see that all the extra code you have does serve it's purpose well. Sorry, my bad. Can't undo my downvote until an edit :(
rubenvb
+3  A: 

I'm quite sure the manual looping can be avoided using std::next_permutation. Manual looping like this is terrible, especially when the Standard library foresees this kind of thing.

Here's some quick code:

#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;
}

This will permutate through all combinations of the string.

rubenvb