C++ STL总结(二)vector

1.vector内存结构

vector在内存中是顺序存储的,但是为了防止插入元素后频繁重新分配内存,所以会预留一定空间,如图所示。
在这里插入图片描述
但是,当预留空间用尽后,vector会重新申请一块更大的内存,并且将旧的内存中的数据全部拷贝到新的内存中,然后把旧内存返还给系统。

2.vector容量操作

函数功能
bool empty()检查容器是否为空
size_t size()返回容器中元素的个数
size_t max_size()返回容器由于系统或库的实现所限制可容纳的元素最大数量
size_t capacity()返回容器已分配空间可容纳的元素数量
void reserve(size_t new_cap)增加容器的容量,若new_cap大于capacity()则分配新存储,否则不做任何处理
void resize(size_t new_size, const T& x)重设容器的size大小,如果size变大,则以默认值填充变长的位置,如果size变小,则删除变短那部分的尾部元素
void shrink_to_fit()请求移除容器未使用的容量
#include <vector>
#include <iostream>
using std::vector;
using std::cout;
using std::endl;
int main(){
        vector<int> test;
        cout << "*************************after init****************************\n";
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after push_back****************************\n";
        test.push_back(1);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after reserve****************************\n";
        test.reserve(20);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after resize****************************\n";
        test.resize(5);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after shrink_to_fit****************************\n";
        test.shrink_to_fit();
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;
        return 0;

}

结果:

*************************after init****************************
empty(): 1  size(): 0  max_size(): 2305843009213693951  capacity(): 0
*************************after push_back****************************
empty(): 0  size(): 1  max_size(): 2305843009213693951  capacity(): 1
*************************after reserve****************************
empty(): 0  size(): 1  max_size(): 2305843009213693951  capacity(): 20
*************************after resize****************************
empty(): 0  size(): 5  max_size(): 2305843009213693951  capacity(): 20
*************************after shrink_to_fit()****************************
empty(): 0  size(): 5  max_size(): 2305843009213693951  capacity(): 5

3.vector构造

函数功能
vector()默认构造函数,构造一个空容器
vector(size_t n)构造具有n个默认值的容器
vector(size_t n, const T& value)构造具有n个值为value的容器
vector(first, last)构造拥有范围 [first, last) 内容的容器
vector( const vector& other )拷贝构造函数,构造一个拥有other内容的容器
vector( vector&& other )移动构造函数,用移动语义构造拥有other内容的容器,移动后other为empty()
vector( std::initializer_list init)构造拥有initializer_list init内容的容器
void test_construct(){
        cout << "*************************vector()****************************\n";
        vector<int> test;
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************vector(10)****************************\n";
        vector<int> test1(10);
        for(int i=0; i<test1.size(); ++i){
                cout << test1[i] << "  ";
        }
        cout << endl;

        cout << "*************************vector(10,2)****************************\n";
        vector<int> test2(10,2);
        for(int i=0; i<test2.size(); ++i){
                cout << test2[i] << "  ";
        }
        cout << endl;

        cout << "*************************vector(test2.begin(), test2.end())****************************\n";
        vector<int> test3(test2.begin(), test2.end());
        for(int i=0; i<test3.size(); ++i){
                cout << test3[i] << "  ";
        }
        cout << endl;

        cout << "*************************vector(test2)****************************\n";
        vector<int> test4(test2);
        for(int i=0; i<test4.size(); ++i){
                cout << test4[i] << "  ";
        }
        cout << endl;

        cout << "*************************vector(std::move(test2))****************************\n";
        vector<int> test5(std::move(test2));
        for(int i=0; i<test5.size(); ++i){
                cout << test5[i] << "  ";
        }

        cout << "*************************vector{1,2,3,4,5}****************************\n";
        vector<int> test6{1,2,3,4,5};
        for(int i=0; i<test6.size(); ++i){
                cout << test6[i] << "  ";
        }
        cout << endl;

*************************vector()****************************

*************************vector(10)****************************
0  0  0  0  0  0  0  0  0  0  
*************************vector(10,2)****************************
2  2  2  2  2  2  2  2  2  2  
*************************vector(test2.begin(), test2.end())****************************
2  2  2  2  2  2  2  2  2  2  
*************************vector(test2)****************************
2  2  2  2  2  2  2  2  2  2  
*************************vector(std::move(test2))****************************
2  2  2  2  2  2  2  2  2  2  
*************************vector{1,2,3,4,5}****************************
1  2  3  4  5  

4.vector元素访问

函数功能
at()访问指定元素,同时进行越界检查
operator[]访问指定元素
front()访问第一个元素
back()访问最后一个元素
data()返回指向内存中第一个元素的指针
void test_access(){
        vector<int>test{1,2,3,4,5};
        cout<< "at(2):" << test.at(2) << "  [3]:"<< test[3] << "  front:" << test.front() << "  back:" << test.back() << "  *data:" << *test.data() << endl;
}

at(2):3  [3]:4  front:1  back:5  *data:1

5.vector元素修改

函数功能
push_back(const T& value)在尾部插入新元素value
insert(iterator pos, const T& value)在pos前插入元素value
insert(iterator pos, size_t n, const T& value)在pos前插入n个元素value
insert(iterator pos, iterator first, iterator last)在pos前插入来自范围 [first, last)的元素
erase(iterator pos)移除pos处的元素
erase(iterator first, iterator last)移除范围 [first, last) 的元素
emplace_back(Args&&… args)在容器尾部就地构造元素
pop_back()删除尾部元素
swap(vector& other)将内容与other交换。不在单个元素上调用任何移动、复制或交换操作
clear()清除所有元素
void test_modifier(){
        vector<int>test;
        cout << "*************************push_back(1)****************************\n";
        test.push_back(1);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),2)****************************\n";
        test.insert(test.begin(), 2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),3,4)****************************\n";
        test.insert(test.begin(), 3, 4);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),test2.begin(),test2.end())****************************\n";
        vector<int>test2{1,2,3,4,5};
        test.insert(test.begin(), test2.begin(), test2.end());
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;
        
        cout << "*************************erase(test.begin())****************************\n";
        test.erase(test.begin());
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************erase(test.begin(),test.begin()+2)****************************\n";
        test.erase(test.begin(), test.begin()+2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************emplace_back****************************\n";
        class Book{
        public:
                Book(const std::string& name):m_name(name){}
                inline std::string getName(){return m_name;}
        private:
                std::string m_name;
        };
        vector<Book> test_book;
        test_book.emplace_back("C++ Primer");
        for(int i=0; i<test_book.size(); ++i){
                cout << test_book[i].getName() << "  ";
        }
        cout << endl;


        cout << "*************************pop_back()****************************\n";
        test.pop_back();
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************test.swap(test2)****************************\n";
        test.swap(test2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;
}
        cout << "*************************clear()****************************\n";
        test.clear();
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

*************************push_back(1)****************************
1  
*************************push_front(2)****************************
2  1  
*************************insert(test.begin(),2)****************************
2  2  1  
*************************insert(test.begin(),3,4)****************************
4  4  4  2  2  1  
*************************insert(test.begin(),test2.begin(),test2.end())****************************
1  2  3  4  5  4  4  4  2  2  1  
*************************erase(test.begin())****************************
2  3  4  5  4  4  4  2  2  1  
*************************erase(test.begin(),test.begin()+2)****************************
4  5  4  4  4  2  2  1  
*************************emplace_back****************************
C++ Primer  
*************************emplace_front****************************
Effective C++  C++ Primer  
*************************pop_back()****************************
4  5  4  4  4  2  2  
*************************pop_front()****************************
5  4  4  4  2  2  
*************************test.swap(test2)****************************
1  2  3  4  5  
*************************clear()****************************
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值