C++之字符串分割函数split

简介: c++之字符串分割: 1 /* 2 *c++之字符串分割: 3 */ 4 5 #include 6 #include 7 #include 8 9 void split(const std::string& s, const std...

 c++之字符串分割:

 1 /*
 2   *c++之字符串分割:
 3   */
 4     
 5 #include <iostream>
 6 #include <string>
 7 #include <vector>
 8 
 9 void split(const std::string& s, const std::string& delim,std::vector< std::string >& ret)
10 {               
11     size_t last = 0;
12     size_t index=s.find_first_of(delim,last);
13     while (index!=std::string::npos) {
14         ret.push_back(s.substr(last,index-last));
15         last=index+1;
16         index=s.find_first_of(delim,last);
17     }   
18     if (index-last>0) {
19         ret.push_back(s.substr(last,index-last));
20     }   
21 }  
22 
23 //取vector的最后一个元素:
24 std::string tmp = str_arr[str_arr.size()-1];
25 
26 int main()
27 {
28     std::string str = "test/jjksdf";
29     if(str.find("/") != std::string::npos){
30         std::vector<std::string> svec;
31         split(str, "/", svec);
32         std::cout << "first:" << svec[0] << " second: "<< svec[1] << std::endl;
33     }
34     std::cout << "src string: " << str << std::endl;
35 
36     return 0;
37 }

 

相关文章
|
22小时前
|
C++ 编译器 程序员
C++ 从零基础到入门(3)—— 函数基础知识
C++ 从零基础到入门(3)—— 函数基础知识
|
22小时前
|
自然语言处理 编译器 C语言
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
本文章是我对C++学习的开始,很荣幸与大家一同进步。 首先我先介绍一下C++,C++是上个世纪为了解决软件危机所创立 的一项面向对象的编程语言(OOP思想)。
34 1
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
|
22小时前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
15 1
|
22小时前
|
存储 算法 数据安全/隐私保护
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
7 0
|
22小时前
|
存储 自然语言处理 C++
刷题用到的非常有用的函数c++(持续更新)
刷题用到的非常有用的函数c++(持续更新)
18 1
|
22小时前
|
存储 编译器 C++
【C++】内存管理和模板基础(new、delete、类及函数模板)
【C++】内存管理和模板基础(new、delete、类及函数模板)
22 1
|
22小时前
|
存储 C++
c/c++宏定义(函数)
c/c++宏定义(函数)
|
22小时前
|
编解码 JavaScript 前端开发
【专栏】介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例
【4月更文挑战第29天】本文介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例。Base64编码将24位二进制数据转换为32位可打印字符,用“=”作填充。文中展示了各语言的编码解码代码,帮助开发者理解并应用于实际项目。
|
22小时前
|
编译器 C++
【C++进阶】引用 & 函数提高
【C++进阶】引用 & 函数提高
|
22小时前
|
存储 编译器 C语言
C++字符串大小写之for语句
C++字符串大小写之for语句
18 0