zoukankan      html  css  js  c++  java
  • 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会事半功倍。

  • 相关阅读:
    leetcode 92. 反转链表 II
    leetcode记录1 快速排序
    Jmeter入门总结
    Jmeter 元件作用域、集合点、检查点
    Jmeter 实例
    badboy脚本开发
    Jmeter 常用功能介绍
    简单的自创线程池
    多线程
    IO多路复用
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3226190.html
Copyright © 2011-2022 走看看