在C++中,我们有时候需要拆分字符串,比如字符串string str = "dog cat cat dog"想以空格区分拆成四个单词,Java中实在太方便了,直接String[] v = str.split(" ");就搞定了,而c++中没有这么方便的实现,但也有很多的方法能实现这个功能,下面列出五种常用的实现的方法,请根据需要选择,个人觉得前三种使用起来比较方便,参见代码如下:

    #include <vector>
#include <iostream>
#include <string>
#include <sstream> string str = "dog cat cat dog";
istringstream in(str);
vector<string> v; // Method 1
string t;
while (in >> t) {
v.push_back(t);
}

// Method 2
// #include <iterator>
copy(istream_iterator<string>(in), istream_iterator<string>(), back_inserter(v));

// Method 3
string t;
while (getline(in, t, ' ')) {
v.push_back(t);
}

// Method 4
string str2 = str;
while (str2.find(" ") != string::npos) {
int found = str2.find(" ");
v.push_back(str2.substr(, found));
str2 = str2.substr(found + );
}
v.push_back(str2);

// Method 5
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
char *dup = strdup(str.c_str());
char *token = strtok(dup, " ");
while (token != NULL) {
v.push_back(string(token));
token = strtok(NULL, " ");
}
free(dup);

C++ Split string into vector<string> by space的更多相关文章

  1. C++ Split string into vector<string> by space(转)

    c++中没有这么方便的实现,但也有很多的方法能实现这个功能,下面列出五种常用的实现的方法,请根据需要选择,个人觉得前三种使用起来比较方便,参见代码如下: #include <vector> ...

  2. 编写程序,将来自文件中的行保存在一个vector<string>,然后使用一个istringstream 从vector中读取数据,每次读一个单词

    #include<fstream> #include <vector> #include<string> #include<iostream> #inc ...

  3. 谈谈两种标准库类型---string和vector

    两种最重要的标准库---string和vector string和vector是两种最重要的标准库类型,string表示可变长的字符序列,vector存放的是某种给定类型对象的可变长序列. 一.标准库 ...

  4. C# String.split()用法小结。String.Split 方法 (String[], StringSplitOptions)

    split()首先是一个分隔符,它会把字符串按照split(' 字符')里的字符把字符串分割成数组,然后存给一个数组对象. 输出数组对象经常使用foreach或者for循环. 第一种方法 string ...

  5. string和vector

    一.String对象 1.string s;      s.size(); //返回的是s中字符的个数,也是s的长度: //string对象最后没有加空字符 //size()返回的是string::s ...

  6. 单独删除std::vector <std::vector<string> > 的所有元素

    下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...

  7. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  8. 编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个对立的元素存于vector中

    #include<iostream> #include<string> #include<vector> #include<fstream> using ...

  9. vector(char*)和vector(string)

    vector<char*> ch; vector<string> str; for(int i=0;i<5;i++) { char *c=fun1();//通过这个语句产 ...

随机推荐

  1. maven An error occurred while filtering resources

    转自:http://stackoverflow.com/questions/18145774/eclipse-an-error-occurred-while-filtering-resources m ...

  2. Linux shell脚本编程基础之练习篇

    shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...

  3. iOS xib中TableView创建的2种模式

    在xcode 5.0中 用xib编辑tableview有2种模式,见下图 其中,dynamic prototype 动态原型 表示tableview会询问它指定的 data source获取数据,如果 ...

  4. 数据库路由器 ICX

    实时并发数据库事务处理同步复制器和负载平衡器    ———通向真正数据库高可用性,高可靠性,高性能之路 一.产品概述    数据库路由器--ICX是美国宾夕法尼亚大学计算机系施教授经过多年研究.开发出 ...

  5. Greedy:Saruman's Army(POJ 3069)

    2015-09-06 萨鲁曼军队 问题大意:萨鲁曼白想要让他的军队从sengard到Helm’s Deep,为了跟踪他的军队,他在军队中放置了魔法石(军队是一条线),魔法石可以看到前后距离为R的距离, ...

  6. Codeforces 424C(异或)

    Magic Formulas Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Subm ...

  7. [Android Pro] 监听WIFI 打开广播

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-perm ...

  8. ubuntu用户添加adduser, useradd

    ubuntu和windows一样,可以任意创建或者删除新的用户,windows下比较简单,ubuntu下需要使用命令,不过操作起来不是很繁琐,所以我尽量写的详细一些.  如何创建ubuntu新用户? ...

  9. 查看TOMCAT的版本

    [root@Apps bin]# sh version.sh Using CATALINA_BASE: /apps/api-tomcat Using CATALINA_HOME: /apps/api- ...

  10. Javascript模块化编程之路——(require.js)

    转自:http://www.ruanyifeng.com/blog/2012/10/javascript_module.html Javascript模块化编程(一):模块的写法 随着网站逐渐变成&q ...