我正在尝试使用while循环从字符串中删除字母,直到没有剩下的为止。我在这里做错了什么?

七宝
#include<iostream>
#include<string>
using namespace std;

int main() {
    string word;
    cout << "please enter a word: ";
    getline(cin, word);
    int str_size;
    word.size() = str_size;
// the line above says "word must be modifiable". new to coding so not sure how to fix it?
// also haven't tested anything below this point yet.
    while (str_size) {
        word.resize(str_size - 1, '\0');
    }

    string end;
    getline(cin, end);
    return 0;
    }

谁能解释我在这里做错了什么。我只是以错误的方式去做吗?

慈济

在这行上:

word.size() = str_size;

您正在尝试为由返回的临时值分配一个值.size()但是,在operator=左侧需要一个左值(即具有名称的变量)。

因此,您需要的是:

str_size = word.size();

要从字符串中实际删除字母,可以使用某些算法,例如std::remove_if这样就可以避免编写任何循环。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档