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) 的要求。

非成员函数

按照字典顺序比较 vector 中的值

operator==,!=,<,<=,>,>=(std::vector)
template< class T, class Alloc >

bool operator==( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(1)
template< class T, class Alloc >

bool operator!=( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(2)
template< class T, class Alloc >

bool operator<( const std::vector<T,Alloc>& lhs,

                const std::vector<T,Alloc>& rhs );
(3)
template< class T, class Alloc >

bool operator<=( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(4)
template< class T, class Alloc >

bool operator>( const std::vector<T,Alloc>& lhs,

                const std::vector<T,Alloc>& rhs );
(5)
template< class T, class Alloc >

bool operator>=( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(6)

 比较二个容器的内容。

1-2) 检查 lhsrhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。

3-6) 按字典序比较 lhsrhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。

参数

lhs, rhs - 要比较内容的容器
- 为使用重载 (1-2) , T 必须满足可相等比较 (EqualityComparable) 的要求。
- 为使用重载 (3-6) , T 必须满足可小于比较 (LessThanComparable) 的要求。顺序关系必须建立全序。

返回值

1) 若容器内容相等则为 true ,否则为 false

2) 若容器内容不相等则为 true ,否则为 false

3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false

4) 若 lhs 的内容按字典序小于等于 rhs 的内容则为 true ,否则为 false

5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false

6) 若 lhs 的内容按字典序大于等于 rhs 的内容则为 true ,否则为 false

复杂度

1-2) 若 lhsrhs 的大小不同则为常数,否则与容器大小成线性

3-6) 与容器大小成线性

特化 std::swap 算法

std::swap(std::vector)
template< class T, class Alloc >

void swap( vector<T,Alloc>& lhs,

           vector<T,Alloc>& rhs );
(C++17 前)
template< class T, class Alloc >

void swap( vector<T,Alloc>& lhs,

           vector<T,Alloc>& rhs ) noexcept(/* see below */);
(C++17 起)

 为 std::vector 特化 std::swap 算法。交换 lhsrhs 的内容。调用 lhs.swap(rhs) 。

参数

lhs, rhs - 要交换内容的容器

返回值

(无)

复杂度

常数。

异常

noexcept 规定:  

noexcept(noexcept(lhs.swap(rhs)))

(C++17 起)

调用示例

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

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;

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

    //6) 复制构造函数。构造拥有 other 内容的容器。
    std::vector<Cell> vector3(vector1);
    std::cout << "vector3:  ";
    std::copy(vector3.begin(), vector3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //1) 若容器内容相等则为 true ,否则为 false
    std::cout << "vector1 == vector2 :  " << (vector1 == vector2) << std::endl;
    std::cout << "vector1 == vector3 :  " << (vector1 == vector3) << std::endl;
    //2) 若容器内容不相等则为 true ,否则为 false
    std::cout << "vector1 != vector2 :  " << (vector1 != vector2) << std::endl;
    std::cout << "vector1 != vector3 :  " << (vector1 != vector3) << std::endl;
    //3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 <  vector2 :  " << (vector1 <  vector2) << std::endl;
    std::cout << "vector1 <  vector3 :  " << (vector1 <  vector3) << std::endl;
    //4) 若 lhs 的内容按字典序小于或等于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 <= vector2 :  " << (vector1 <= vector2) << std::endl;
    std::cout << "vector1 <= vector3 :  " << (vector1 <= vector3) << std::endl;
    //5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 >  vector2 :  " << (vector1 >  vector2) << std::endl;
    std::cout << "vector1 >  vector3 :  " << (vector1 >  vector3) << std::endl;
    //6) 若 lhs 的内容按字典序大于或等于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 >= vector2 :  " << (vector1 >= vector2) << std::endl;
    std::cout << "vector1 >= vector3 :  " << (vector1 >= vector3) << std::endl;
    std::cout << std::endl;

    //为 std::vector 特化 std::swap 算法。
    //交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
    std::cout << "swap before:" << std::endl;
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    vector1.swap(vector2);
    std::cout << "swap after:" << std::endl;
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

 输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/130497372