为什么我的字符串在此代码中被修改
Why is my string getting modified inside this code?
我正在将一个整数作为字符串 s 的字符串中的输入。我想以"-"和"|"的形式在 s 中写入整数。我确信我的逻辑是正确的。问题是字符串 s 在代码中自动修改。当我最初打印字符串 s 时,它返回完整的 12345(我的输入是"2 12345"),但是当我之后尝试打印它时,它被截断了或其他东西。我该如何解决此问题?
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
std::string s;
cin >> n;
cin >> s;
cout << s.at(3) <<endl;
while(n!=0){
for (int l=0;l<3+2*n;l++){
// for (int i=0;i<s.length();i++){
if (l==0){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='4'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='0'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='5'||s.at(j)=='6'||s.at(j)=='7'||s.at(j)=='8'||s.at(j)=='9'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if (l==n+1){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='7'||s.at(j)=='0'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='4'||s.at(j)=='5'||s.at(j)=='6'||s.at(j)=='8'||s.at(j)=='9'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if (l==2*n+2){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='4'||s.at(j)=='7'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='0'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='6'||s.at(j)=='8'||s.at(j)=='9'||s.at(j)=='5'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if ((l>0) && (l<n+1)){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='7'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)='4'||s.at(j)=='8'||s.at(j)=='9'||s.at(j)=='0') {
cout << "|";
cout << "s "<< s<< endl;
cout << "check 2";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)=='5'||s.at(j)=='6'){
cout << "|";
cout << "check";
for (int k=0;k<n;k++){
cout <<" ";
}
cout << " ";
}
cout << " ";
}
}
else if ((l>n+1) && (l<2*n+2)){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='3'||s.at(j)=='5'||s.at(j)=='7'||s.at(j)=='9'||s.at(j)=='4'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)='6'||s.at(j)=='8'||s.at(j)=='0') {
cout << "|";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)=='2'){
cout << "|";
for (int k=0;k<n;k++){
cout <<" ";
}
cout << " ";
}
cout << " ";
}
}
cout << s << endl;
cout << endl;
}
cin >> n;
cin >> s;
}
}
else if(s.at(j)='4'||
您忘记了一个 = 符号。
快速查看会发现s.at(j)='6'
和s.at(j)='4'
修改字符串。您可以使用尤达条件来避免这种情况。
这里有编译器可以警告你一个作业。(见评论)。
另请参阅Mats Petersson的回答,我认为这是最好的答案:如果您不想在特定情况下更改内容,请使用const对象,引用或指针来const内存。
如果你使用一个函数来std::string const &s
它的参数,做这项工作,你最终会有
- 更清晰的代码结构和
- 更改参数时编译器错误
s
.
如果要避免无意中修改变量(例如字符串),则可以将相关代码分解为函数并将const string& x
传递给函数,也可以创建局部 const 引用。在您的代码中,可以执行以下操作:
std::string ms;
cin >> n;
cin >> ms;
const &std::string s = ms;
现在,每当您尝试更改s
时,您都会收到错误,因为它是一个const
。
相关文章:
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 修改 VS Code 中的默认C++代码段
- 如何修改代码以打印出数组 v2
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 代码错误修改
- 我在代码中收到一个运行时错误,该错误如何通过修改最多一个元素来查找数组是否可以变得不减少
- 如何跟踪通过外部代码修改类变量的所有位置
- "check if member exists using enable_if"中修改的代码不起作用
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 张量流内核代码修改
- 代码审查:常量字符 * 修改
- 如果我制作一个代码,每个线程都会修改数组的完全不同的部分,则将保持缓存相干性
- 如何查看修改后的代码,其中对内联函数进行了预处理
- 是否可以有一个零成本assert(),这样代码就不必在调试和发布版本之间进行修改
- Matlab 编码器错误 "for loop index expressions.." 如何修改代码?
- 如何修改CUDA代码以获得100%的GPU负载
- 自修改代码vs编译器优化vs重复代码
- 创建一个txt文件来调用f2(),而不修改代码
- 通过GUI修改代码
- 如何从用户输入修改c++代码