您现在的位置是:网站首页> 编程资料编程资料
.net开发:为程式码加上行号的方法详解_实用技巧_
2023-05-24
311人已围观
简介 .net开发:为程式码加上行号的方法详解_实用技巧_
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
#include
#include
#include
执行结果
以下为引用的内容:
/*
(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
#include
#include
#include
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
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;这麽漂亮的写法。
相关内容
- ASP.NET(C#) 读取EXCEL另加解决日期问题的方法分享_实用技巧_
- 在ASP.NET中插入flash代码实例_实用技巧_
- 创建基于ASP.NET的SMTP邮件服务的具体方法_实用技巧_
- ASP.NET DropDownListCheckBox使用示例(解决回发问题)_实用技巧_
- 一个.net 压缩位图至JPEG的实例代码_实用技巧_
- Attribute/特性心得随笔_实用技巧_
- .Net程序防止被注入代码(整站通用)分享_实用技巧_
- asp.net中调用Office来制作3D统计图的实例代码_实用技巧_
- ASP.NET中实时图表的实现方法分享_实用技巧_
- IIS 浏览aspx页面出现无法显示XML页的解决方法分享_实用技巧_
