• std::string的split函数


    刚刚要找个按空格分离std::string的函数, 结果发现了stackoverflow上的这个问题.

    也没仔细看, 直接拿来一试, 靠, 不对啊, 怎么分离后多出个空字符串, 也就是 "abc def" 分离后, 得到的是:

       "abc"

       "def"

       ""

    这不科学! 老外在耍我么, 再看原来的回答下面已经有人commet了:

    while (iss) { string subs; iss >> subs; cout << "Substring: " << sub << endl; }

    满心欢喜的再一试, 靠, 还是不对啊! 老外也太不靠谱了, 再看下面的comment, 乐了:

     @Eduardo: that's wrong too... you need to test iss between trying to stream another value and using that value,
     i.e. string sub; while (iss >> sub) cout << "Substring: " << sub <<
    ' '; – Tony D Apr 11 '12 at 2:24

    感觉老外有时候也是会激动, 手抖的, 哈哈.

    拿这位仁兄的代码再试, OK了.

    不过这些老外的代码感觉还是不太规范的, 比如在判断iss是否可用的时候, 是直接判断的, 跟进去的话, 可以看到这里其实是调用了这么个函数:

    __CLR_OR_THIS_CALL operator void *() const
    {
        // test if any stream operation has failed
        return (fail() ? 0 : (void *)this);
    }

    我在文档里没有找到这个的说明, 所以最后我还是用了比较规范的方式:

    std::string str = "abc def";
    std::istringstream iss(str);
    while (iss.good())
    {
        iss >> str;
        std::cout << str << std::endl;
    }
  • 相关阅读:
    如何将cordova导入Android studio,只需两步即可
    Cordova 教程 学习步骤-从零基础开始
    特效插件
    jq 命名空间
    input聚焦后光标移动至末尾
    时间常用api
    jq 便捷api jq 常用 api jq 快捷 api
    键盘事件
    创建爬网规则
    SharePoint 2013 本地开发解决方案以及程调试(真的可以)
  • 原文地址:https://www.cnblogs.com/chaoswong/p/3457849.html
Copyright © 2020-2023  润新知