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会事半功倍。

  • 相关阅读:
    DNS域名解析抓包分析
    Redis实现分布式锁
    Redis内存回收淘汰策略
    Redis缓存雪崩、击穿、穿透
    Redis内存碎片
    C++ 友元
    C++ const
    C++构造函数与析构函数调用虚函数
    C++类成员变量的初始化顺序
    C++ 类对象和类指针
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3226190.html
Copyright © 2011-2022 走看看