生成具有所有字符排列的字符串 [英] generate strings with all permutation of character

查看:115
本文介绍了生成具有所有字符排列的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

#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 ==========

请帮助我困惑为什么我不能直接从char转换为string by这个方法string(char)

please help i am confused why i can't directly convert from char to string by this method string(char)

推荐答案

问题是这个表单的表达式:

The problem is with your expressions of this form:

(new string *)(c1)

左侧不是一个类型,它是一个表达式。当使用另一个括号表达式后缀时,它看起来像一个函数调用,但只有左表达式是函数名或函数指针时才起作用。在这种情况下,新表达式具有类型 std :: string ** ,它不是函数指针。

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.

要从单个 char 构建临时字符串,您不应使用动态分配对象的 new 而是可以使用构造函数。一个合适的是一个计数,并且 char 为该计数重复。在你的情况下,计数1是你想要的:

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);

注意,你也不会在任何地方调用generate,如果你 return for 循环的第一次迭代你不会迭代所有的组合,你只会每个生成第一个compination。

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.

这篇关于生成具有所有字符排列的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆