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

内容太多分成多部分来写

构造,替换,访问元素

构造

1) 默认构造函数。构造拥有默认构造的分配器的空容器。

vector();

2)构造拥有 count 个有值 value 的元素的容器 

vector( size_type count,const T& value, const Allocator& alloc = Allocator());

 3) 构造拥有个 count 默认插入T 实例的容器。不进行复制。

explicit vector( size_type count );

4) 构造拥有范围 [first, last) 内容的容器。

InputIt 是整数类型,则此构造函数拥有的效果同 vector(static_cast<size_type>(first), static_cast<value_type>(last), a) 。

(C++11 前)

此重载仅若InputIt 满足遗留输入迭代器 (LegacyInputIterator) 才参与重载决议,以避免和重载 (2) 的歧义。

扫描二维码关注公众号,回复: 13543786 查看本文章
(C++11 起)

vector( const vector& other, const Allocator& alloc );

5) 复制构造函数。构造拥有 other 内容的容器。若不提供 alloc ,则如同通过调用 

vector( const vector& other );

6) 移动构造函数。用移动语义构造拥有 other 内容的容器。分配器通过属于 other 的分配器移动构造获得。移动后,保证 otherempty()

vector( vector&& other );

参数

alloc - 用于此容器所有内存分配的分配器
count - 容器的大小
value - 以之初始化容器元素的值
first, last - 复制元素的来源范围
other - 用作初始化容器元素来源的另一容器

复杂度

1) 常数

2-3) 与 count 成线性

4) 与 firstlast 的距离成线性

5) 与 other 的大小成线性

6) 常数。

替换

1) 以 countvalue 的副本替换内容。

void assign( size_type count, const T& value );

2) 以范围 [first, last) 中元素的副本替换内容。若任一参数是指向 *this 中的迭代器则行为未定义。

InputIt 为整数类型,则此重载与 (1) 拥有相同效果。

(C++11 前)

此重载仅若 InputIt 满足遗留输入迭代器 (LegacyInputIterator) 才参与重载决议。

(C++11 起)

template< class InputIt >
void assign( InputIt first, InputIt last );

参数

count - 容器的新大小
value - 用以初始化容器元素的值
first, last - 复制来源元素的范围

复杂度

1) 与 count 成线性

2) 与 firstlast 间的距离成线性

访问元素

1、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

复杂度

常数。

 2、[]

reference       operator[]( size_type pos );

const_reference operator[]( size_type pos ) const;

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

参数

pos - 要返回的元素的位置

返回值

到所需元素的引用。

复杂度

常数。

注意

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

用例:

#include <vector>
#include <iostream>
using namespace std;

template<typename T>
void printVector(const string &name, const std::vector<T>& vec)
{
    std::cout << name << " : " ;
    for(auto &a :vec)
    {
        std::cout << a << " ";
    }
    std::cout << endl;
}

// 构造函数
void structure()
{
    std::cout << "structure start" << endl;
    // c++11 初始化器列表语法:
    std::vector<std::string> words1 {"I", "am", "the", "most", "handsome", "programmer"};
    printVector("words1", words1);

    // 构造拥有范围 [first, last) 内容的容器
    // words2 = words1
    std::vector<std::string> words2(words1.begin(), words1.end());
    printVector("words2", words2);

    // 拷贝构造 words3 = words1
    std::vector<std::string> words3(words1);
    printVector("words3", words3);

    // 构造拥有 count 个有值 value 的元素的容器
    //  words4 为 {"handsome", "handsome", "handsome", "handsome", "handsome"}
    std::vector<std::string> words4(5, "handsome");
    printVector("words4", words4);

    // 赋值构造
    std::vector<std::string> words5 = words3;
    printVector("words5", words5);

    std::cout << "structure end" << endl << endl;
}

void assign()
{
    std::cout << "assign start" << endl;

    // 以 count 份 value 的副本替换内容。
    std::vector<std::string> words1 {"I", "am", "the", "most", "handsome", "programmer"};
    printVector("words1_f", words1);
    words1.assign(5, "handsome");
    printVector("words1_s", words1);

    //以范围 [first, last) 中元素的副本替换内容。若任一参数是指向 *this 中的迭代器则行为未定义
    std::vector<std::string> words2;
    words2.assign(words1.begin(), words1.end());
    printVector("words2", words1);

    std::cout << "assign end" << endl << endl;
}

void at()
{
    std::cout << "at start" << endl;

    std::vector<std::string> words1 {"I", "am", "the", "most", "handsome", "programmer"};
    size_t size = words1.size();
    std::cout << "words1: ";
    for(size_t i = 0; i < size; i++)
    {
        std::cout << words1.at(i) << " ";
    }
    std::cout << endl;

    std::cout << "words1: ";
    for(size_t i = 0; i < size; i++)
    {
        std::cout << words1[i] << " ";
    }
    std::cout << endl;
    std::cout << "at end" << endl << endl;
}

int main( )
{
    structure();
    assign();
    at();
    return 0;
}

猜你喜欢

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