Abstract
If you need to put the code into Word to report or make a file, perhaps we would like to add code to facilitate the explanation of the line number, as the blog garden shows the code, how do we do?
Introduction
Working environment: Visual C + + 9.0/visual Studio 2008
A section of C + + program, can help code plus line number after the output.
The following are the referenced contents:
Map_code_line.cpp/c++
Copy Code code as follows:
/*
(C) Oomusou 2008
Filename:map_code_line.cpp
Compiler:visual C + + 9.0/visual Studio 2008
Description:demo and 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 () (Pair<int, string> p) {
cout << p.first << "" << p.second << Endl;
outfile << p.first << "" << p.second << Endl;
}
};
int main () {
Map<int, 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 ();
}
Execution results
The following are the referenced contents:
Copy Code code as follows:
/*
(C) Oomusou 2008 http://oomusou.cnblogs.com
Filename:map_code_line.cpp
Compiler:visual C + + 9.0/visual Studio 2008
Description:demo and 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 () (Pair<int, string> p) {
cout << p.first << "" << p.second << Endl;
outfile << p.first << "" << p.second << Endl;
}
};
int main () {
Map<int, 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 ();
}
32 lines
The following are the referenced contents:
Copy Code code as follows:
while (Getline (infile, line))
lines[line_num++] = line;
is the key to the entire program: use Map,key to store line numbers, and value to store the code for each line. And with each travel type code reading, automatically increase the line number.
37 lines
The following are the referenced contents:
Copy Code code as follows:
For_each (Lines.begin (), Lines.end (), Print_map ());
The map content was printed because the map could not match copy () and had to be followed by the use of For_each () and functor.
20 lines
The following are the referenced contents:
Copy Code code as follows:
struct Print_map {
void operator () (Pair<int, string> p) {
cout << p.first << "" << p.second << Endl;
outfile << p.first << "" << p.second << Endl;
}
};
The cout of the functor,22 line with For_each () can be taken off, just as the aspect is displayed on the screen.
Conclusion
STL map is very useful container, especially substring, if there is no element index, will automatically add, so there will be lines[line_number++] = line;