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>::begin, 
std::vector<T,Allocator>::cbegin

iterator begin();

(C++11 前)

iterator begin() noexcept;

(C++11 起)

const_iterator begin() const;

(C++11 前)

const_iterator begin() const noexcept;

(C++11 起)

const_iterator cbegin() const noexcept;

(C++11 起)

 返回指向容器首元素的迭代器。

若容器为空,则返回的迭代器将等于 end() 。

参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。

返回指向容器尾端的迭代器

std::vector<T,Allocator>::end, 
std::vector<T,Allocator>::cend

iterator end();

(C++11 前)

iterator end() noexcept;

(C++11 起)

const_iterator end() const;

(C++11 前)

const_iterator end() const noexcept;

(C++11 起)

const_iterator cend() const noexcept;

(C++11 起)

返回指向容器末元素后一元素的迭代器。

此元素表现为占位符;试图访问它导致未定义行为。

参数

(无)

返回值

指向后随最后元素的迭代器。

复杂度

常数。

返回指向容器最后元素的逆向迭代器

std::vector<T,Allocator>::rbegin, 
std::vector<T,Allocator>::crbegin

reverse_iterator rbegin();

(C++11 前)

reverse_iterator rbegin() noexcept;

(C++11 起)

const_reverse_iterator rbegin() const;

(C++11 前)

const_reverse_iterator rbegin() const noexcept;

(C++11 起)

const_reverse_iterator crbegin() const noexcept;

(C++11 起)

返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。

 参数

(无)

返回值

指向首元素的逆向迭代器。

复杂度

常数。

返回指向前端的逆向迭代器

std::vector<T,Allocator>::rend, 
std::vector<T,Allocator>::crend

reverse_iterator rend();

(C++11 前)

reverse_iterator rend() noexcept;

(C++11 起)

const_reverse_iterator rend() const;

(C++11 前)

const_reverse_iterator rend() const noexcept;

(C++11 起)

const_reverse_iterator crend() const noexcept;

(C++11 起)

 返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

参数

(无)

返回值

指向末元素后一元素的逆向迭代器。

复杂度

常数。

调用示例

#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;

    //返回指向容器首元素的迭代器。若容器为空,则返回的迭代器将等于 end() 。
    //返回指向容器末元素后一元素的迭代器。此元素表现为占位符;试图访问它导致未定义行为。
    for (std::vector<Cell>::iterator it = vector1.begin(); it != vector1.end(); it++)
    {
        Cell cell = generate();
        *it = cell;
        std::cout << "iterator: " << &(*it) << " = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (std::vector<Cell>::const_iterator cit = vector1.cbegin(); cit != vector1.cend(); cit++)
    {
        std::cout << "const_iterator: " << &(*cit) << " : " << *cit << std::endl;
    }
    std::cout << std::endl;

    //返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。
    //返回指向逆向容器末元素后一元素的逆向迭代器。
    //它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。
    for (std::vector<Cell>::reverse_iterator rit = vector1.rbegin(); rit != vector1.rend(); rit++)
    {
        Cell cell = generate();
        *rit = cell;
        std::cout << "reverse_iterator: " << &(*rit) << " = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (std::vector<Cell>::const_reverse_iterator crit = vector1.crbegin(); crit != vector1.crend(); crit++)
    {
        std::cout << "const_reverse_iterator: " << &(*crit) << " : " << *crit << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/130456513
今日推荐