婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > .net開發:為程式碼加上行號的方法詳解

.net開發:為程式碼加上行號的方法詳解

熱門標簽:手機地圖標注如何刪除 外呼系統代理品牌 怎么給超市做地圖標注入駐店 辦理400電話證件 巫師3為什么地圖標注的財寶沒有 寧波自動外呼系統代理 十堰正規電銷機器人系統 外呼系統費用一年 世紀佳緣地圖標注怎么去掉

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

Introduction

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

一段C++的小程式,可以幫程式碼加上行號后輸出。
以下為引用的內容:
map_code_line.cpp / C++

復制代碼 代碼如下:

         /*
         (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() (pairint, string> p) {
             cout    p.first " " p.second endl;
             outfile p.first " " p.second endl;
           }
         };
         int main() {
           mapint, 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();
         }

執行結果
以下為引用的內容:

復制代碼 代碼如下:


         /*
         (C) 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() (pairint, string> p) {
             cout    p.first " " p.second endl;
             outfile p.first " " p.second endl;
           }
         };
         int main() {
           mapint, 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行
以下為引用的內容:

復制代碼 代碼如下:

          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() (pairint, 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;這麼漂亮的寫法。

您可能感興趣的文章:
  • dhtmlxGrid 添加行號詳細步驟
  • DataGridView控件顯示行號的正確代碼及分析
  • FLEX 獲取DataGrid行號和列號示例代碼
  • python中使用sys模板和logging模塊獲取行號和函數名的方法
  • pycharm 使用心得(四)顯示行號
  • Python實現去除代碼前行號的方法
  • MyEclipse刪除網上復制下來的來代碼帶有的行號(正則去除行號)

標簽:山西 平涼 牡丹江 嘉興 泰州 景德鎮 天門 通遼

巨人網絡通訊聲明:本文標題《.net開發:為程式碼加上行號的方法詳解》,本文關鍵詞  .net,開發,為,程式,碼加,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《.net開發:為程式碼加上行號的方法詳解》相關的同類信息!
  • 本頁收集關于.net開發:為程式碼加上行號的方法詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 辉南县| 同德县| 太白县| 衡阳县| 太保市| 津市市| 玉林市| 岗巴县| 海伦市| 定兴县| 逊克县| 潢川县| 彰武县| 改则县| 七台河市| 乃东县| 乐业县| 阿克苏市| 连山| 天津市| 朝阳县| 中西区| 平乐县| 温泉县| 永川市| 漾濞| 稷山县| 昔阳县| 塔河县| 千阳县| 新化县| 根河市| 格尔木市| 石阡县| 桐城市| 黄陵县| 江阴市| 长葛市| 长白| 伊金霍洛旗| 东至县|