我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
I'm trying to use a while loop to remove letters from a string until there are none left. What am I doing wrong here?
#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, ' ');
}
string end;
getline(cin, end);
return 0;
}
谁能解释我在这里做错了什么。我只是走错了路吗?
在这一行:
word.size() = str_size;
您正在尝试为.size()
返回的临时值赋值。但是,operator=
需要在左侧有一个左值(即具有名称的变量(。
所以你需要的是:
str_size = word.size();
要实际从字符串中删除字母,您可以使用一些算法,例如std::remove_if
来避免编写任何循环。
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?