码迷,mamicode.com
首页 > 编程语言 > 详细

split 实现(c++ string)

时间:2015-09-08 23:47:40      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <vector>

size_t split(std::string &src, std::vector<std::string> *tokens, std::string sep)
{
	size_t last= 0;
	size_t index = src.find(sep, last);
	size_t length = src.size();
	while(index != std::string::npos)
	{
		tokens->push_back(src.substr(last, index-last));
		last = index + 1;
		index = src.find(sep, last);
	}
	if(length - last > 0)
	{
		tokens->push_back(src.substr(last, length-last));
	}
	return tokens->size();
}
int main(int argc, char* argv[])
{
	
	std::string src = "QWEQWE";
	std::string sep = "W";
	std::vector<std::string> tokens;
	std::cout << split(src, &tokens, sep) << std::endl;
	for(std::vector<std::string>::iterator iter = tokens.begin(); iter != tokens.end(); iter++)
	{
		std::cout << *iter->c_str() << std::endl;
	}
	return 0;
}


split 实现(c++ string)

标签:

原文地址:http://my.oschina.net/mjRao/blog/503543

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!