|
发表于 2013-7-30 15:35:46
|
显示全部楼层
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <iterator>
- using namespace std;
- struct STU{
- int num;
- string name;
- };
- map<int,string> replace_map;
- void init_map() {
- //在这里添加要替换的学号和姓名
- //可以添加多个
- replace_map[456]="赵六";
- replace_map[234]="曹操";
- }
- istream& operator>>( istream& in, STU& stu ) {
- in>>stu.num;
- if( replace_map.find(stu.num) != replace_map.end() )
- stu.name = replace_map[stu.num];
- else in>>stu.name;
- return in;
- }
-
- ostream& operator<<( ostream& out, const STU& stu ) {
- return out<<stu.num<<"\t"<<stu.name;
- }
-
- int main() {
- typedef istream_iterator<STU> ISIT;
- typedef ostream_iterator<STU> OSIT;
- init_map();
- vector<STU> vec;
- ifstream in("text.txt");
- copy( ISIT(in), ISIT(), back_inserter(vec) );
- in.close();
- ofstream out("text.txt");
- copy( vec.begin(), vec.end(), OSIT(out,"\n") );
- }
复制代码 |
|