编程联系笔记

2016.9.26 动态数组模板

  • 一般情况下把template的定义和实现都写在.h文件里面;
  • 先引入标准库,再引入自己的库
  • 将普通类改编成模板类
    – 将类定义改编为模板类定义
    – 将函数定义改编成函数定义

2016.9.26 STL(Standard Template Library)标准模板库

  • vector
    – 动态数组
    – 头文件vector
    – 命名空间std
    – vector vt(n_elem)
    – n_elem可以是常量也可以是变量
  • array(C++11)
    – 使用栈内存
    – 长度固定
    – 头文件 array
    – array arr
    – n_elem必须是常量

  • list
    – 动态数组
    – 链表实现
    – 头文件list
    – 命名空间std
    – 构造函数

 
 
  1. list<int> c0; //空链表
  2. list<int> c1(3); //建一个含三个默认值是0的元素的链表
  3. list<int> c2(5,2); //建一个含五个元素的链表,值都是2
  4. list<int> c4(c2); //建一个c2的copy链表
  5. list<int> c5(c1.begin(),c1.end()); //c5含c1一个区域的元素[_First, _Last)

– 成员函数

2016.9.26 文件操作

  • 读文件
    – 头文件fstream
  1. 创建ifstream对象管理输入流
  2. 将该对象与特定的文件关联起来
  3. 使用cin的方式使用该对象
 
 
  1. //1、声明ifstream对象,将它与文件关联起来
  2. ifstream fin;
  3. fin.open("points.txt");
  4. //1、或者
  5. ifstream fin("points.txt");
  6. //2、以使用cin的方式使用该对象
  7. char ch;
  8. fin >> ch;//read a character from points.txt file
  9. char buf[80];
  10. fin >> buf;//read a word from points.txt file
  11. fin.getline(buf,80);//read a line from points.txt file
  12. string line
  13. getline(fin,line);//read a line from points.txt file
  • 流状态
    – 较新的C++提供了一种检查文件是否被打开的函数is_open
    – 打开一组文件:创建一个输入流对象并把它们依次关联到多个文件,更节省计算机资源
 
 
  1. ifstream fin;//create stream using default construct
  2. fin.open("P1.txt"); //associate stream with P1.txt file
  3. ........//do stuff
  4. fin.close();//terminate associate with P1.txt file
  5. fin.clear();//reset fin
  6. fin.open("P2.txt");
  7. ........
  8. fin.close();
  9. fin.clear();
  10. fin.open("P3.txt");
  11. ........
  12. fin.close();
  13. fin.clear();




转载于:https://www.cnblogs.com/LingjieLi-vera/p/5911503.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值