如何在C ++中列出长度为12的所有字符串?(How to list all strings of length 12 in C++?)

编程入门 行业动态 更新时间:2024-10-27 06:22:16
如何在C ++中列出长度为12的所有字符串?(How to list all strings of length 12 in C++?)

我是一个研究自己的新手程序员。 我试图创建一个程序,列出长度为12的所有字符串,例如字符来自az。 但是,似乎有一个我能找到的错误。 它输出例如The word is ). 任何人都可以告诉我我做错了什么,有没有更简单的方法来执行该程序?

#include <iostream> #include <string> int main(int argc, char *argv[]) { using namespace std; string l ("qwertyuiopasdfghjklzxcvbnm"); string test (""); for(int i1 = 0;i1 < 26;++i1) for(int i2 = 0;i2 < 26;++i2) for(int i3 = 0;i3 < 26;++i3) for(int i4 = 0;i4 < 26;++i4) for(int i5 = 0;i5 < 26;++i5) for(int i6 = 0;i6 < 26;++i6) for(int i7 = 0;i7 < 26;++i7) for(int i8 = 0;i8 < 26;++i8) for(int i9 = 0;i9 < 26;++i9) for(int i10 = 0;i10 < 26;++i10) for(int i11 = 0;i11 < 26;++i11) for(int i12 = 0;i12 < 26;++i12) { test = l[i1]+l[i2]+l[i3]+l[i4]+l[i5]+l[i6]+l[i7]+l[i8]+l[i9]+l[i10]+l[i11]+l[i12]; cout << "The word is " << test << "." << endl; test = ""; } return 0; }

I'm a novice programmer studying my own. I tried to make a program that lists all strings of length 12 such as its characters are from a-z. However, there seems to be a bug I could find. It outputs for example The word is ). Could anyone tell me what I'm doing wrong, and is there some easier way to do the program?

#include <iostream> #include <string> int main(int argc, char *argv[]) { using namespace std; string l ("qwertyuiopasdfghjklzxcvbnm"); string test (""); for(int i1 = 0;i1 < 26;++i1) for(int i2 = 0;i2 < 26;++i2) for(int i3 = 0;i3 < 26;++i3) for(int i4 = 0;i4 < 26;++i4) for(int i5 = 0;i5 < 26;++i5) for(int i6 = 0;i6 < 26;++i6) for(int i7 = 0;i7 < 26;++i7) for(int i8 = 0;i8 < 26;++i8) for(int i9 = 0;i9 < 26;++i9) for(int i10 = 0;i10 < 26;++i10) for(int i11 = 0;i11 < 26;++i11) for(int i12 = 0;i12 < 26;++i12) { test = l[i1]+l[i2]+l[i3]+l[i4]+l[i5]+l[i6]+l[i7]+l[i8]+l[i9]+l[i10]+l[i11]+l[i12]; cout << "The word is " << test << "." << endl; test = ""; } return 0; }

最满意答案

l[i1]+l[i2]不会做你期望的。 你要添加两个char类型的表达式,这样你就会得到int类型的结果。

简单的解决方法是:

test = std::string() + l[i1]+l[i2]+l[i3]+l[i4]+l[i5]+l[i6]+l[i7]+l[i8]+l[i9]+l[i10]+l[i11]+l[i12];

l[i1]+l[i2] won't do what you expect. You're adding two expressions of type char so you'll get a result of type int.

An easy fix is:

test = std::string() + l[i1]+l[i2]+l[i3]+l[i4]+l[i5]+l[i6]+l[i7]+l[i8]+l[i9]+l[i10]+l[i11]+l[i12];

更多推荐

本文发布于:2023-07-28 19:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   长度为   如何在   strings   length

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!