如何为程式码加上行号

2008-08-11 10:50:12.0     推荐:0    收藏:0    评论:0     来源:中国IT实验室

Abstract
若需要将程式码放进word交报告或做文件时,或许我们会想将程式码加上行号方便讲解,如同博客园显示程式码那样,我们该如何做呢?

Introduction

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

一段C++的小程式,可以帮程式码加上行号后输出。 map_code_line.cpp / C++
/*
(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
#include
using namespace std;
ifstream infile("map_code_line.cpp");
ofstream outfile("map_code_line_r.cpp");
struct print_map {
void operator() (pair p) {
cout << p.first << " " << p.second << endl;
outfile << p.first << " " << p.second << endl;
}
};
int main() {
map 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();
}


执行结果

[第1页]   [第2页]   [第3页]   [下一页]
您可以针对本文进行:[评论]  [收藏]  [推荐]  
  • 共有0条评论  点击查看更多评论
  • 网友评论仅供网友表达个人看法,并不表明e800同意其观点或证实其描述
我想发表评论:
用户名密码
  • 匿名发表
    验证码: