[英]How to get every possible string of n characters in c++?
I know it is possible to use n nested for loops to get the result. 我知道可以使用n个嵌套的for循环来获取结果。 This however isn't very flexible.
但是,这不是很灵活。 If I wanted to get every string of n+2 characters I would have to write an extra two for loops.
如果我想获取n + 2个字符的每个字符串,则必须再编写两个for循环。
I'm pretty sure I should use a parameter called n_Letters and use some kind of recursion. 我很确定我应该使用一个名为n_Letters的参数并使用某种递归。 Any ideas?
有任何想法吗? This is how my code looks right now.
这就是我的代码现在的样子。 It gives all the 3 character combinations.
它给出了所有3个字符的组合。
#include <iostream>
#include <string>
using namespace std;
void StringMaker(){
for(int firstLetter = 97; firstLetter < 123; firstLetter++){
char a = firstLetter;
for(int secondLetter = 97; secondLetter < 123; secondLetter++){
char b = secondLetter;
for(int thirdLetter = 97; thirdLetter < 123; thirdLetter++){
char c = thirdLetter;
cout << a << b << c << endl;
}
}
}
}
int main() {
StringMaker(); // I could add a parameter n_Letters here
}
This is a simple tree traversal problem that can easily be solved using recursion. 这是一个简单的树遍历问题,可以使用递归轻松解决。 Using a counter (
count
) and accumulator ( partial
) recur on your function for each letter until count
is zero then print partial
. 对每个字母使用计数器(
count
)和累加器( partial
)在您的函数上重复出现,直到count
为零,然后打印partial
。
#include <iostream>
#include <string>
void StringMaker(int count, std::string partial = "") {
if (count == 0) {
std::cout << partial << '\n';
}
else {
for (char letter = 'a'; letter <= 'z'; ++letter) {
StringMaker(count - 1, partial + letter);
}
}
}
int main() {
StringMaker(3);
return 0;
}
Edit: It seems their are some concerns with my answer regarding memory allocations. 编辑:似乎他们对我有关内存分配的回答有些担忧。 If it's a concern for you, consider this alternative solution.
如果您有问题,请考虑使用该替代解决方案。 Increment the first character if it isn't
'z'
, otherwise set it to a
and repeat with the the second character. 如果第一个字符不是
'z'
,则将其递增,否则将其设置为a
并与第二个字符重复。 Do this until the last character is set from z
to a
. 这样做直到将最后一个字符从
z
设置为a
为止。 This acts as a sort of base 26 counter with count
digits. 这相当于一种带有
count
数字的基数26计数器。
#include <iostream>
#include <string>
void StringMaker(size_t count)
{
std::string data(count, 'a');
size_t i = 0;
do
{
std::cout << data << '\n';
for (i = 0; i < count; ++i)
{
auto & next_char = data[i];
if (next_char < 'z') {
++next_char;
break;
}
else {
next_char = 'a';
}
}
} while (i != count);
}
int main() {
StringMaker(3);
return 0;
}
Here is my just-for-fun solution: 这是我的好玩的解决方案:
void StringMaker(int n)
{
int base = ('z' - 'a' + 1);
std::string str(n, '\0');
for(int i = 0; i < int_pow(base, n); ++i)
{
for(int j = 0; j < n; ++j)
{
str[n - j - 1] = 'a' + i / int_pow(base, j) % base;
}
cout << str << '\n';
}
}
Suppose we have i
written in numerical system with base 26 (from a to z), so increment i with n = 4 give us aaaa, aaab and so on 假设我们用以26为底的数字系统(从a到z)编写了
i
,因此以n = 4递增i会得到aaaa,aaaab等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.