为什么我的字符串在此代码中被修改

Why is my string getting modified inside this code?

本文关键字:修改 代码 我的 字符串 为什么      更新时间:2023-10-16

我正在将一个整数作为字符串 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