C++读写文件并排序

比如一条记录是
1987 9 2
1988 8 26
代表公司员工生日
然后需要读入到系统
现在需要放入容器,并且排序

最后输出到新的文件中,按照年龄由大到小。

 

#include "stdafx.h"

#include <fstream>

#include <iostream>

#include<vector>

#include<string>

#include<algorithm>

using namespace std;



int _tmain(int argc, _TCHAR* argv[])

{

	string ifilename = "birthday.txt",s;

	string ofilename = "orderedBirth.txt";

	vector <string> birth ;

	ifstream infile(ifilename.c_str());	

	ofstream outfile(ofilename.c_str());	

	if(!infile||!outfile)

		return -1;

	while(getline(infile,s))

	//while (infile >> s)

	{

		birth.push_back(s);

		//outfile << s <<endl;

	}

	sort(birth.begin(),birth.end());

	for(vector<string>::iterator iter = birth.begin();iter != birth.end(); ++iter)

	{	

		outfile << *iter <<endl;

	}

	infile.close();

	infile.clear();

	outfile.close();

	outfile.clear();

	//getchar();

	return 0;

}


知识点:

 

1.熟悉文件的读写操作。

2.运用CSTL会事半功倍。

 

你可能感兴趣的:(C++)