【发布时间】:2021-02-22 01:57:23
【问题描述】:
我必须处理来自std::remove 的 [[nodiscard]] 警告;
static_cast<void>(std::remove(stringVar.begin(), stringVar.end(), ' '));
我想要正确的方法。警告可以通过以下代码停止:
auto temp = std::remove(stringVar.begin(), stringVar.end(), ' ');
我不想使用std::remove的返回值。
void main()
{
std::string stringVar { "Operation : In , Value : 3884 ," };
size_t from = 0, to = 0, pos = 0;
std::string delFrom{ ":" }, delTo{ "," };
static_cast<void>(std::remove(stringVar.begin(), stringVar.end(), ' '));
from = stringVar.find(delFrom, pos);
to = stringVar.find(delTo, pos);
std::cout<< stringVar.substr(from + 1, to - from - 1);
}
输出:
In
这是一个对 SO 上已搜索的问题不感兴趣的特定问题。
更新:数据一致且可读的格式。
【问题讨论】:
-
这里的问题是,如果你不使用
temp,你就没有正确地从你的字符串中删除空格。 -
如果您收到有关丢弃返回值的警告,这应该被解释为您的代码有问题的迹象,不是您应该找到一种方法使警告静音。
-
@john 我再次在 stringVar 上搜索第一次出现的分隔符。所以,我对返回迭代器指向的垃圾不感兴趣
-
这类似于“我的汽车的检查引擎灯亮着,我该如何取下灯泡?”
-
请注意,输入如
" ",允许输出为“hello”。std::partition似乎更合适。
标签: c++ c++17 compiler-warnings nodiscard