刚刚要找个按空格分离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 << '\n'; – Tony D Apr 11 ' at :

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

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

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

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

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

std::string str = "abc def";
std::istringstream iss(str);
while (iss.good())
{
iss >> str;
std::cout << str << std::endl;
}

std::string的split函数的更多相关文章

  1. std::string 字符替换函数

    // 替换路径中所有“\”为“/” #include <algorithm> static std::string ConvertSlash(std::string& strUrl ...

  2. 利用c++ std::getline实现split

    getline reads characters from an input stream and places them into a string: getline从输入流中读取字符, 并把它们转 ...

  3. 转义字符\(在hive+shell以及java中注意事项):正则表达式的转义字符为双斜线,split函数解析也是正则

    转义字符 将后边字符转义,使特殊功能字符作为普通字符处理,或者普通字符转化为特殊功能字符. 各个语言中都用应用,如java.python.sql.hive.shell等等. 如sql中 "\ ...

  4. std::string在多字节字符集环境下substr的实现方法

    昨天写到<使用多字节字符集的跨平台(PC.Android.IOS.WP)编码/解码方法>中提到服务端使用std::string处理字符串,std::string对多字节字符集支持并不是很完 ...

  5. Java - split()函数和trim()函数的使用方法

    split()函数和trim()函数的使用方法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24465141 详细參考Java ...

  6. std::string类详解

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...

  7. [转]标准C++字符串string以及MFC6.0字符串CString的tokenize和split函数

    标准字符串的方法: /******************************************** the tokenize function for std::string ****** ...

  8. java String.split()函数的用法分析

    java String.split()函数的用法分析 栏目:Java基础 作者:admin 日期:2015-04-06 评论:0 点击: 3,195 次 在java.lang包中有String.spl ...

  9. Java——String.split()函数

    在java doc里有 String[] java.lang.String.split(String regex) Splits this string around matches of the g ...

随机推荐

  1. GIT本地配置和PUSH

    因为GIT使用的是LINUX命令,所以可以参考LINUX的相关命令 一.在本地配置好GIT仓库: 1.首先进入当前工程的目录:cd [filepath] (记住这里的目录应该用/,因为LINUX是这样 ...

  2. CSS line-height与vertical-align:baseline

    一.当line-height与vertical-align相遇,会发生很多匪夷所思的现象 首先,请看如下代码: <!DOCTYPE html> <html> <head& ...

  3. 每日学习笔记:js中可以直接用id名调用的问题?

    在JavaScript中,标准的id选择器调用语法是: document.getElementById('myid').style.width = pc + "%"; 但是,今天发 ...

  4. Error: Collection was modified; enumeration operation may not execute.

    http://blog.csdn.net/ffeiffei/article/details/6131254

  5. 2015.4.25-2015.5.1 字符串去重,比例圆设计,中奖机和canvas橡皮擦效果等

    1.字符串去重,html模板取值   2.javascript正则表达式之$1...$9   3.jquery插件   4.返回上一页并刷新 解决方法: <a href ="javas ...

  6. Javascript 俄罗斯方块 游戏代码解释!

    俄罗斯方块代码说明 /** 名称:Javascript 俄罗斯方块! 作者:Gloot 邮箱:glootz@gmail.com QQ:345268267 网站:http://www.cnblogs.c ...

  7. [译]git log进阶

    格式化log输出 oneline --oneline标记将每个commit压缩成一行. 默认情况下显示一个commit ID和commit描述的第一行. 输出如下: 0e25143 Merge bra ...

  8. 3、CCS样式表

    一.CCS样式表的分类(优先级从低到高): 1.浏览器默认样式表 2.外部样式表:在外部创建的.ccs文件中.使用外部样式表可以使样式应用于多个网页.通过这个方法只需改动一个文件就能改变整个网站的外观 ...

  9. 《征服 C 指针》摘录1:什么是空指针?区分 NULL、0 和 '\0'

    一.什么是空指针? 空指针 是一个特殊的指针值. 空指针 是指可以确保没有向任何一个对象的指针.通常使用宏定义 NULL 来表示空指针常量值. 空指针 确保它和任何非空指针进行比较都不会相等,因此经常 ...

  10. MySQL中find_in_set()和in的区别

    弄个测试表来说明两者的区别 CREATE TABLE `test` ( `id` int(8) NOT NULL auto_increment, `name` varchar(255) NOT NUL ...