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

底层数组的指针

T* data() noexcept;						//(C++11 起) 
const T* data() const noexcept;			//(C++11 起) 

返回指向作为元素存储工作的底层数组的指针。指针满足范围 [data(); data() + size()) 始终是合法范围,即使容器为空(该情况下 data() 不可解引用)。
参数 (无)
返回值 指向底层元素存储的指针。对于非空容器,返回的指针与首元素地址比较相等。
复杂度 常数。
注意 若 size() 为 0 ,则 data() 可能或可能不返回空指针。
用例

    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    for(auto it = words.data(); it != words.data() + words.size(); it ++)
    {
    
    
        cout << *it << " ";
    }

容器擦除所有元素

void clear();						(C++11) 
void clear() noexcept;				(C++11) 

从容器擦除所有元素。此调用后 size() 返回零。
非法化任何指代所含元素的引用、指针或迭代器。任何尾后迭代器亦被非法化。
保持 vector 的 capacity() 不变(注意:更改容量上的标准限制在 vector::reserve 的规定中,见 [1] )。
参数 (无)
返回值 (无)
复杂度 与容器大小,即元素数成线性。

检查容器是否无元素

bool empty() const;						(C++11) 
bool empty() const noexcept;			(C++11) 

检查容器是否无元素,即是否 begin() == end() 。
参数 (无)
返回值 若容器为空则为 true ,否则为 false
复杂度 常数。
用例

 std::vector<std::string> words1 {
    
    "I", "am", "the", "most", "handsome", "programmer"};

    if(words1.empty())
    {
    
    
        cout << "words1 is empty!" << endl;
    }
    else
    {
    
    
        cout << "words1 is not empty!" << endl;
    }

    words1.clear();
    if(words1.empty())
    {
    
    
        cout << "words1 is empty!" << endl;
    }
    else
    {
    
    
        cout << "words1 is not empty!" << endl;
    }

    std::vector<std::string> words2;
    if(words2.empty())
    {
    
    
        cout << "words2 is empty!" << endl;
    }
    else
    {
    
    
        cout << "words2 is not empty!" << endl;
    }
    cout << endl;

容器擦除指定的元素

iterator erase( iterator pos );							(C++11) 
iterator erase( const_iterator pos );					(C++11)  
iterator erase( iterator first, iterator last );		(C++11) 
iterator erase( const_iterator first, const_iterator last ); (C++11) 

从容器擦除指定的元素。

  1. 移除位于 pos 的元素。
  2. 移除范围 [first; last) 中的元素。
    非法化位于擦除点或之后的迭代器,包含 end() 迭代器。
    迭代器 pos 必须合法且可解引用。从而不能以 end() 迭代器(合法,但不可解引用)为 pos 的值。
    若 first==last 则迭代器 first 不必可解引用:擦除空范围是无操作。
    参数
    pos - 指向要移除的元素的迭代器
    first, last - 要移除的元素范围
    类型要求 T 必须满足可移动赋值 (MoveAssignable) 的要求。
    返回值 后随最后被移除元素的迭代器。若迭代器 pos 指代最后元素,则返回 end() 迭代器。
    异常 不抛出,除非 T 的赋值运算符抛异常。
    复杂度
    线性:调用 T 析构函数的次数与被擦除的元素数相同,调用 T 赋值运算符的次数与 vector 中被擦除元素后的元素数相等。
    用例
    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    printVector("words : ", words);
    //删除偏移量为3的元素
    words.erase(words.begin() + 3);
    printVector("words erase: ", words);
    // 范围删除,删除偏移量为3至末尾的元素
    words.erase(words.begin() + 3, words.end());
    printVector("words erase range: ", words);

根据系统或库实现限制的容器可保有的元素最大数量

size_type max_size() const;				(C++11) 
size_type max_size() const noexcept;	(C++11) 

返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。
参数 (无)
返回值 元素数量的最大值。
复杂度 常数。
注意
此值通常反映容器大小上的理论极限,至多为 std::numeric_limits<difference_type>::max() 。运行时,可用 RAM 总量可能会限制容器大小到小于 max_size() 的值。
用例

    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    cout << "vector<std::string> max_size: " << words.max_size() << std::endl;
    std::vector<int> words1;
    cout << "vector<int> max_size: " << words1.max_size() << std::endl;

交换容器内容

void swap( vector& other );			 (C++17) 
void swap( vector& other ) noexcept(/* see below */);			(C++17) 

将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。
所有迭代器和引用保持合法。尾后迭代器被非法化。
若 std::allocator_traits<allocator_type>::propagate_on_container_swap::value 为 true ,则用非成员 swap 的非限定调用交换分配器。否则,不交换它们(且若 get_allocator() != other.get_allocator() ,则行为未定义)。 (C++11 起)
参数 other - 要与之交换内容的容器
返回值 (无)
异常 (无) (C++17 前)
noexcept 规定:
noexcept(std::allocator_traits::propagate_on_container_swap::value
|| std::allocator_traits::is_always_equal::value) (C++17 起)
复杂度 常数。

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 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
参数 lhs, rhs - 要交换内容的容器
返回值 (无)
复杂度 常数。
异常 noexcept 规定:
noexcept(noexcept(lhs.swap(rhs))) (C++17 起)
用例:

 std::vector<std::string> words1 {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    std::vector<std::string> words2 {
    
    "handsome"};
    printVector("words1: ", words1);
    printVector("words2: ", words2);
    words1.swap(words2);
    printVector("words1 swap: ", words1);
    printVector("words2 swap: ", words2);
    std::swap(words1, words2);
    printVector("words1 swap 2nd: ", words1);
    printVector("words2 swap 2nd: ", words2);

代码汇总:

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

void front_back()
{
    
    
    std::cout << "front_back start " << endl;
    std::vector<std::string> words1 {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    // 返回到容器首元素的引用
    std::cout << "words1: " << "front() : " << words1.front() << endl;
    //  修改值
    words1.front() = "you";
    std::cout << "words1: " << "front() : " << words1.front() << endl;

    // 返回到容器中最后一个元素的引用
    std::cout << "words1: " << "back() : " << words1.back() << endl;
    words1.back() = "human";
    std::cout << "words1: " << "back() : " << words1.back() << endl;
    std::cout << "front_back end" << endl << endl;
}

void MYiterator()
{
    
    
    std::cout << "MYiterator start " << endl;
    std::vector<std::string> words1 {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    for(std::vector<std::string>::const_iterator it = words1.cbegin(); it != words1.cend(); it ++)
    {
    
    
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    for(std::vector<std::string>::const_reverse_iterator it = words1.crbegin(); it != words1.crend(); it ++)
    {
    
    
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::cout << "MYiterator end" << endl << endl;
}

void capacity_size()
{
    
    
    std::cout << "capacity_size start " << endl;
    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    std::cout << "words :" << "capacity(): " << words.capacity() << std::endl;
    std::cout << "wordswordswords :" << "size(): " << words.size() << std::endl;

    words.push_back("GGX");
    std::cout << "words :" << "capacity(): " << words.capacity() << std::endl;
    std::cout << "words :" << "size(): " << words.size() << std::endl;

    //请求移除未使用的容量
    words.shrink_to_fit();
    std::cout << "words1 :" << "capacity(): " << words.capacity() << std::endl;
    std::cout << "words1 :" << "size(): " << words.size() << std::endl;
    std::cout << "capacity_size end" << endl << endl;
}

void MYResize()
{
    
    
    std::cout << "MYResize start " << endl;
    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    printVector("words : ", words);

    //若当前大小大于 count ,则减小容器为其首 count 个元素。
    std::vector<std::string> words1(words);
    std::cout << "words1 size : " << words1.size() << std::endl;
    words1.resize(words1.size() -1);
    std::cout << "words1 resize : " << words1.size() <<std::endl;
    printVector("words resize after: ", words1);

    // 若当前大小小于 count ,则后附额外元素,并以 value 的副本初始化
    std::vector<std::string> words2(words);
    std::cout << "words2 size : " << words2.size() << std::endl;
    words2.resize(words2.size() + 1, "handsome");
    std::cout << "words2 resize : " << words2.size() <<std::endl;
    printVector("words resize after: ", words2);
    std::cout << "MYResize end" << endl << endl;
}

void myData()
{
    
    
    std::cout << "myData start " << endl;
    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    for(auto it = words.data(); it != words.data() + words.size(); it ++)
    {
    
    
        cout << *it << " ";
    }
    cout << endl;
    std::cout << "myData end" << endl << endl;
}

void empty_clear()
{
    
    
    std::cout << "empty_clear start " << endl;
    std::vector<std::string> words1 {
    
    "I", "am", "the", "most", "handsome", "programmer"};

    if(words1.empty())
    {
    
    
        cout << "words1 is empty!" << endl;
    }
    else
    {
    
    
        cout << "words1 is not empty!" << endl;
    }

    words1.clear();
    if(words1.empty())
    {
    
    
        cout << "words1 is empty!" << endl;
    }
    else
    {
    
    
        cout << "words1 is not empty!" << endl;
    }

    std::vector<std::string> words2;
    if(words2.empty())
    {
    
    
        cout << "words2 is empty!" << endl;
    }
    else
    {
    
    
        cout << "words2 is not empty!" << endl;
    }
    cout << endl;
    std::cout << "empty_clear end" << endl << endl;
}

void myerase()
{
    
    
    std::cout << "myerase start " << endl;
    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    printVector("words : ", words);
    //删除偏移量为3的元素
    words.erase(words.begin() + 3);
    printVector("words erase: ", words);
    // 范围删除,删除偏移量为3至末尾的元素
    words.erase(words.begin() + 3, words.end());
    printVector("words erase range: ", words);
    std::cout << "myerase end" << endl << endl;
}

void max_size()
{
    
    
    std::cout << "max_size start " << endl;
    std::vector<std::string> words {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    cout << "vector<std::string> max_size: " << words.max_size() << std::endl;
    std::vector<int> words1;
    cout << "vector<int> max_size: " << words1.max_size() << std::endl;
    std::cout << "max_size end" << endl << endl;
}

void mySwap()
{
    
    
    std::cout << "mySwap start " << endl;
    std::vector<std::string> words1 {
    
    "I", "am", "the", "most", "handsome", "programmer"};
    std::vector<std::string> words2 {
    
    "handsome"};
    printVector("words1: ", words1);
    printVector("words2: ", words2);
    words1.swap(words2);
    printVector("words1 swap: ", words1);
    printVector("words2 swap: ", words2);
    std::swap(words1, words2);
    printVector("words1 swap 2nd: ", words1);
    printVector("words2 swap 2nd: ", words2);
    std::cout << "mySwap end" << endl << endl;
}

int main( )
{
    
    
//    structure();
//    assign();
//    at();
//    front_back();
//    MYiterator();
//    capacity_size();
//    MYResize();

    myData();
    myerase();
    max_size();
    mySwap();
    return 0;
}

在这里插入图片描述

猜你喜欢

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