C++每日一练(STL_Vector)

本文为博主原创文章,未经博主允许不得转载。

https://blog.csdn.net/lxy_2011/article/details/52703216 

一、今日课题

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
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值