<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

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

如何為程序代碼加上行號

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

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

本文引用地址:http://dyxdggzs.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>線(xiàn);
29
30串線(xiàn);
31 int line_num = 1;
32,當時(shí)(infile的getline (線(xiàn)))
33 線(xiàn)[line_num++] =線(xiàn);
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/視覺(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>線(xiàn);
29
30 串線(xiàn);
31 int line_num = 1;
32 當時(shí)(infile的getline (線(xiàn)))
33 線(xiàn)[line_num++] =線(xiàn);
34
35 infile.close ();
36
37 for_each (lines.begin (), lines.end (), print_map ());
38 outfile.close ();
39}


32行


當時(shí)(infile的getline (線(xiàn)))
線(xiàn)[line_num++] =線(xiàn);

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

37行


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

將地圖內容印出,因為地圖無(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可以拿掉,只是方面在屏幕顯示而已。

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



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

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>