简体   繁体   English

逐字母存储字符串并打印

[英]storing a string letter by letter and printing it

Why won't this program print out "hello" in reverse? 为什么该程序不能反向打印“ hello”? It works when I uncomment the line inside the loop. 当我取消注释循环中的行时,它可以工作。 I wanna know the concept behind why the string value isn't getting stored. 我想知道为什么不存储字符串值的概念。 Thank you! 谢谢!

#include<iostream>

using namespace std;

void reverse(string str) {
        int length = str.length();
        int x = length, i = 0;
        string newString;

        while(x >= 0) {
                newString[i] = str[x-1];
                //cout << newString[i];
                x--;
                i++;
        }

        cout << newString;
}

int main() {
        reverse("hello");

return 0;
}

newString has a size of 0 (constructed with the default constructor), so writing past the end of it with newString[i] = ... causes undefined behavior. newString的大小为0(由默认构造函数构造),因此使用newString[i] = ...写入末尾会导致未定义的行为。 Use .resize to resize the string (make it big enough) before writing into it 在写入字符串之前,使用.resize调整字符串的大小(使其足够大)

There are several problems with the program. 该程序有几个问题。

For starters you should include the header <string> 对于初学者,您应该包含标题<string>

#include <string>

because the program uses declarations from this header. 因为程序使用此标头中的声明。 It is not necessary that the header <iostream> includes the header <string> 标头<iostream>不必包含标头<string>

It is much better to declare the function like 最好像这样声明函数

void reverse(const string &str);

Otherwise a copy of the original string used as argument is created each time when the function is called. 否则,每次调用该函数时,都会创建用作参数的原始字符串的副本。

For the size type the class std::string defines its own unsigned integer type named size_type . 对于大小类型,类std::string定义了其自己的名为size_type的无符号整数类型。 It is better to use it or the type specifier auto instead of the type int . 最好使用它,或者使用类型说明符auto代替int类型。

After this declaration 此声明后

string newString;

newString is empty. newString为空。 So you may not apply the subscript operator. 因此,您可能不应用下标运算符。 You should either resize the string or reserve enough memory for the new added elements to the string. 您应该调整字符串的大小或为新添加的元素保留足够的内存。

Taking this into account the function can be defined the following way. 考虑到这一点,可以通过以下方式定义功能。

#include <iostream>
#include <string>

using namespace std;

void reverse( const string &str) {
        auto length = str.length();
        string newString;
        newString.reserve( length );

        for ( auto i = length; i-- != 0;  ) newString += str[i];

        cout << newString << endl;
}

int main() {
        reverse("hello");

        return 0;
}

Take into account that the function could be defined simpler based on the features of the class std::string itself. 考虑到可以根据类std::string本身的功能更简单地定义函数。 For example 例如

#include <iostream>
#include <string>

using namespace std;

void reverse( const string &str) {
        string newString( str.rbegin(), str.rend() );

        cout << newString << endl;
}

int main() {
        reverse("hello");

        return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM