改标签文件内容顺序

#include"stdafx.h"
#include<fstream>  //ifstream
#include<iostream>
#include<string>     //包含getline()
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;
class Tosort {
public:
	string s;
	//double ctcloss;
	int Long;
	Tosort(string s1,int Long1) {
		s = s1;
	//	ctcloss = clc;
		Long = Long1;

	}
};
typedef std::vector<std::string>  StringList;
//为了快速排序
bool comp(Tosort a, Tosort b) {
	if (a.Long < b.Long) {
		return true;
	}
	else {
		return false;
	}
}

/*
分割字符串
*/
StringList splitstr(const std::string& str, char tag)
{
	StringList  li;
	std::string subStr;

	//遍历字符串,同时将i位置的字符放入到子串中,当遇到tag(需要切割的字符时)完成一次切割
	//遍历结束之后即可得到切割后的字符串数组
	for (size_t i = 0; i < str.length(); i++)
	{
		if (tag == str[i]) //完成一次切割
		{
			if (!subStr.empty())
			{
				li.push_back(subStr);
				subStr.clear();
			}
		}
		else //将i位置的字符放入子串
		{
			subStr.push_back(str[i]);
		}
	}

	if (!subStr.empty()) //剩余的子串作为最后的子字符串
	{
		li.push_back(subStr);
	}

	return li;
}

int main() {
	string s;//一行的所有东西
	ifstream inf;
	inf.open("C://Users//11865//Desktop//train.txt");          //特别注意,这里是://  是双斜杠喔~~     ifstream inf("d://out.txt");用这一句可以代替这两句喔,很简单有木有~~
														   //打开输出文件
	
	ofstream outf;
	string trainTxt = "C:/Users/11865/Desktop/train2.txt";
	outf.open(trainTxt, ios::out);
	//outf.open("C://Users//11865//Desktop//2.txt");
	vector<Tosort> FTosort;//存储所有的模型类
	while (getline(inf, s))      //getline(inf,s)是逐行读取inf中的文件信息
	{
		//outf << s << '\n';
		//cout << s << endl << endl;
		vector<string> test;
		test = splitstr(s, ' ');//文件名是test[0]
		string FileName = test[0];
		vector<string> test2= splitstr(FileName, '_');//名字里面有用的在test2[3];
		test2[3]= test2[3].substr(0, test2[3].length() - 4);
		string usename = test2[3];
		int Long = usename.length();//长度
		/*vector<string> FileName;
		FileName.push_back(test[0]);*/
		FTosort.push_back(Tosort(s,Long));
	}
	sort(FTosort.begin(), FTosort.end(), comp);
	for (int i = 0; i < FTosort.size(); i++) {
		cout << FTosort[i].s << endl;
		outf << FTosort[i].s << endl;
	}
	
	inf.close();
	outf.close();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值