2017年5月22日星期一

类的用法-2017.2

### protected属性是准备给派生类用的,因为他是半公开的成员,只有派生类和基类可以修改他的内容,至于private是只有基类自己可以修改的,public是每个人都可以修改的
### 类可以多重派生,即A,B,C都是D的基类,怎样写呢:class D:public A,public B,public C{XXXX_xxx};举例子来说,你不光是你妈生的,也是你姥爷,你爸,你姥姥,你奶,你爷生的,不是很恰当;再举例子:纸和纸币的区别,自己买的纸就没有货币功能,必须是国家发行的才有货币功能,不但有货币功能,随着时间的流逝,时间赋予纸币了收藏价值的属性,即纸币至少有四个属性:本身是纸,有购买能力,有流通能力,有价值。

## 去重排序程序,sort,vector,ifstream读取,ofstream读写
// 命令行演示程序
```c
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int filetovector(string& fname,vector<string>& sevc)
{
//所有文件按行读入到sevc中,返回0为正常
   ifstream infile(fname.c_str());
   if(!infile)//没有输入文件!
    return 1;
   string s;
   while (getline(infile,s))
    sevc.push_back(s);
 infile.close();
 if (infile.eof())
  return 4;//没什么可排序的!
 else if(infile.bad()) //系统故障
  return 2;
 else if(infile.fail()) //读数据失败
  return 3;
  else
  return 0;
}
void vector2file(string& fname,vector<string>& sevc)
{
    ofstream ofile(fname.c_str());
    vector<string>::iterator it;
    for(it=sevc.begin(); it!=sevc.end(); it++)
    {
        ofile<<*it<<endl;
    }
}
int main(int argc, char* argv[])
{
    string filename; //文件名通过运行参数argv传递进来
    filename=argv[1];
    vector<string> v;
    filetovector(filename,v);
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    vector<string>::iterator it;
// 测试结果
//    for(it=v.begin(); it!=v.end(); it++)
//        cout<<*it<<endl;
    vector2file(filename,v);
    cout<<filename<<"去重排序元素结束!"<<endl;
    return 0;
}
```



58883
50097
41980~42015

没有评论:

发表评论