C++ 字符串分割函数
平时刷 leetcode 、剑指 offer 等可能不会用到,但在找工作做笔试题的过程中还是会经常用到的,C++标准库里面没有字符分割函数split,这里做个总结。
方法1、利用 STL 实现
#include <iostream>
#include <vector>
#include <string>using namespace std;vector<string> split(const string &str, const string &pattern)
{vector<string> res;if ("" == str)return res;string strs = str + pattern;size_t pos = strs.find(pattern);size_t size = strs.size();while (pos != string::npos){string x = strs.substr(0, pos);res.push_back(x);//stoi(x)转整型strs = strs.substr(pos + 1, size);pos = strs.find(pattern);}return res;
}
/*
这样写也可以
void split(const string& s, vector<string>& v, const string& c)
{string::size_type pos1, pos2;pos2 = s.find(c);pos1 = 0;while(string::npos != pos2){v.push_back(s.substr(pos1, pos2-pos1));pos1 = pos2 + c.size();pos2 = s.find(c, pos1);}if(pos1 != s.length())v.push_back(s.substr(pos1));
}
*/
int main()
{string str="abc,def,ghi";vector<string> res=split(str,",");for(auto r:res)cout<<r<<endl;return 0;
}
方法2、利用 stringstream 来分割
将字符串绑定到输入流 istringstream,然后使用getline的第三个参数,自定义使用什么符号进行分割就可以了。
#include <iostream>
#include <vector>
#include <string>
#include <sstream>using namespace std;void split(const string &str,vector<string> &res,const char pattern)
{istringstream is(str);string temp;while(getline(is,temp,pattern))res.push_back(temp);//stoi(temp)转整型return ;
}int main()
{string str="abc,def,ghi";vector<string> res;split(str,res,',');for(auto r:res)cout<<r<<endl;return 0;
}
方法三 利用C语言中的 strtok 函数进行分割
strtok 函数包含在头文件<string.h>中,函数原型如下
char *strtok(char *str,const char *delim);
实现代码如下
#include <iostream>
#include <string.h>using namespace std;int main()
{//char str[]="a,b,c,d";//如果输入是string要转成char数组[strcpy(buf,str.c_str())]//或者用const_cast转换成char指针string str="a,b,c,d";const char *delim=",";char *p=strtok(const_cast<char *>(str.c_str()),delim);while(p){cout<<p<<endl;p=strtok(NULL,delim);}return 0;
}
参考文章
1、https://www.cnblogs.com/dingxiaoqiang/p/8228390.html
2、https://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html
C++ 字符串分割函数相关推荐
- Split字符串分割函数
非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg"Dim arrTest arrT ...
- strtok(), strtok_s() 字符串分割函数
strtok(), strtok_s() 字符串分割函数 //strtok()函数原型/*_Check_return_ _CRT_INSECURE_DEPRECATE(strtok_s) _CRTIM ...
- C++的字符串分割函数
转载:http://blog.csdn.net/shanzhizi/article/details/17189267 C++的字符串没有分割函数,因此需要自己写方便使用.而受到开发工具的影响,有很多用 ...
- php 字符串分割出数字,php 字符串分割函数的总结
在php中字符串分割函数有很多很多,下面我来给各位朋友来介绍各种php中字符串分割实例,有需要的朋友可进入参考参考. PHP函数split()的基本语法为 array split ( string $ ...
- 小猿圈学习Python字符串操作--字符串分割函数
对于长字符串,我们一般要怎么处理呢?除了切片,但是切片太麻烦了.python为我们提供了字符串切割函数,它不仅可以通过指定字符切割,也可以去除空格符哦.下面就看一下小猿圈Python讲师是怎么介绍Py ...
- 探索C语言之字符串分割函数:strtok和strsep的区别
探索C语言之字符串分割函数:strtok和strsep的区别 概述 strsep - extract token from string(linux 下) strtok, strtok_r - ext ...
- php字符串分隔函数,php字符串分割函数的简单示例
这篇文章主要为大家详细介绍了php字符串分割函数的简单示例,具有一定的参考价值,可以用来参考一下. 对php字符串分割函数感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! php中 exp ...
- php中的分割字符串函数,关于PHP中两款字符串分割函数explode,str_split
关于PHP中两款字符串分割函数explode,str_split 2019.02.19 4140 将一个字符串分割成数组在日常的微信公众号开发中的应用应该是很多的.如果指定分割符,可以使用explod ...
- c语言中字符切割函数split,c 语言 指针 C语言字符串分割函数split实现
c语言也有类似其他语言的split字符串分割函数,就是strtok使用方法如下://用逗号做分隔符,读出内容//char s[] = "123-sldkf-123ls-343434-dfjd ...
- c语言将字符串按空格分割_如何在c++中实现字符串分割函数split详解
前言 在学习c++中string相关基本用法的时候,发现了sstream的istringstream[1]可以将字符串类似于控制台的方式进行输入,而实质上这个行为等同于利用空格将一个字符串进行了分割, ...
最新文章
- Handler消息机制(一):Linux的epoll机制
- C#.Net网页加载等待效果漂亮并且简单
- 超火GitHub项目!一夜获得5000星,竟是微软开源的计算器
- node中的缓存机制
- 数学建模 线性规划模型基本原理
- BugkuCTF-MISC题啊哒,白哥的鸽子
- 设置虚拟机上的redis可以被windows的环境下的python访问连接
- java线程池1001java线程池_深入浅出Java(Android )线程池ThreadPoolExecutor
- [转]把复杂事物简明化
- 杭电 4907 Task schedule ·
- JAVA梅森旋转随机算法,C语言实现梅森旋转算法
- 隐藏元素 mayaa
- 二维特征分类的基础_3D 分割分类总结
- 神剧《切尔诺贝利》引发的技术思考:如何避免下一次核灾难?
- CAD二次开发获取已选择实体
- eact native生成APP报错:You have not accepted the license agreements of the following SDK components:...
- HTML+CSS笔记5
- 淘宝店小蜜配置手册——店小蜜的配置框架
- 第十三章 Java 文件与流
- PCB加logo图标——winTopo转换矢量图