. NET development: How to add line numbers to code-practical tips

Source: Internet
Author: User

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;

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.