【STL】C++ vector基本使用

目录

一 vector常见构造

1 空容器构造函数(默认构造函数)

2 Fill 构造函数

3 Range 构造函数

4 拷贝构造函数

5 C++11构造

二 vector迭代器

1 begin && end

2 rbegin && rend

3 补充排序

三 vector 容量操作

1 size

2 resize

3 capacity

4 测试1

5 empty 

6 reserve

7 shrink_to_fit

8 注意

四 vector 修改操作

1 assign

2 push_back

3 pop_back

4 insert

5 erase

6 swap 

7 clear

五  vector 元素访问

1 operator [ ]

2 at

3 front && back

4 find

六 总结


一 vector常见构造

1 空容器构造函数(默认构造函数)

构造一个没有元素的容器。

2 Fill 构造函数

构造一个包含 n 个元素的容器。每个元素都是 val 的副本。

3 Range 构造函数

构造一个容器,其中包含与范围 [first,last] 一样多的元素,每个元素都按相同的顺序从该范围中的相应元素构造而成。

4 拷贝构造函数

构造一个容器,其中包含 x 中每个元素的副本,顺序相同。

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

int main()
{
       vector<int> a;
       vector<int> b(4, 100);//也可以只写4 构造大小和容量为4并且初始化值为0的vector
       vector<int> c(b.begin(), b.end());
       vector<int> d(b);

       for (size_t i = 0; i < b.size(); i++)
       {
              cout << b[i] << ' ';
       }
       cout << endl;

       for (size_t i = 0; i < c.size(); i++)
       {
              cout << c[i] << ' ';
       }
       cout << endl;

       for (size_t i = 0; i < d.size(); i++)
       {
              cout << d[i] << ' ';
       }
       cout << endl;

       a = d;//赋值拷贝
       for (size_t i = 0; i < d.size(); i++)
       {
              cout << d[i] << ' ';
       }

       return 0;
}

迭代器构造能否和string产生联系?

void Test2()
{
       string s("123456");
       vector<int> v(s.begin(), s.end());
       for (auto e : v)
       {
              cout << e << " ";
       }
}

int main()
{
       //Test1();
       Test2();
       return 0;
}

这里的字符1 2 3 4 5 6 对应的ASCII的值 

5 C++11构造

vector<int> v1 = { 1,2,3,4,5 };
vector<int> v2{ 1,2,3,4,5 };
vector<int> v3({ 1,2,3 });

二 vector迭代器

这里只演示用法, 不做过多讲解

1 begin && end

iterator begin();const_iterator begin() const;
iterator end();const_iterator end() const;
void Test3()
{
       vector<int> v(4, 1);
       vector<int>::iterator it = v.begin();
       while (it != v.end())
       {
              cout << *it << ' ';
              ++it;
       }
       cout << endl;

       const vector<int> vv(4, 10);
       vector<int>::const_iterator itt = vv.begin();
       while (itt != vv.end())
       {
              cout << *itt << ' ';
              ++itt;
       }
       
}

2 rbegin && rend

reverse_iterator rbegin();const_reverse_iterator rbegin() const;
reverse_iterator rend();const_reverse_iterator rend() const;
void Test4()
{
       vector<int> v(5);
       vector<int>::reverse_iterator it = v.rbegin();
       int i = 1;
       while (it != v.rend())
       {
              *it += i;
              ++it;
              ++i;
       }
       for (auto e : v)
       {
              cout << e << ' ';
       }

}

这个大家就想成和begin && end 相反就行, 没啥难度

 

3 补充排序

#include<algorithm>

sort(v1.begin(), v1.end()); // 升序
sort(v1.begin(), v1.end(), greater<int>()); // 降序

三 vector 容量操作

 这里就讲常用的, 大家看明白意思就可以直接上手, 没啥难度的

1 size

返回容器中的元素数。

size_type size() const noexcept;

2 resize

调整容器的大小,使其包含 n 个元素。

void resize (size_type n);void resize (size_type n, const value_type& val);

调整容器的大小,使其包含 n 个元素。

如果 n 小于当前容器大小(size),则内容将减少到其前 n 个元素,删除超出的元素(并销毁它们)。

如果 n 大于当前容器大小,则通过在末尾插入任意数量的元素来扩展内容,以达到 n 的大小。如果指定了 val,则新元素将初始化为 val。

如果 n 也大于当前容器容量(capacity),则会自动重新分配分配的存储空间。size 和 capacity都要改变

请注意,此函数通过插入或擦除容器中的元素来更改容器的实际内容

3 capacity

分配的存储容量的返回大小

此容量不一定等于向量大小。它可以相等或更大,额外的空间可以容纳增长,而无需在每次插入时重新分配。

size_type capacity() const;

4 测试1

void Test5()
{
       vector<int> v(5, 10);
       cout << v.size() << endl;
       cout << v.capacity() << endl;

       v.resize(2);
       cout << v.size() << endl;
       cout << v.capacity() << endl;
       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;

       v.resize(4, 10);
       cout << v.size() << endl;
       cout << v.capacity() << endl;
       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;

       v.resize(10);
       cout << v.size() << endl;
       cout << v.capacity() << endl;

}

5 empty 

返回向量是否为空(即其大小是否为 0)。

bool empty() const;

6 reserve

void reserve (size_type n);

请求容器容量至少足以包含 n 个元素。

如果 n 大于当前容器容量,则该函数会导致容器重新分配其存储,从而将其容量增加到 n(或更大)。

在所有其他情况下,函数调用不会导致重新分配,并且容器容量不受影响。

此函数对容器大小(size)没有影响,并且不能更改其元素。

void Test6()
{
       vector<int> v(5, 10);
       cout << v.size() << endl;
       cout << v.capacity() << endl;

       v.reserve(1);
       cout << v.size() << endl;
       cout << v.capacity() << endl;

       v.reserve(100);
       cout << v.size() << endl;
       cout << v.capacity() << endl;

       v.reserve(50);
       cout << v.size() << endl;
       cout << v.capacity() << endl;
}

7 shrink_to_fit

 请求容器减小其容量以适合其大小。

void shrink_to_fit();
void Test7()
{
       vector<int> v(4, 100);
       cout << v.size() << endl;
       cout << v.capacity() << endl;

       v.reserve(100);
       cout << v.size() << endl;
       cout << v.capacity() << endl;

       v.shrink_to_fit();
       cout << v.size() << endl;
       cout << v.capacity() << endl;
}

8 注意

capacity的代码在vs和g++下分别运行会发现,vs下capacity是按1.5倍增长的,g++是按2倍增长的。

这个问题经常会考察,不要固化的认为,vector增容都是2倍,具体增长多少是根据具体的需求定义的。vs是PJ版本STL,g++是SGI版本STL。

reserve只负责开辟空间,如果确定知道需要用多少空间,reserve可以缓解vector增容的代价缺陷问题。

resize在开空间的同时还会进行初始化,影响size.

四 vector 修改操作

有些用法我没有演示出来, 因为实际应用的时候基本用不到, 大家如果感兴趣也可以查阅文档, 非常容易上手 

1 assign

为容器分配新内容,替换其当前内容,并相应地修改其大小

range(1)
template <class InputIterator>  void assign(InputIterator first, InputIterator last);

fill(2)
void assign(size_type n, const value_type& val);

initializer list(3)
void assign(initializer_list<value_type> il);
void Test8()
{
       vector<int> a(5, 10);
       cout << a.size() << endl;
       cout << a.capacity() << endl;

       a.assign(6, 1);
       for (auto e : a)
       {
              cout << e << ' ';
       }
       cout << endl;
       cout << a.size() << endl;
       cout << a.capacity() << endl;

       vector<int> b;
       b.assign(a.begin(), a.end() - 1);
       for (auto e : b)
       {
              cout << e << ' ';
       }
}

2 push_back

在容器末尾的当前最后一个元素之后添加一个新元素。val 的内容被复制(或移动)到新元素。

void push_back (const value_type& val);
void push_back (value_type&& val);
void Test9()
{
       vector<int> v(4);
       v.push_back(1);
       v.push_back(2);
       v.push_back(3);
       for (auto e : v)
       {
              cout << e << ' ';
       }
}

3 pop_back

删除容器中的最后一个元素,从而有效地将容器大小减小 1。

void pop_back();

4 insert

过在指定位置的元素之前插入新元素来扩展容器,从而有效地通过插入的元素数增加容器大小

single element(1)
iterator insert(const_iterator position, const value_type& val);

fill(2)
iterator insert(const_iterator position, size_type n, const value_type& val);

range(3)
template <class InputIterator>iterator insert(const_iterator position, InputIterator 
first, InputIterator last);

move(4)
iterator insert(const_iterator position, value_type&& val);

initializer list(5)
iterator insert(const_iterator position, initializer_list<value_type> il);
void Test10()
{
       vector<int> v(4, 10);
       v.insert(v.begin(), 100);
       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;

       v.insert(v.end(), 4, 100);
       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;
}

5 erase

从容器中删除单个元素 (position) 或一系列元素 ( [first,last))

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
void Test11()
{
       vector<int> v;
       v.push_back(1);
       v.push_back(2);
       v.push_back(3);
       v.push_back(4);
       v.erase(v.begin());
       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;

       v.erase(v.begin(), v.begin()+2);
       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;
}

6 swap 

通过 x 的内容交换容器的内容,x 是另一个相同类型的容器对象。size可能有所不同。

void swap (vector& x);
void Test12()
{
       vector<int> v1(4, 10);
       for (auto e : v1)
       {
              cout << e << ' ';
       }
       cout << endl;

       vector<int> v2(4, 1);
       v1.swap(v2);
       for (auto e : v1)
       {
              cout << e << ' ';
       }
       cout << endl;
}

7 clear

从容器中删除所有元素(这些元素被销毁),使容器的大小为0。

void clear() noexcept;
void Test13()
{
       vector<int> v(1, 4);
       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;

       v.clear();
       cout << v.size() << endl;
       cout << v.capacity() << endl;

       for (auto e : v)
       {
              cout << e << ' ';
       }
       cout << endl;
}

五  vector 元素访问

 

1 operator [ ]

reference operator[] (size_type n);
const_reference operator[] (size_type n) const;
void Test14()
{
       vector<string> v;
       v.push_back("hello");
       v.push_back("world");

       cout << v[0] << endl;
       cout << v[1] << endl;

       cout << v[0][1] << endl;
       cout << v[0][2] << endl;
}

2 at

返回对容器中位置 n 处的元素的引用。

该函数会自动检查 n 是否在容器中有效元素的范围内,如果不是,则抛出out_of_range异常(即,如果 n 大于或等于其大小)。这与成员 operator[] 形成鲜明对比,后者不检查边界。

void Test15()
{
       vector<int> v;
       v.push_back(1);
       v.push_back(2);
       
       cout << v[0] << endl;
       cout << v.at(0) << endl;     
}

3 front && back

void Test16()
{
       vector<int> v;
       v.push_back(1);
       v.push_back(2);
       v.push_back(3);

       cout << v.front() << endl;
       cout << v.back() << endl;
}

4 find

template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)

在迭代器范围内寻找val, 如果 没找到, 那就返回v.end() 

void Test17()
{
       vector<int> v;
       v.push_back(1);
       v.push_back(3);
       v.push_back(2);
       v.push_back(0);

       vector<int>::iterator pos = find(v.begin(), v.end(), 2);
       if (pos != v.end())
       {
              v.insert(pos, 300);
       }
       for (auto e : v)
       {
              cout << e << ' ';
       }

}

 另一个测试:

int main()
{
       vector<int> v(1);
       v.push_back(1);
       v.push_back(2);
       v.push_back(3);
       v.push_back(4);
       vector<int>::iterator pos = find(v.begin(), v.end(), 9);
       v.insert(pos, 100);
       for (auto e : v)
       {
              cout << e << ' ';
       }

       return 0;

六 总结

好久没写博客了, 非常的抱歉. 很长一段时间都在学习Linux, 还是比较困难的. 中间也有一段时间摆烂. 幸好又复活过来了. 感谢所有支持我的人的陪伴吧!

vector这节用法非常简单的, 如果大家感觉概念和理解还是差点意思的话, 一定看看我前面写的 string, 里面对一些概念进行了详细讲解, 当然后续我也会出简单的模拟实现, 大家看了底层, 就更容易懂了. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值