新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 如何為程序代碼加上行號

        如何為程序代碼加上行號

        作者: 時間:2011-05-12 來源:網絡 收藏

        若需要將放進詞交報告或做文件時,或許我們會想將方便講解,如同博客園顯示那樣,我們該做呢?

        本文引用地址:http://www.104case.com/article/150758.htm

        介紹
        使用環境:Visual C++ 9.0/視覺演播室2008年

        一段C++的小程序,可以幫程序代碼后輸出。

        map_code_line.cpp/C++


        1/*
        2 (c) OOMusou 2008年
        3
        4文件名 : map_code_line.cpp
        5編譯器 : Visual C++ 9.0/視覺演播室2008年
        6描述: 演示增加代碼的
        7發行 : 07/18/2008 1.0
        8 *
        9 #include iostream>
        10 #include fstream>
        11 #include string>
        12 #include map>
        13 #include algorithm>
        14
        15使用namespace std;
        16
        17 infile的ifstream (“map_code_line.cpp”);
        18 outfile的ofstream (“map_code_line_r.cpp”);
        19
        20 struct print_map {
        21空操作員() (pairint, string> p) {
        22 cout p.first ““ p.second endl;
        23 outfile p.first ““ p.second endl;
        24}
        25};
        26
        27 int扼要() {
        28 mapint, string>線;
        29
        30串線;
        31 int line_num = 1;
        32,當時(infile的getline (線))
        33 線[line_num++] =線;
        34
        35 infile.close ();
        36
        37 for_each (lines.begin (), lines.end (), print_map ());
        38 outfile.close ();
        39}

        執行結果

        1/*
        2 (c) OOMusou 2008年
        3
        4文件名 : map_code_line.cpp
        5編譯器 : Visual C++ 9.0/視覺演播室2008年
        6描述: 演示增加代碼的行號
        7發行 : 07/18/2008 1.0
        8 *
        9 #include iostream>
        10 #include fstream>
        11 #include string>
        12 #include map>
        13 #include algorithm>
        14
        15使用namespace std;
        16
        17 infile的ifstream (“map_code_line.cpp”);
        18 outfile的ofstream (“map_code_line_r.cpp”);
        19
        20 struct print_map {
        21 空操作員() (pairint, string> p) {
        22 cout p.first ““ p.second endl;
        23 outfile p.first ““ p.second endl;
        24 }
        25};
        26
        27 int扼要() {
        28 mapint, string>線;
        29
        30 串線;
        31 int line_num = 1;
        32 當時(infile的getline (線))
        33 線[line_num++] =線;
        34
        35 infile.close ();
        36
        37 for_each (lines.begin (), lines.end (), print_map ());
        38 outfile.close ();
        39}


        32行


        當時(infile的getline (線))
        線[line_num++] =線;

        是整個程序的關鍵:使用地圖,關鍵存放行號,價值存放每一行的程序代碼。而且隨著每一行程序代碼的讀入,自動增加行號。

        37行


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

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

        20行


        struct print_map {
        空操作員() (pairint, string> p) {
        cout p.first ““ p.second endl;
        outfile p.first ““ p.second endl;
        }
        };

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

        結論
        STL的地圖是很好用的容器,尤其子鏈寫法,若索引下沒有元素,會自動新增,所以才會有排行[line_number++] =線; 這么漂亮的寫法。



        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 囊谦县| 叙永县| 辽阳市| 德保县| 大石桥市| 灵台县| 德兴市| 望谟县| 广昌县| 青阳县| 湘潭市| 炎陵县| 临洮县| 措勤县| 江油市| 民勤县| 乐业县| 谷城县| 固镇县| 高碑店市| 哈密市| 钟祥市| 鹰潭市| 高邑县| 丰都县| 咸丰县| 宁波市| 庆安县| 华亭县| 兴国县| 永德县| 澳门| 广安市| 咸宁市| 巴马| 阳高县| 吉水县| 确山县| 湄潭县| 尼玛县| 庆城县|