C++每日一练(STL_Vector)

来源:互联网 发布:淘宝一元抢拍是真的吗 编辑:程序博客网 时间:2024/05/17 08:28

一、今日课题

vector

二、实战演练

vector<type> variable_name (number_of_elements)

1)有何用?

Vectors 包含着一系列连续存储的元素,其行为和数组类似。访问Vector中的任意元素或从末尾添加元素都可以在常量级时间复杂度内完成,而查找特定值的元素所处的位置或是在Vector中插入元素则是线性时间复杂度

2)怎么用?

  • 头文件#include
  • 创建vector对象,vector vec
  • 代码示例
#include <iostream>#include <vector>#include<algorithm>using namespace std;typedef struct  rect{    int id;    int length;    int width;    //对于向量元素是结构体的,可在结构体内部定义比较函数,下面按照id,length,width升序排序    bool operator<(const rect &a)   const    {        if (id != a.id)            return id < a.id;        else        {            if (length != a.length)                return length < a.length;            else                return width < a.length;        }    }}Rect;int main(){    vector<Rect> vec;    Rect rect;    rect.id = 1;    rect.length = 2;    rect.width = 3;    vec.push_back(rect);    vector<Rect>::iterator it = vec.begin();    cout << (*it).id << ' ' << (*it).width << ' ' << (*it).length << endl;    system("pause"); return 0;}

3)Access & Operations
这里写图片描述


三、C++树

这里写图片描述


9/29/2016 4:43:17 PM

0 0