Abstract
If we need to put the program code in word to submit a report or make a file, we may want to add the program code to the line number for ease of explanation, just as the blog garden shows the program code, what should we do?
Introduction
Environment: Visual C ++ 9.0/Visual Studio 2008
A small C ++ program can add a line number to the code and output the code.
Reference content is as follows:
Map_code_line.cpp/C ++
Copy codeThe Code is as follows:
/*
(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 () (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 result
Reference content is as follows:
Copy codeThe Code is as follows:
/*
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 () (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 rows
Reference content is as follows:
Copy codeThe Code is as follows:
While (getline (infile, line ))
Lines [line_num ++] = line;
Is the key of the entire program: Use map, key to store the line number, and value to store the code of each line. The row number is automatically added as each line of code is read.
37 rows
Reference content is as follows:
Copy codeThe Code is as follows:
For_each (lines. begin (), lines. end (), print_map ());
Print the map content. Because map cannot work with copy (), you have to leave and use for_each () and functor.
20 rows
Reference content is as follows:
Copy codeThe Code is as follows:
Struct print_map {
Void operator () (pair <int, string> p ){
Cout <p. first <"" <p. second <endl;
Outfile <p. first <"" <p. second <endl;
}
};
With for_each () functor, 22 Lines of cout can be removed, but only on the screen.
Conclusion
STL map is a very useful container, especially in substring writing. If there are no elements in the index, it will be automatically added, so lines [line_number ++] = line; this beautiful writing method.