【ThinkingInC++】58、文件加入行号

Linenum.cpp


/**
* 书本:【ThinkingInC++】
* 功能:为任何文件加入行号
* 时间:2014年9月23日19:35:25
* 作者:cutter_point
*/

#include "../require.h"
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    cout<<"输入要加行号的文件:";
    string fname;
    cin>>fname;

    ifstream in(fname+".txt");
    assure(in, fname+".txt");
    string line;
    vector<string> lines;
    while(getline(in, line))
        lines.push_back(line);
    if(lines.size() == 0)
        return 0;

    int num=0;
    const int width=int(log10(lines.size()))+1;
    cout<<"width="<<width<<endl;

    for(int i=0 ; i<lines.size() ; ++i)
    {
        //这两个是为了控制“行号”这个数值占用的空间的
        cout.setf(ios::right, ios::adjustfield);
        cout.width(width);
        if(i < lines.size())
        {
            cout<<++num<<") "<<lines[i]<<endl;
        }
        else
        {
            //当吧那个控制行号的去掉,那么这个就会出现没有对齐的情况
            cout<<++num<<") "<<"cutter_point"<<endl;
        }
    }

    return 0;
}

/*
//! 吧for循环改成这样,你就会发现差距了
    for(int i=0 ; i<1000 ; ++i)
    {
        //这两个是为了控制“行号”这个数值占用的空间的
        //cout.setf(ios::right, ios::adjustfield);
        //cout.width(width);
        if(i < lines.size())
        {
            cout<<++num<<") "<<lines[i]<<endl;
        }
        else
        {
            //当吧那个控制行号的去掉,那么这个就会出现没有对齐的情况
            cout<<++num<<") "<<"cutter_point"<<endl;
        }
    }
*/

DefaultCopyConstructor.cpp

/**
* 书本:【ThinkingInC++】
* 功能:编译器为新类(组合类),创建默认拷贝构造函数
* 时间:2014年9月23日19:36:17
* 作者:cutter_point
*/

#include <iostream>
#include <string>

using namespace std;

class WithCC
{
public:
    WithCC() {}
    //这个类的拷贝构造函数,显示写出来了,那么以后所有的关于这个类的拷贝全部调用这个
    WithCC(const WithCC&){cout<<"WithCC(WithCC&)"<<endl;}
};

class WoCC
{
    string id;
public:
    //这个没有拷贝构造函数,调用的时候用默认的位拷贝
    WoCC(const string& ident="") : id(ident) {}
    void print(const string& msg="" ) const
    {
        if(msg.size() != 0 )
            cout<<msg<<" : ";
        cout<<id<<endl;
    }
};

class Composite
{
    WithCC withcc;
    WoCC wocc;
public:
    //这个没有拷贝构造函数,调用数据成员的数据执行相应的操作
    Composite() : wocc("Composite()") {}
    void print(const string& msg="") const {wocc.print(msg); }
};

int main()
{
    Composite c;
    c.print("Contents of c");
    cout<<"calling Composite copy-constructor"<<endl;
    Composite c2=c; //调用私有成员的拷贝构造函数拷贝,没有的就使用默认的位拷贝
    c2.print("Contents of c2");

    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值