c++11 standard template (STL) std::vector (eleven)

Defined in the header file <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) (since C++17)

 1) std::vectoris a sequential container that encapsulates a dynamic array.

2) std::pmr::vectoris a template alias that uses a polymorphic allocator.

Elements are stored sequentially, which means that elements can be accessed not only through iterators, but also with regular pointers to elements. This means that a pointer to a vector element can be passed to any function expecting a pointer to an array element.

(since C++03)

The storage of vector is managed automatically, expanding and shrinking on demand. A vector typically takes up more space than a static array because more memory is allocated to manage future growth. The way the vector is used is not to reallocate every time an element is inserted, but only when the extra memory is exhausted. The total amount of memory allocated can be queried with the capacity() function. Additional memory can be returned to the system through a call to shrink_to_fit(). (since C++11)

Reallocation is usually a performance-intensive operation. The reserve() function can be used to eliminate reallocation if the number of elements is known.

The complexity (efficiency) of common operations on vector is as follows:

  • Random access - constant O(1)
  • Insert or remove elements at the end - amortized constant O(1)
  • Insert or remove elements - O(n) linear in distance to end of vector

std::vector(For boolother T) Satisfy the requirements of Container , AllocatorAwareContainer , SequenceContainer , ContiguousContainer (since C++17) and ReversibleContainer .

non-member function

Compares the values ​​in vector lexicographically

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)

 Compares the contents of two containers.

1-2) lhsChecks rhsthat the contents of and are equal, that is, they have the same number of elements lhsand each element rhsof is compared equal to the same-positioned element of .

3-6) Compares the contents of lhsand rhs. Compare by a function equivalent to std::lexicographical_compare.

parameter

lhs, rhs - the container whose contents are to be compared
- In order to use the overload (1-2), the requirement of EqualityComparableT must be satisfied .
- In order to use the overload (3-6), the requirement of LessThanComparableT (LessThanComparable) must be met . Order relations must establish a total order.

return value

1) true if the container contents are equal, otherwise false

2) true if the container contents are not equal, otherwise false

3) true lhsif the content of is lexicographically less than rhs the content of , otherwise false

4) true if the content lhsof is lexicographically less than or equal to rhs the content of , otherwise 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;
}

 输出

 

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/130497372