如何為程式碼加上行號? (C/C++) (STL)

來源:互聯網
上載者:User

Abstract
若需要將程式碼放進word交報告或做檔案時,或許我們會想將程式碼加上行號方便講解,如同部落格園顯示程式碼那樣,我們該如何做呢?

Introduction
使用環境:Visual C++ 9.0 / Visual Studio 2008

一段C++的小程式,可以幫程式碼加上行號後輸出。

map_code_line.cpp / C++

1 /* 
2 (C) OOMusou 2008 http://oomusou.cnblogs.com

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 }

執行結果

1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com

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行

while(getline(infile, line))
  lines[line_num++] = line;

是整個程式的關鍵:使用map,key存允許存取號,value存放每一行的程式碼。而且隨著每一行程式碼的讀入,自動增加行號。

37行

for_each(lines.begin(), lines.end(), print_map());

將map內容印出,因為map無法配合copy(),只好退而求其次使用for_each()與functor。

20行

struct print_map {
  void operator() (pair<int, string> p) {
    cout    << p.first << " " << p.second << endl;
    outfile << p.first << " " << p.second << endl;
  }
};

配合for_each()的functor,22行的cout可以拿掉,只是方面在螢幕顯示而已。

Conclusion
STL的map是很好用的容器,尤其substring寫法,若index下沒有元素,會自動新增,所以才會有lines[line_number++] = line;這麼漂亮的寫法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.