c++ ------>std :: vector__用法实现
  L8iEHH07GzZb 2023年11月02日 43 0

各位好友, 接下来, 开战 vector 环节 !

----->序列式容器

序列式容器, 其中的元素都是可序的, 但未必有序 !C++ 语言本身提供了一个 序列容器 ---->array(数组)

STL 另外提供了一个 vector --------->原因 :>vector 数据安排 以及操作方式, 与 array 非常相似

两者的唯一差别 ------------>在于 空间运用的灵活性。 array 是静态空间, vector 是动态可以增长的空间 !

------------------------->因此, vector 的运用 ------>对于内存的合理利用 ~~ 运用灵活性有着很多的帮助 !

其实, 在上一章节的学习下, String 类 模拟实现 ---->便可发现 :>

增加元素的过程, 实际上 ---->开辟新空间 / 数据移动 / 释放旧空间 ------->这是一个大的工程, 时间成本很高

----------------->因此, vector 的实现技术, 关键在于 ---->其对大小的控制 以及重新配置时的数据移动效率 !


下面,展示 vector 具体用法 ----->代码如下 :>

--------->测试环节一 :>

//展示 vector 用法

#include <iostream>
#include <vector>

using std :: cout;
using std :: endl;

using std :: string;
using std :: vbector;

void test_01()
{
	vector<int> v;
  
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  v.push_back(4);
  v.push_back(5);
  
  //遍历
  for(size_t i = 0; i < v.size(); i++)
  {
    cout << v[i] << " ";
  }
  cout << endl;
  
  vector<int> :: iterator it = v.begin();
  while(it != v.end())
  {
  	cout << *it << " ";
    it++;
  }
  cout << endl;
  
  for(auto e : v)
  {
  	cout << e << " ";
  }
  cout << endl;
}

void test_02()
{
	vector<string> strv;
  
  string name1("苹果");
  
  strv.push_back(name1);
  
  strv.push_back(string("橘子"));
  
  strv.push_bak("荔枝");
  
  for(auto e : strv)
  {
  	cout << e << " ";
  }
  cout << endl;
}

int main()
{
	test_0x();
}

----->图示 一 :>

c++ ------>std :: vector__用法实现_Vector 具体用法实现


----->图示 二 :>

c++ ------>std :: vector__用法实现_Vector 具体用法实现_02


--------->测试环节 二 :>

---->代码 (1) :

//继续测试 vector 具体用法

void test_0a()
{
	vector<int>v1(10, 1);
  
  vector<string>v2(10, "$$$");
  
  for(auto e : v1)
  {
  	cout << e << "   ";
  }
  cout << endl;
  
  for(auto e : v2)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  vector<int>v3(v1.begin(), v2.end());
  for(auto e : v3)
  {
  	cout << e << "   ";
  }
  cout << endl;
  
  vector<string>v4(v2.begin(), v2.end());
  for(auto e : v4)
  {
  	cout << e << " ";
  }
  cout << endl;
}
    
int main()
{
  test_0x();
	return 0;
}

---->图示 一 :>

c++ ------>std :: vector__用法实现_不同平台下 容量增长情况对比_03


---->代码 (2) :>

//值初始化 <int>
//圆括号 与花括号对比

void test_0c()
{
	vector<int> v1(10);
  
  for(auto e : v1)
  {
		cout << e << " ";
  }
  cout << endl;
  
  vector<int> v3{ 10 };
  
  for(auto e : v3)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  vector<int> v2(10, 1);
  
  for(auto e : v2)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  vector<int> v4{10, 1};
  
  for(auto e : v4)
  {
  	cout << e << " ";
  }
  cout << endl;
}

int main()
{
  test_0c();
	return 0;
}

---->图示 二 :>

c++ ------>std :: vector__用法实现_Vecttor 常用接口实现_04


---->代码 (3) :>

//值初始化 <string>
//圆括号 与花括号对比

void test_0D()
{
	vector<string> v1{ " apple " };
  
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  /*
  //错误
  //不能使用字符串的字面值 来构建 vector 对象
  vector<string> v1(" apple ");
  
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  */
  
  //空字符串
  vector<string> v2{ 5 };
  
  for(auto e : v2)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  vector<string> v3{5, "apple"};
  
  for(auto e : v3)
  {
  	cout << e << " ";
  }
  cout << endl;
}

int main()
{
  test_0D();
	return 0;
}

---->图示 三 :>

c++ ------>std :: vector__用法实现_圆括号 与花括号对比_05


--------->测试环节 三 :>

//继续测试 vector 应用场景

#include <algorithm>

using std :: less;
using std :: greater;

void test_0b()
{
	vector<int>v1;
  
  v1.push_back(1);
  v1.push_back(7);
  v1.push_back(2);
  v1.push_back(5);
  v1.push_back(3);
  v1.push_back(8);
  v1.push_back(0);
  
  //升序
  sort(v1.begin(), v1.end());
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //运用 std :: less<T> 进行升序
  sort(v1.begin(), v1.end(), less<T>());
  for(auto e : v1) 	//此种写法太 画蛇添足了 !不建议 !
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //降序
  sort(v1.rbegin(), v1.rend());
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //运用 std :: greater<T> 进行降序
  sort(v1.begin(), v1.end(), greater<T>());
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
}

int main()
{
  test_0b();
	return 0;
}

---->图示  :>

c++ ------>std :: vector__用法实现_值初始化_06


--------->测试环节 四 :>

//继续测试 vector 具体应用场景
#include <algorithm>

void test_0c()
{
  //区间取值
	int a[] = {12, 2, 7, 3, 8, 20};
  vector<int>v1(a, a + 4);
  
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //排序字符串 (升序)
  vector<string>strv;
  
  strv.push_back("longan");
  strv.push_back("litchi");
  strv.push_back("nactarine");
  
  strv.push_back("orange");
  strv.push_back("apple");
  
  sort(strv.begin(), strv.end());
  for(auto e : strv)
  {
  	cout << e << " ";
  }
  cout << endl;
}

int main()
{
  test_0c();
	return 0;
}

-------->图示 :>

c++ ------>std :: vector__用法实现_值初始化_07


--------->测试环节 五 :>

//继续测试 vector 具体应用场景

void test_0d()
{
	vector<int> v1;
  
  //开辟 指定空间 存储数据
  v1.reserve(10);
  
  //错误样例
  for(size_t i = 1; i < 10; i++)
  {
  		v1[i] = i;
  }
  
  //正确示范
  for(size_t i = 1; i < 10; i++)
  {
  	v1.push_back(i);
  }
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
}

int main()
{
  test_0d();
	return 0;
}

----->错误示范 :>

c++ ------>std :: vector__用法实现_圆括号 与花括号对比_08


----->正确示范 :>

c++ ------>std :: vector__用法实现_Vector 具体用法实现_09


--------->测试环节 0A :>

//继续探究 vector 具体应用场景

void test_0A()
{
	int a[] = {11, 13, 11, 12, 18, 11, 23, 22, 11, 11, 10};
  vector<int> v1(a, a + sizeof(a) / sizeof(a[0]));
  
  //遍历
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //头删
  v1.erase(v1.begin());
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //头插
  v1.insert(v1.begin(), 16);
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //删除第五个数据(下标从 零 开始)
  v1.erase(v1.begin() + 4);
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //删除所有数字 " 11 "
  //第一种写法
  //vector<int> :: iterator pos = find(v1.begin(), v1.end(), 11);
  //
  //第二种写法
  auto pos = find(v1.begin(), v1.end(), 11);
  while(pos != v1.end())
  {
  	v1.erase(pos);
    
    pos = find(v1.begin(), v1.end(), 11);
  }
  
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
  
  //值覆盖
  v1.assign(7, 36);
  for(auto e : v1)
  {
  	cout << e << " ";
  }
  cout << endl;
}

int main()
{
	test_0A();
  return 0;
}

------->图示 一 :>

c++ ------>std :: vector__用法实现_值初始化_10


------->图示 二 :>

c++ ------>std :: vector__用法实现_圆括号 与花括号对比_11


各位好友, 下面展示 ---->vector (容器)在不同平台下, 增长情况 :>

代码如下 :>

//测试 vector(容器)于不同平台下, 容量增长情况

#include <iostream>
#include <vector>

using std :: cout;
using std :: endl;
using std :: vector;

void test_0B()
{
	size_t sz;
  vbector<int> v;
  sz = v.capacity();
  
  cout << " v.capoacity()  ---->grow :>" << endl;
  for(size_t i = 0; i < 100; i++)
  {
  	v.push_back(i);
    
    if(sz != v.capacity())
    {
      sz = v.capacity;
      
      cout << " show v.capacity() ---->change :>" << sz << endl;
    }
  }
}

int main()
{
  test_0B();
	return 0;
}

-------->VS__2019 平台 :>

c++ ------>std :: vector__用法实现_值初始化_12


-------->Linux 平台 :>

c++ ------>std :: vector__用法实现_Vector 具体用法实现_13

c++ ------>std :: vector__用法实现_圆括号 与花括号对比_14


各位好友, 本期 vector(容器)具体应用场景  ------>已测试完成 !

----------->下一期, 将展示 模拟实现 vector 底层原理 !敬请期待 !😊😊


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

L8iEHH07GzZb