C++实现给定字符的字符串分割

10 篇文章 0 订阅
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>

using namespace std;
void readTxt(string file)
{
	ifstream infile;
	infile.open(file.data());   //将文件流对象与文件连接起来 
	assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行 

	char ch;
	std::vector<std::string> lines;
	std::string strLine;
	while (!infile.eof())
	{
		//infile >> c;
		infile.get(ch);
		if (ch == '\n' || ch == '\n\r') {
			lines.push_back(strLine);
			//cout << strLine << endl;
			strLine.clear();
			continue;
		}
		strLine.push_back(ch);
	}
	for(size_t i=0;i< lines.size(); i++)
	    cout << lines[i] << endl;
	infile.close();             //关闭文件输入流 
}

static std::vector<std::string> parseListFile(const std::string& fileName)
{
	std::vector<std::string> lines;
	std::ifstream infile(fileName);
	if (!infile)
	{
		return lines;
	}
	std::string line;
	while (getline(infile, line))
	{
		lines.push_back(line);
	}
	infile.close();
	return lines;
}
static bool isSeparator(const std::string& seps, char c)
{
	// seps.size() had been defined as unsigned int
	for (size_t i = 0; i < seps.size(); ++i)
	{
		if (seps[i] == c) return true;
	}
	return false;
}
static std::vector<std::string> split(const std::string& s, const std::string& seps)
{
	std::vector<std::string> ret;
	std::string::size_type i = 0;
	// invariant: we have processed characters [original value of i, i)
	while (i != s.size())
	{
		// ignore leading blanks
		// invariant: chartacters in range [original i, current i) are all spaces
		while (i != s.size() && isSeparator(seps, s[i]))
			++i;
		// find end of next word
		std::string::size_type j = i;
		// invariant: none of the characters in rang [original j, current j) is a space
		while (j != s.size() && !isSeparator(seps, s[j]))
			++j;
		// if we found some nonewithspace characters
		if (i != j)
		{
			// copy from s starting at i and taking j - i chars
			ret.push_back(s.substr(i, j - i));
			i = j;
		}
	}
	return ret;
}

static std::vector<std::vector<std::string>> parseTableFile(const std::string& fileName, const std::string& sep)
{
	std::vector<std::vector<std::string>> lines;
	std::ifstream infile(fileName);
	if (!infile)
	{
		return lines;
	}
	std::string line;
	while (getline(infile, line))
	{
		//cout << split(line, sep) << endl;
		lines.push_back(split(line, sep));
	}
	infile.close();
	return lines;
}
std::string int2str(int val)
{
	std::string str;
	std::stringstream ss;
	ss << val;
	return ss.str();
}
int main()
{
	string fileName = "a-mei//a-mei_0005.jpg 163.0751 76.10593 166.3308 173.72844";
	string sep = "mei";
	std::vector<std::string> ret;
	ret = split(fileName, sep);
	//cout << ret[1] << " " << ret[2] << " " << ret[3] << " " << endl;
	size_t index = 0;
	while (index != ret.size()){
		cout << ret[index] << endl;
		++index;
	}
	getchar();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值