c++中提取和排序

来源:百度知道 编辑:UC知道 时间:2024/04/28 08:55:29
我要做一个data.txt的文件中提取和排序,比如文档内容是:
011:112:I am doing the home work!
010:222:thank you for your help!
022:121:today is good day!
011:099:I need some one can help me, please!
022:002:you are a good man!
将上面的文档的冒号前面的数字进行排序,然后再屏幕上显示出来的是:
010:222:thank you for your help!
011:099:I need some one can help me, please!
011:112:I am doing the home work!
022:002:you are a good man!
022:121:today is good day!

比较简单的,还是做了

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<algorithm>
using namespace std;

typedef struct tagMyData {
string strHeader1;
string strHeader2;
string strLine;
} MyData;

bool ReadInputFile(string strFileName, vector<MyData> &aDataArr)
{
string strLine, strHeader1, strHeader2;
ifstream infile(strFileName.c_str());
if (!infile) return false;

while (getline(infile, strLine)) {
// Ignore the empty line OR the first char of this line is #
if (strLine.length() == 0 || strLine.at(0) == '#') continue;

string key (":");
size_t found1, found2;

found1 = strLine.find(key);
if (found1 != string::npos) {
strHeader1 = strLine.substr(0, found1);
string strTmp = strLine;

found2 =