. Net development: detailed explanation of how to add a line number to the code

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.