1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c++ split实现

Posted on 2013-04-06 16:18  1957  阅读(1806)  评论(0编辑  收藏  举报

之前看的谁出的面试题来着。不太记得了。

不过今天水微软的编程之美第一题的时候我用了split,所以就实现了一个。

当然可能还不满足那个面试题的要求,因为我只是用了istringstream而已。

先写在这儿吧,过两天再来做那个面试题。

 

std::vector<std::string>& split(const std::string& ori , char ch , std::vector<std::string>& ans)
{
	std::istringstream iss(ori);
	std::string item;
	while(std::getline(iss , item , ch)) ans.push_back(item);
	return ans;
}
std::vector<std::string> split(const std::string& ori , char ch)
{
	std::vector<std::string> ans;
	split(ori , ch , ans);
	return ans;
}