如何从字符串中删除第一个单词? [英] How to remove first word from a string?

查看:160
本文介绍了如何从字符串中删除第一个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找从 std :: string 中删除​​第一个单词的最佳方法。这是我有,但我觉得这是过于复杂的事情。什么是最好和最短的方法来做到这一点?感谢。

I'm looking for the best way to remove the first word from a std::string. This is what I have but I feel that this is overcompilicating things. What's the best and shortest way to do this? Thanks.

#include <string>
#include <iostream>
#include <sstream>

int main()
{
    std::string str{"Where is everybody?"};
    std::string first;
    if (std::stringstream{str} >> first)
    {
        str.erase(str.begin(), str.begin() + first.size());
    }
    std::cout << str; // " is everybody?"
}


推荐答案

下半部分的流:)

#include <string>
#include <iostream>
#include <sstream>

int main()
{
    std::string str{"Where is everybody?"};

    std::string first;
    std::istringstream iss{str};
    iss >> first;

    std::ostringstream oss;
    oss << iss.rdbuf();

    std::cout << oss.str(); // " is everybody?"
}

这篇关于如何从字符串中删除第一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆