• 【一起来学C++】————(11)STL之vector容器容器


    1、vector基本概念

    功能:

    • vector数据结构和数组非常相似,也称为单端数组

    vector与普通数组区别:

    • 不同之处在于数组是静态空间,而vector可以动态扩展

    动态扩展:

    • 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
    • vector容器的迭代器是支持随机访问的迭代器

    在这里插入图片描述

    2、vector构造函数

    在这里插入图片描述

    #include 
    void printVector(vector<int>& v) {
    	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    	cout << *it << " ";
    	}
    	cout << endl;
    }
    
    void test01()
    {
    	vector<int> v1; //无参构造
    	for (int i = 0; i < 10; i++)
    	{
    		v1.push_back(i);
    	}
    	printVector(v1);
    	vector<int> v2(v1.begin(), v1.end());
    	printVector(v2);
    	vector<int> v3(10, 100);
    	printVector(v3);
    	vector<int> v4(v3);
    	printVector(v4);
    }
    
    int main() {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    vector iterator 的使用

    在这里插入图片描述
    在这里插入图片描述

    3、vector容量和大小

    在这里插入图片描述

    #include 
    void printVector(vector<int>& v) {
    	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    	cout << *it << " ";
    	}
    	cout << endl;
    }
    void test01()
    {
    	vector<int> v1;
    	for (int i = 0; i < 10; i++)
    	{
    		v1.push_back(i);
    	}
    	printVector(v1);
    	if (v1.empty())
    	{
    		cout << "v1为空" << endl;
    	}
    	else
    	{
    		cout << "v1不为空" << endl;
    		cout << "v1的容量 = " << v1.capacity() << endl;
    		cout << "v1的大小 = " << v1.size() << endl;
    	}
    //resize 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充
    	v1.resize(15,10);
    	printVector(v1);
    //resize 重新指定大小 ,若指定的更小,超出部分元素被删除
    	v1.resize(5);
    	printVector(v1);
    }
    int main() {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    总结:

    • 判断是否为空 — empty
    • 返回元素个数 — size
    • 返回容器容量 — capacity
    • 重新指定大小 — resize
    • capacity的代码在vs和g++下分别运行会发现,vs下capacity是按1.5倍增长的,g++是按2倍增长的。这个问题经常会考察,不要固化的认为,顺序表增容都是2倍,具体增长多少是根据具体的需求定义的。vs是PJ版本STL,g++是SGI版本STL。
    • resize在开空间的同时还会进行初始化,影响size。

    4、vector预留空间

    在这里插入图片描述

    #include 
    void test01()
    {
    	vector<int> v;
    	//预留空间
    	v.reserve(100000);
    	int num = 0;
    	int* p = NULL;
    	for (int i = 0; i < 100000; i++) {
    		v.push_back(i);
    		if (p != &v[0]) {
    			p = &v[0];
    			num++;
    			}
    	}
    	cout << "num:" << num << endl;
    }
    int main() {
    	test01();
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    总结:

    • 如果数据量较大,可以一开始利用reserve预留空间
    • reserve只负责开辟空间,如果确定知道需要用多少空间,reserve可以缓解vector增容的代价缺陷问题。

    5、vector 增删查改

    在这里插入图片描述

    // push_back/pop_back
    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a[] = { 1, 2, 3, 4 };
    	vector<int> v(a, a+sizeof(a)/sizeof(int));
    	vector<int>::iterator it = v.begin();
    	while (it != v.end()) {
    		cout << *it << " ";
    		++it;
    	}
    	cout << endl;
    	v.pop_back();
    	v.pop_back();
    	it = v.begin();
    	while (it != v.end()) {
    		cout << *it << " ";
    		++it;
    	}
    	cout << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    // find / insert / erase
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a[] = { 1, 2, 3, 4 };
    	vector<int> v(a, a + sizeof(a) / sizeof(int));
    	// 使用find查找3所在位置的iterator
    	vector<int>::iterator pos = find(v.begin(), v.end(), 3);
    	// 在pos位置之前插入30
    	v.insert(pos, 30);
    	vector<int>::iterator it = v.begin();
    	while (it != v.end()) {
    		cout << *it << " ";
    		++it;
    	}
    	cout << endl;
    	pos = find(v.begin(), v.end(), 3);
    	// 删除pos位置的数据
    	v.erase(pos);
    	it = v.begin();
    	while (it != v.end()) {
    		cout << *it << " ";
    		++it;
    	}
    	cout << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    // operator[]+index 和 C++11中vector的新式for+auto的遍历
    // vector使用这两种遍历方式是比较便捷的。
    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a[] = { 1, 2, 3, 4 };
    	vector<int> v(a, a + sizeof(a) / sizeof(int));
    	// 通过[]读写第0个位置。
    	v[0] = 10;
    	cout << v[0] << endl;
    	// 通过[i]的方式遍历vector
    	for (size_t i = 0; i < v.size(); ++i)
    		cout << v[i] << " ";
    	cout << endl;
    	vector<int> swapv;
    	swapv.swap(v);
    	cout << "v data:";
    	for (size_t i = 0; i < v.size(); ++i)
    		cout << v[i] << " ";
    	cout << endl;
    	cout << "swapv data:";
    	for (size_t i = 0; i < swapv.size(); ++i)
    		cout << swapv[i] << " ";
    	cout << endl;
    	// C++11支持的新式范围for遍历
    	for(auto x : v)
    		cout<< x << " ";
    	cout<<endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    6、vector 迭代器失效问题

    迭代器的主要作用就是让算法能够不用关心底层数据结构,其底层实际就是一个指针,或者是对指针进行了封装,比如:vector的迭代器就是原生态指针T*。因此迭代器失效,实际就是cpp 迭代器底层对应指针所指向的空间被销毁了,而使用一块已经被释放的空间,造成的后果是程序崩溃(即如果继续使用已经失效的迭代器,程序可能会崩溃)。

    对于vector可能会导致其迭代器失效的操作有:

    1. 会引起其底层空间改变的操作,都有可能是迭代器失效,比如:resize、reserve、insert、assign、
      push_back等。
    #include 
    using namespace std;
    #include 
    int main()
    {
    	vector<int> v{1,2,3,4,5,6};
    	auto it = v.begin();
    	// 将有效元素个数增加到100个,多出的位置使用8填充,操作期间底层会扩容
    	// v.resize(100, 8);
    	// reserve的作用就是改变扩容大小但不改变有效元素个数,操作期间可能会引起底层容量改变
    	// v.reserve(100);
    	// 插入元素期间,可能会引起扩容,而导致原空间被释放
    	// v.insert(v.begin(), 0);
    	// v.push_back(8);
    	// 给vector重新赋值,可能会引起底层容量改变
    	v.assign(100, 8);
    	/*
    	出错原因:以上操作,都有可能会导致vector扩容,也就是说vector底层原理旧空间被释放掉,
    	而在打印时,it还使用的是释放之间的旧空间,在对it迭代器操作时,实际操作的是一块已经被释放的
    	空间,而引起代码运行时崩溃。
    	解决方式:在以上操作完成之后,如果想要继续通过迭代器操作vector中的元素,只需给it重新
    	赋值即可。
    	*/
    	while(it != v.end())
    	{
    	cout<< *it << " " ;
    	++it;
    	}
    	cout<<endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    1. 指定位置元素的删除操作–erase
    #include 
    using namespace std;
    #include 
    int main()
    {
    	int a[] = { 1, 2, 3, 4 };
    	vector<int> v(a, a + sizeof(a) / sizeof(int));
    	// 使用find查找3所在位置的iterator
    	vector<int>::iterator pos = find(v.begin(), v.end(), 3);
    	// 删除pos位置的数据,导致pos迭代器失效。
    	v.erase(pos);
    	cout << *pos << endl; // 此处会导致非法访问
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    erase删除pos位置元素后,pos位置之后的元素会往前搬移,没有导致底层空间的改变,理论上讲迭代器不应该会失效,但是:如果pos刚好是最后一个元素,删完之后pos刚好是end的位置,而end位置是没有元素的,那么pos就失效了。因此删除vector中任意位置上元素时,vs就认为该位置迭代器失效了。
    总结

    • 在vector删除元素时,若迭代器指向删除元素之前的元素,则该迭代器仍有效。若迭代器指向被删元素及其之后的元素,则该迭代器将会失效。
    • 在添加、删除、修改元素时,尽量直接使用begin()和end(),或者使用insert()和erase()更新相应的迭代器,避免使用迭代器的中间量。
    vector<int> vi;
    //i保存的是vi的迭代器,有时操作后(如添加元素,删除元素)不确定i是否有效
    auto i=vi.begin()+n;
    vi.erase(i);
    //尽量直接使用begin()和end(),避免使用中间量i
    vi.erase(vi.begin()+n);
    //下面的表达式更好,因为它会自动更新i
    i=vi.erase(i);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    #include 
    #include 
    using namespace std;
     
    typedef map<int, int> Map;
    typedef map<int, int>::iterator MapIt;
     
    void print(Map &m)
    {
    	MapIt it;
    	for(it = m.begin(); it != m.end(); it++)
    	{
    		cout << it->second << " ";
    	}
     
    	cout << endl;
    }
     
    void deleteValueFromMap(Map &m, int n = 5)
    {
    	MapIt it;
    	for(it = m.begin(); it != m.end(); /*不能再自增了*/)
    	{
    		if(0 == it->second % n)
    		{
    			m.erase(it++);//it=m.erase(it);
    		}
    		else
    		{
    			it++;
    		}
    	}
    }
     
    int main()
    {
    	Map m;
    	int i = 0;
    	for(i = 0; i < 21; i++)
    	{
    		m[i] = i;
    	}
     
    	print(m);
     
    	deleteValueFromMap(m); // 程序ok
    	print(m);
     
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    【进阶版】 机器学习分类算法之XGBoost(集成学习算法)、LightGBM(梯度提升框架)(13)
    Vue中path和component属性
    心的感悟
    PHP 函数
    从零开始搭建仿抖音短视频APP-开发评论业务模块(1)
    C语言编程陷阱(四)
    2021 Java面试题大全(整理版)1000+面试题附答案详解,最全面详细,看完稳了!
    记使用docker部署项目出现问题
    408知识框架总结——操作系统
    第一季:12Linux常用服务类相关命令【Java面试题】
  • 原文地址:https://blog.csdn.net/qq_38364548/article/details/126415030