gpt4 book ai didi

c++ - 如何从字符串中删除第一个单词?

转载 作者:太空狗 更新时间:2023-10-29 19:52:24 24 4
gpt4 key购买 nike

假设我有

string sentence{"Hello how are you."}

我希望字符串句子中有“你好吗”而不是“你好”。我该怎么做。

我试过做类似的事情:

stringstream ss(sentence);
ss>> string junkWord;//to get rid of first word

但是当我这样做的时候:

cout<<sentence;//still prints out "Hello how are you"

很明显 stringstream 不会改变实际的字符串。我也尝试过使用 strtok 但它不能很好地与 string 一起使用。

最佳答案

尝试以下操作

#include <iostream>
#include <string>

int main()
{
std::string sentence{"Hello how are you."};

std::string::size_type n = 0;
n = sentence.find_first_not_of( " \t", n );
n = sentence.find_first_of( " \t", n );
sentence.erase( 0, sentence.find_first_not_of( " \t", n ) );

std::cout << '\"' << sentence << "\"\n";

return 0;
}

输出是

"how are you."

关于c++ - 如何从字符串中删除第一个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com