我怎样把许多字符放到一个字符串里?
How Can I Take Many Chars and Put Them Into One String
我想我只是从来没有学过这个。我以前从来没有做过这个。我见过strcat(S1, S2)
的用法,但在这里不适用,是吗?
string all_possible_strings[10];
char jumbled_chars[] = "ABCDEFG";
all_possible_strings[1] = jumbled_chars[0] << jumbled_chars[1]
<< jumbled_chars[2] << jumbled_chars[3]
<< jumbled_chars[4];
我想做的是编写一个程序,它可以将一个单词分解成所有可能的排列。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string theString = "";
char a = 'a';
char b = 'b';
const char* c = "cdefghijklmnopqrstuvwxyz";
theString += a;
theString += b;
theString += c;
cout << theString;
return 0;
}
打印出整个字母表
使用append
功能或std::string
的operator+=
过载。你应该仔细阅读STL文档。
如果jumbled_chars
已经是你想要的顺序,那么你可以只构造像
all_possible_strings[counter] = std::string(jumbled_chars, 5);
更新:
好的,这里有一些建议。不要将字符串存储在数组中,而是使用std::vector
。
std::vector<std::string> possible_strings;
std::string jumbled_chars; //This could be a char[] or char* or whatever
我将把如何准确地获得字符串的所有排列留给读者作为练习。但是,假设您希望按照w
, x
, y
, z
的顺序获得jumbled_chars
,其中w-z
是jumbled_chars
的索引:
std::string str = "";
str += jumbled_chars[w];
str += jumbled_chars[x];
str += jumbled_chars[y];
str += jumbled_chars[z];
possible_strings.push_back(str);
相关文章:
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 如何检查一个c++字符串中有多少相同的字符/数字
- 获取用C/C++打印的最后一个字符串
- cin >> int 给定一个字符串将 int 赋值为 0
- 我如何通过引用将 c++ 字符串分配给另一个字符串
- 使用另一个字符串从字符串中删除空格
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 当分配一个字符串值并稍后通过分配另一个值进行更改时C++如何管理内存?
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- C++为 void 指针分配一个字符串
- 如何在使用 getch 后在另一个字符串的末尾连接一个字符串
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 如何在另一个字符串的x位置插入一个字符串?
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- 在另一个字符串中插入文件扩展名之前的字符串
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- C++自定义流操纵器,用于更改流上的下一个字符串
- 从类返回一个字符串 — 奇怪的行为
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- 你能从"volatile const char*"构造一个字符串吗?(不使用`const_cast`