vector的用法

vector的用法

1、构造函数

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;


namespace std
{
	//vector作为一个管理 动态增长数组(可以是各种类型的数据) 的类。

	void test_vector1()
	{
		vector<int> v1;
		vector<int> v2(10,1);
		vector<int> v3(v2);
	}

2、遍历vector

cpp 复制代码
void test_vector2()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		//访问vector中的数据(方式)
		//(1)下标+[]
		for (size_t i=0;i<v1.size();++i)
		{
			v1[i]++;
		}

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

		//(2)迭代器
		vector<int>::iterator it = v1.begin();
		while (it != v1.end())
		{
			(*it)--;
			cout<< *it <<" ";
			++it;
		}
		cout << endl;
		
	
	//(3)范围for
		for (auto e:v1)
		{
			cout<< e <<" ";
		}
		cout << endl;
	}

3、测试 vector 的默认扩容机制

cpp 复制代码
void TestVectorExpand()
	{
		size_t sz;
		vector<int> v;

		//v.resize(100); //不仅开了100个空间,而且初始化(赋值)了
		v.reserve(100); //提前开好100个空间
		sz = v.capacity();
		cout <<"making v grow:\n";
		for (int i=0;i<100;++i)
		{
			v.push_back(i);
			if (sz != v.capacity()) //之前的_capacity 不等于 扩容后的_capacity
			{
				sz = v.capacity();
				cout<< "capacity changed" << sz << '\n';
			}
		}
	}

	void test_vector3()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		cout<< v1.max_size() <<endl;

		TestVectorExpand();
	}

4、vector的迭代器区间与插入、删除

cpp 复制代码
void test_vector4()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		//迭代器区间 与 插入
		vector<int>::iterator pos = find(v1.begin(),v1.end(),3);  //在迭代器区间 [v1.begin(),v1.end()) 内查找 3
		if (pos != v1.end())  //这儿是检查
		{
			v1.insert(pos,30); //在迭代器类型的pos位置之前插入30
		}

		for (auto e:v1)
		{
			cout<< e <<" ";
		}
		cout << endl;

		//迭代器区间 与 删除
		pos = find(v1.begin(), v1.end(), 3);  //在迭代器区间 [v1.begin(),v1.end()) 内查找 3
		if (pos != v1.end())  //这儿是检查
		{
			v1.erase(pos); //删除pos位置的数
		}

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

5、vector的排序

cpp 复制代码
void test_vector5()
	{
		vector<int> v1;
		v1.push_back(9);
		v1.push_back(7);
		v1.push_back(8);
		v1.push_back(6);
		v1.push_back(14);
		v1.push_back(18);
		v1.push_back(16);
		v1.push_back(17);
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		sort(v1.begin(),v1.end()); //sort默认升序排列
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		less<int> ls; //仿函数--- STL里的sort就是默认用这个的(< 排升序)
		greater<int> gt; //(> 排降序)
		//sort(v1.begin(),v1.end(),gt);
		sort(v1.begin(), v1.end(), greater<int>());

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		
		string s("hello,1234564321");
		//s 是按照ascall码表来排序的
		sort(s.begin(),s.end()); //升序排列:<
		cout<< s <<endl;

		sort(s.begin(), s.end(),greater<char>()); //降序排列:>
		cout << s << endl;

	//附:vector<char> v 与 string str 的区别:数据结尾\0  +=  find  比较大小  to_string  <<  >>  
    //因此,vector<char> v 无法替代 string str 

		vector<string> strV;

		string str1("张三");
		strV.push_back(str1);
		strV.push_back(string("李四"));
		strV.push_back("王六");
		strV.push_back("赵七");

		for (const auto& str:strV) //不加引用前,将strV的元素逐个取出来赋值(由于每个元素都是string类,赋值需要深拷贝,代价太大)给str,然后再逐个打印str
		{                          //加入应用后,赋值的过程不再需要深拷贝,而是取别名,代价大大降低
			cout<< str <<endl;
		}

	// vector::push_back
	// void push_back(const T& val)
	// (1)引用: T -> int/string
	// (2) const引用:一方面保障传入的参数不可修改的特性;另一方面允许常量作为参数。
	
	}

}

int main()
{
	std::test_vector5();

	return 0;
}
相关推荐
云泽8081 小时前
函数模板与类模板:C++泛型编程核心解析
java·开发语言·c++
R-G-B5 小时前
【25】MFC入门到精通——MFC静态文本框 中字符串 连续输出 不覆盖先前的文本 换行输出
c++·mfc·mfc静态文本框输出字符串·mfc静态文本框连续输出字符串·mfc静态文本框换行输出字符串
FFZero17 小时前
【C++/Lua联合开发】 (二) Lua调用C++函数
c++·junit·lua
CoderCodingNo8 小时前
【GESP】C++四级真题 luogu-B4068 [GESP202412 四级] Recamán
开发语言·c++·算法
一个不知名程序员www8 小时前
算法学习入门---双指针(C++)
c++·算法
Maple_land8 小时前
常见Linux环境变量深度解析
linux·运维·服务器·c++·centos
Larry_Yanan8 小时前
QML学习笔记(四十四)QML与C++交互:对QML对象设置objectName
开发语言·c++·笔记·qt·学习·ui·交互
Want5959 小时前
C/C++大雪纷飞①
c语言·开发语言·c++
Mr_WangAndy9 小时前
C++设计模式_行为型模式_策略模式Strategy
c++·设计模式·策略模式·依赖倒置原则
LoveXming9 小时前
Chapter11—适配器模式
c++·设计模式·适配器模式·开闭原则