【C++】基于“stringstream+getline”实现字符串分割(split)

哇...

最近在练习C++编程,遇到一个题目需要用到字符串分割(类似python的split函数)。C++并没有提供关于这个函数的功能,所以要自己实现。

就在此时,看到字符串流 stringstream 这个高级的类,功能非常强大,如数字与字符串之间的转换。

本文只介绍基于“stringstream+getline”实现字符串分割的功能,建议读者们去详细看看这个类。

http://www.cplusplus.com/reference/sstream/stringstream/


#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
	
	string str1 = "/d2/d4/f1";
	string str2 = "/d1/../../d2";
	string str3 = "hello world hcq!";
	string dir;
	string dir_list[50];
	int i=0;
	
	stringstream ss(str3);
	while(getline(ss, dir, ' ')){
		dir_list[i] = dir;
		cout<<dir_list[i]<<endl;
		i++;
	}
	
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值