(Original example) how to add a line feed to a program notebook? (C/C ++) (STL)

Source: Internet
Author: User

Abstract
If you need to put the program into a word report or file, maybe we want to add a line of code to the program for easy solution, what should we do as the blog shows program discovery?

Introduction
Use environment: Visual C ++ 9.0/Visual Studio 2008

A section of C ++ small program can be added to the program example and then exported.

Map_code_line.cpp/C ++

1 /*  
2 (C) oomusou 2008 Http://oomusou.cnblogs.com
3
4 Filename: map_code_line.cpp
5 Compiler: Visual C ++ 9.0/Visual Studio 2008
6 Description: Demo how to add line number for code
7 Release: 07/18/2008 1.0
8 */
9 # Include < Iostream >
10 # Include < Fstream >
11 # Include < String >
12 # Include < Map >
13 # Include < Algorithm >
14
15 Using   Namespace STD;
16
17 Ifstream infile ( " Map_code_line.cpp " );
18 Ofstream OUTFILE ( " Map_code_line_r.cpp " );
19
20 Struct Print_map {
21 Void   Operator () (Pair < Int , String > P ){
22 Cout < P. First <   "   "   < P. Second < Endl;
23 OUTFILE < P. First <   "   "   < P. Second < Endl;
24 }
25 };
26
27 Int Main (){
28 Map < Int , String > Lines;
29
30 String Line;
31 Int Line_num =   1 ;
32 While (Getline (infile, line ))
33 Lines [line_num ++ ] = Line;
34
35 Infile. Close ();
36
37 For_each (lines. Begin (), lines. End (), print_map ());
38 OUTFILE. Close ();
39 }

Results

1 /*
2 (C) oomusou 2008 Http://oomusou.cnblogs.com
3  
4 Filename: map_code_line.cpp
5 Compiler: Visual C ++ 9.0 /Visual Studio 2008
6 Description: Demo how to add line number for code
7 Release: 07 / 18 / 2008   1.0
8 */
9 # Include <iostream>
10 # Include <fstream>
11 # Include <string>
12 # Include <map>
13 # Include <algorithm>
14  
15 Using namespace std ;
16  
17 Ifstream infile ( " Map_code_line.cpp " ) ;
18 Ofstream OUTFILE ( " Map_code_line_r.cpp " ) ;
19
20 Struct print_map {
21 Void operator () (pair <int , String> P ){
22 Cout <p. First < "   " <P. Second <Endl ;
23 OUTFILE <p. First < "   " <P. Second <Endl ;
24 }
25 } ;
26  
27 Int main (){
28 Map <int , String> lines ;
29  
30 String line ;
31 Int line_num =   1 ;
32 While (Getline (infile , Line ))
33 Lines [ Line_num ++ ]   = Line ;
34  
35 Infile. Close () ;
36
37 For_each (lines. Begin () , Lines. End () , Print_map ()) ;
38 OUTFILE. Close () ;
39 }

32 rows

While (Getline (infile, line ))
Lines [line_num ++ ] = Line;

Is the concern of the entire program:Use Map, Key stores the rows, and value stores the program records of each row. In addition, each line of the program is automatically added.

37 rows

For_each (lines. Begin (), lines. End (), print_map ());

Print the map content. Because map cannot work with copy (), you have to leave the map and use for_each () and functor.

20 rows

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 it is only shown on the terminal screen.

Conclusion
STL map is a very useful container, especially the substring embedding method. If there are no elements in the index, it will be automatically added, so lines [line_number ++] = line; this beautiful method.

See also
(formerly known as "lead") Is there a more convenient way to export map workers to cout? (C/C ++) (STL)
(formerly called functions) How to Use Function objects? (C/C ++) (STL)
(formerly called) function pointer, delegate, and function object (C/C ++) (Template )(. net) (C #)
(formerly called forward) How to Use the forward circle (using for_each) correctly )? (C/C ++) (STL) (Template)

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.