c++ 11标准模板(STL) std::vector (四)

定义于头文件 <vector>
template<

    class T,
    class Allocator = std::allocator<T>

> class vector;
(1)
namespace pmr {

    template <class T>
    using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;

}
(2)(C++17 起)

 1) std::vector 是封装动态数组的顺序容器。

2) std::pmr::vector 是使用多态分配器的模板别名。

元素相继存储,这意味着不仅可通过迭代器,还能用指向元素的常规指针访问元素。这意味着指向 vector 元素的指针能传递给任何期待指向数组元素的指针的函数。

(C++03 起)

vector 的存储是自动管理的,按需扩张收缩。 vector 通常占用多于静态数组的空间,因为要分配更多内存以管理将来的增长。 vector 所用的方式不在每次插入元素时,而只在额外内存耗尽时重分配。分配的内存总量可用 capacity() 函数查询。额外内存可通过对 shrink_to_fit() 的调用返回给系统。 (C++11 起)

重分配通常是性能上有开销的操作。若元素数量已知,则 reserve() 函数可用于消除重分配。

vector 上的常见操作复杂度(效率)如下:

  • 随机访问——常数 O(1)
  • 在末尾插入或移除元素——均摊常数 O(1)
  • 插入或移除元素——与到 vector 结尾的距离成线性 O(n)

std::vector (对于 bool 以外的 T )满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、序列容器 (SequenceContainer) 、连续容器 (ContiguousContainer) (C++17 起)及可逆容器 (ReversibleContainer) 的要求。

元素访问

访问指定的元素,同时进行越界检查

std::vector<T,Allocator>::at

reference       at( size_type pos );

const_reference at( size_type pos ) const;

 返回位于指定位置 pos 的元素的引用,有边界检查。

pos 不在容器范围内,则抛出 std::out_of_range 类型的异常。

参数

pos-要返回的元素的位置

返回值

到所需元素的引用。

异常

若 !(pos < size()) 则抛出 std::out_of_range

复杂度

常数。

访问指定的元素

std::vector<T,Allocator>::operator[]

reference       operator[]( size_type pos );

const_reference operator[]( size_type pos ) const;

 返回位于指定位置 pos 的元素的引用。不进行边界检查。

参数

pos-要返回的元素的位置

返回值

到所需元素的引用。

复杂度

常数。

注意

不同于 std::map::operator[] ,此运算符决不插入新元素到容器。

访问第一个元素

std::vector<T,Allocator>::front

reference front();

const_reference front() const;

 返回到容器首元素的引用。

在空容器上对 front 的调用是未定义的。

参数

(无)

返回值

到首元素的引用

复杂度

常数

注意

对于容器 c ,表达式 c.front() 等价于 *c.begin() 。

访问最后一个元素

std::vector<T,Allocator>::back

reference back();

const_reference back() const;

 返回到容器中最后一个元素的引用。

在空容器上对 back 的调用是未定义的。

参数

(无)

返回值

到最后元素的引用。

复杂度

常数。

注意

对于容器 c ,表达式 return c.back(); 等价于 { auto tmp = c.end(); --tmp; return *tmp; }

返回指向内存中数组第一个元素的指针

std::vector<T,Allocator>::data

T* data() noexcept;

(C++11 起)

const T* data() const noexcept;

(C++11 起)

 返回指向作为元素存储工作的底层数组的指针。指针满足范围 [data(); data() + size()) 始终是合法范围,即使容器为空(该情况下 data() 不可解引用)。

参数

(无)

返回值

指向底层元素存储的指针。对于非空容器,返回的指针与首元素地址比较相等。

复杂度

常数。

注意

若 size() 为 0 ,则 data() 可能或可能不返回空指针。

调用示例

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <vector>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}


int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };


    //3) 构造拥有 count 个有值 value 的元素的容器。
    std::vector<Cell> vector1(6, generate());
    std::generate(vector1.begin(), vector1.end(), generate);
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    for (size_t index = 0 ; index < vector1.size(); index++)
    {
        Cell cell = generate();
        //返回位于指定位置 pos 的元素的引用,有边界检查。
        //若 pos 不在容器范围内,则抛出 std::out_of_range 类型的异常。
        //reference       at( size_type pos );
        vector1.at(index) = cell;
        std::cout << "vector1.at( " << index << " ) = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (size_t index = 0 ; index < vector1.size(); index++)
    {
        //返回位于指定位置 pos 的元素的引用,有边界检查。
        //若 pos 不在容器范围内,则抛出 std::out_of_range 类型的异常。
        //const_reference at( size_type pos ) const;
        std::cout << "vector1.at( " << index << " ) = "
                  << vector1.at(index) << std::endl;
    }
    std::cout << std::endl;

    //3) 构造拥有 count 个有值 value 的元素的容器。
    std::vector<Cell> vector2(6, generate());
    std::generate(vector2.begin(), vector2.end(), generate);
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    for (size_t index = 0 ; index < vector2.size(); index++)
    {
        Cell cell = generate();
        //返回位于指定位置 pos 的元素的引用。不进行边界检查。
        //reference       operator[]( size_type pos );
        vector2[index] = cell;
        std::cout << "vector2[] " << index << " ] = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (size_t index = 0 ; index < vector2.size(); index++)
    {
        //返回位于指定位置 pos 的元素的引用。不进行边界检查。
        //const_reference operator[]( size_type pos ) const;
        std::cout << "vector2[ " << index << " ] = "
                  << vector2[index] << std::endl;
    }
    std::cout << std::endl;

    //返回到容器首元素的引用。在空容器上对 front 的调用是未定义的。
    Cell cell = generate();
    vector1.front() = cell;
    std::cout << "vector1.front() = " << cell << std::endl;
    std::cout << "vector1.front() : " << vector1.front() << std::endl;
    std::cout << std::endl;

    //返回到容器中最后一个元素的引用。在空容器上对 back 的调用是未定义的。
    vector1.back() = cell;
    std::cout << "vector1.back()  = " << cell << std::endl;
    std::cout << "vector1.back()  : " << vector1.back() << std::endl;
    std::cout << std::endl;

    for (size_t index = 0; index < vector1.size(); index++)
    {
        //返回指向作为元素存储工作的底层数组的指针。
        //指针满足范围 [data(); data() + size()) 始终是合法范围,
        //即使容器为空(该情况下 data() 不可解引用)。
        //T* data() noexcept;
        Cell cell = generate();
        *(vector1.data() + index) = cell;
        std::cout << "vector1: " << (vector1.data() + index)
                  << " = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (size_t index = 0; index < vector1.size(); index++)
    {
        //返回指向作为元素存储工作的底层数组的指针。
        //指针满足范围 [data(); data() + size()) 始终是合法范围,
        //即使容器为空(该情况下 data() 不可解引用)。
        //const T* data() const noexcept;
        std::cout << "vector1: " << (vector1.data() + index) << " : "
                  << *(vector1.data() + index) << std::endl;
    }

    return 0;
}

输出

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值