/*
(C) OOMusou 2008
Filename : map_code_line.cpp
Compiler : Visual C++ 9.0 / Visual Studio 2008
Description : Demo how to add line number for code
Release : 07/18/2008 1.0
*/
#include iostream>
#include fstream>
#include string>
#include map>
#include algorithm>
using namespace std;
ifstream infile("map_code_line.cpp");
ofstream outfile("map_code_line_r.cpp");
struct print_map {
void operator() (pairint, string> p) {
cout p.first " " p.second endl;
outfile p.first " " p.second endl;
}
};
int main() {
mapint, string> lines;
string line;
int line_num = 1;
while(getline(infile, line))
lines[line_num++] = line;
infile.close();
for_each(lines.begin(), lines.end(), print_map());
outfile.close();
}
/*
(C) OOMusou 2008 http://oomusou.cnblogs.com
Filename : map_code_line.cpp
Compiler : Visual C++ 9.0 / Visual Studio 2008
Description : Demo how to add line number for code
Release : 07/18/2008 1.0
*/
#include iostream>
#include fstream>
#include string>
#include map>
#include algorithm>
using namespace std;
ifstream infile("map_code_line.cpp");
ofstream outfile("map_code_line_r.cpp");
struct print_map {
void operator() (pairint, string> p) {
cout p.first " " p.second endl;
outfile p.first " " p.second endl;
}
};
int main() {
mapint, string> lines;
string line;
int line_num = 1;
while(getline(infile, line))
lines[line_num++] = line;
infile.close();
for_each(lines.begin(), lines.end(), print_map());
outfile.close();
}
for_each(lines.begin(), lines.end(), print_map());
STL的map是很好用的容器,尤其substring写法,若index下没有元素,会自动新增,所以才会有lines[line_number++] = line;这麽漂亮的写法。