第五章 C++与STL入门

来源:互联网 发布:mac恢复出厂系统版本 编辑:程序博客网 时间:2024/05/21 12:45

1.C++版框架

头文件、命名空间、流、const声明常数、bool类型

2.引用

现在存在着三种值的传递方式:单向值传递、指针传递、引用传递。注意引用只适用于变量,不适用于数组!!

3.字符串string

string、cstring、sstream。

字符串流:stringstream ss(s);然后ss可以当做流输入输出!,读取相当于cin和cout

4.再谈结构体

C++结构体不需要typedef,可以直接就是一个类型了!

结构体中可以含有:成员变量、成员函数、构造函数。

struct Point{    int x,y;//成员变量    Point(int x=0,int y=0):x(x),y(y){}//构造函数    Point operator +(const Point &b)//重载运算符    {        x=x+b.x;        y=y+b.y;        return *this;    }    ostream& operator << (ostream &out,const Point &b)    {        out<<"("<<b.x<<","<<b.y<<")";        return out;    }};

对于构造函数,可以在声明变量的时候初始化。如Point a,b(1,2),相当于调用了Point (),Point(1,2)。其中,this是指向当前结构体的指针。

5.模板格式

template<typename T>T sum(T *begin,T *end){    T ans=0;    for(T *p=begin;p!=end;p++)        ans=ans+*p;    return ans;}

6.algorithm头文件包含的常用函数(待更新)

  1. sort(a,a+n,cmp);
  2. lower_bound(a,a+n,x); upper_bound(a,a+n,x);
  3. swap(a,b);包括结构体
  4. max(a,b); min(a,b);
  5. reverse(a,a+n);
  6. next_permutation(begin,end);
7.不定长数组vector(向量)
#include <vector>

8.set(集合)
#include <set>

9.map(映射)
#include <map>

10.栈、队列、优先队列、双向队列
#include <stack>#include <queue>#include <deque>


原创粉丝点击