简体   繁体   English

C++ 不打印字符串

[英]String not being printed by C++

Sorry for the noob question, I'm a newbie programmer and transitioning from C to C++.对于菜鸟问题​​很抱歉,我是一个新手程序员,正在从 C 过渡到 C++。 I could easily write a program to reverse a string in C the same way with minor changes but writing this in C++, why does this not print anything:我可以很容易地编写一个程序来以相同的方式在 C 中反转字符串,只需稍作更改,但在 C++ 中编写它,为什么这不打印任何内容:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string s,p;
    getline(cin,s);
    int j=0,i = 0;
    while(s[i]!='\0'){
        i++;
    }
    i--;
    while(i!=-1){
        p[j] = s[i];
        j++;
        i--;
    }
    cout << p << endl;
    return 0;
}

if i replace the p with say p[2], it correctly prints out the reverse 3rd character of the original string, but i cant find a way to print the whole string.如果我用 p[2] 替换 p,它会正确打印出原始字符串的反向第三个字符,但我找不到打印整个字符串的方法。

    std::string str{"reverse me"};
    std::string rev{str.rbegin(), str.rend()};
    //or when you are not interested in the orignal string
    std::reverse(str.begin(), str.end());

Giving the constructur of the reverse string the reversed iterators of your input string gives you the string in reversed order.为反向字符串的构造函数提供输入字符串的反向迭代器,可以以相反的顺序为您提供字符串。

To fix your string reverse code you just have to resize the string object p :要修复您的字符串反向代码,您只需resize字符串对象p resize


int main(){
    std::string s = "hello",
           p;
    p.resize(s.size()); // this was causing your problems, p thought it was size 0

    for (int i = s.size() - 1, j = 0; i >= 0; i--, j++)
    {
        p[j] = s[i];
    }

    std::cout << p << std::endl;
    return 0;
}

In addition to this, there is no need to find \\0 in the string, while it will be there, you can just ask std::string what its size() is.除此之外,不需要在字符串中找到\\0 ,虽然它会在那里,你可以只问std::string它的size()是什么。

On a side note, while std::string probably allocates some memory by default, just assuming it has enough to store whatever you input is going to be undefined behaviour.附带说明一下,虽然std::string可能默认分配一些内存,但假设它有足够的空间来存储您输入的任何内容,这将是未定义的行为。

While there are ways to iterate over a std::string and fill the contents of another, the overload of std::basic_string::operator= will replace the content of p (if any) with the content of s in a simple assignment.虽然有多种方法可以迭代std::string并填充另一个的内容,但std::basic_string::operator=的重载将在简单的赋值中用s的内容替换p的内容(如果有)。 See std::basic_string::operator=std::basic_string::operator=

For example:例如:

#include <iostream>
#include <string>

int main (void) {

    std::string s {}, p {};

    std::cout << "enter string: ";
    if (getline (std::cin, s)) {
        p = s;  /* simple assignment replaced content of p with content of s */
        std::cout << "string in p : " << p << '\n';
    }
}

Example Use/Output示例使用/输出

$ ./bin/stringps
enter string: the string s
string in p : the string s

string p; doesn't have enough allocated space for directly accessing by something like p[j]没有足够的分配空间供p[j]类的东西直接访问

You can change to initialize p from copying s like below, your code will work.您可以更改为通过复制 s 来初始化 p ,如下所示,您的代码将起作用。

string s;
getline(cin,s);
string p(s);  // p will be allocated and be the same as s

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

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