<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 如何為程序代碼加上行號(hào)

          如何為程序代碼加上行號(hào)

          作者: 時(shí)間:2011-05-12 來(lái)源:網(wǎng)絡(luò) 收藏

          若需要將放進(jìn)詞交報(bào)告或做文件時(shí),或許我們會(huì)想將方便講解,如同博客園顯示那樣,我們?cè)?a class="contentlabel" href="http://www.ex-cimer.com/news/listbylabel/label/如何">如何做呢?

          本文引用地址:http://www.ex-cimer.com/article/150758.htm

          介紹
          使用環(huán)境:Visual C++ 9.0/視覺(jué)演播室2008年

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

          map_code_line.cpp/C++


          1/*
          2 (c) OOMusou 2008年
          3
          4文件名 : map_code_line.cpp
          5編譯器 : Visual C++ 9.0/視覺(jué)演播室2008年
          6描述: 演示增加代碼的
          7發(fā)行 : 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,當(dāng)時(shí)(infile的getline (線))
          33 線[line_num++] =線;
          34
          35 infile.close ();
          36
          37 for_each (lines.begin (), lines.end (), print_map ());
          38 outfile.close ();
          39}

          執(zhí)行結(jié)果

          1/*
          2 (c) OOMusou 2008年
          3
          4文件名 : map_code_line.cpp
          5編譯器 : Visual C++ 9.0/視覺(jué)演播室2008年
          6描述: 演示增加代碼的行號(hào)
          7發(fā)行 : 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 當(dāng)時(shí)(infile的getline (線))
          33 線[line_num++] =線;
          34
          35 infile.close ();
          36
          37 for_each (lines.begin (), lines.end (), print_map ());
          38 outfile.close ();
          39}


          32行


          當(dāng)時(shí)(infile的getline (線))
          線[line_num++] =線;

          是整個(gè)程序的關(guān)鍵:使用地圖,關(guān)鍵存放行號(hào),價(jià)值存放每一行的程序代碼。而且隨著每一行程序代碼的讀入,自動(dòng)增加行號(hào)。

          37行


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

          將地圖內(nèi)容印出,因?yàn)榈貓D無(wú)法配合拷貝(),只好退而求其次使用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可以拿掉,只是方面在屏幕顯示而已。

          結(jié)論
          STL的地圖是很好用的容器,尤其子鏈寫(xiě)法,若索引下沒(méi)有元素,會(huì)自動(dòng)新增,所以才會(huì)有排行[line_number++] =線; 這么漂亮的寫(xiě)法。



          關(guān)鍵詞: 行號(hào) 加上 代碼 程序 如何

          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();