当前位置:网站首页>Chapter VIII vector of STL
Chapter VIII vector of STL
2022-07-19 09:32:00 【qq_ forty-three million two hundred and five thousand two hundr】
1 vector Basic concepts

vector Container iterators are iterators that support random access
2 vector Constructors
#include <iostream>
using namespace std;
#include <vector>
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//1. No arguments structure
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//2. Interval construction
vector<int> v2(v1.begin(), v1.end());
printVector(v2);
//3. Structure has 10 individual 100 Array of
vector<int> v3(10, 100);
printVector(v3);
//4. Copy structure
vector<int> v4(v3);
printVector(v4);
}
int main()
{
test01();
return 0;
}3 vector Assignment operation
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// Assignment operation
void test01()
{
//1. Loop assignment
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//2. Equal sign assignment
vector<int>v2;
v2 = v1;
printVector(v2);
//3. Interval assignment
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
//4. take 10 individual 100 copy assignment
vector<int>v4;
v4.assign(10, 100);
printVector(v4);
}
int main()
{
test01();
return 0;
}
summary : vector The assignment method is relatively simple , Use operator=, perhaps assign Fine .
4 vector Capacity and size
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 It's empty " << endl;
}
else
{
cout << "v1 Not empty " << endl;
cout << "v1 The capacity of = " << v1.capacity() << endl;
cout << "v1 Size = " << v1.size() << endl;
}
//resize Re size , If specified larger , The default with 0 Fill in new location , You can replace the default padding with an overloaded version
v1.resize(15,10);
printVector(v1);
//resize Re size , If specified smaller , Excess elements are deleted
v1.resize(5);
printVector(v1);
}
int main()
{
test01();
return 0;
}5 vector Insert and delete
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// Insert and delete
void test01()
{
vector<int> v1;
// Tail insertion
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
printVector(v1);
// Deletion at the end
v1.pop_back();
printVector(v1);
// Insert
v1.insert(v1.begin(), 100);
printVector(v1);
v1.insert(v1.begin(), 2, 1000);
printVector(v1);
// Delete
v1.erase(v1.begin());
printVector(v1);
// Empty
v1.erase(v1.begin(), v1.end()); // Method 1
v1.clear(); // Method 2
printVector(v1);
}
int main()
{
test01();
return 0;
}6 vector Data access
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
//1. Use [] visit
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
//2. Use at visit
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout << "v1 The first element of is : " << v1.front() << endl;
cout << "v1 The last element of is : " << v1.back() << endl;
}
int main()
{
test01();
return 0;
}7 vector Exchange containers
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;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printVector(v2);
// Exchange containers
cout << " After exchange " << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);
}
void test02()
{
vector<int> v;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
}
cout << "v The capacity of is :" << v.capacity() << endl;
cout << "v The size is :" << v.size() << endl;
v.resize(3);
cout << "v The capacity of is :" << v.capacity() << endl;
cout << "v The size is :" << v.size() << endl;
// Shrink memory
vector<int>(v).swap(v);
// Anonymous object
cout << "v The capacity of is :" << v.capacity() << endl;
cout << "v The size is :" << v.size() << endl;
}
int main()
{
test01();
test02();
return 0;
}8 vector Reserve space
void test01()
{
vector<int> v;
// Reserve space
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();
return 0;
}边栏推荐
- C语言基础篇 —— 2-3 指针与数组
- 【洛谷】P2357 守墓人
- 组件间的相互访问
- Set the ID field to increase automatically when creating tables in SQL Server (Navicat demo)
- 【愚公系列】2022年7月 Go教学课程 012-强制类型转换
- Pytorch框架之优化器 Optimizer
- Fundamentals of C language -- 2-3 pointers and arrays
- SAP Fiori 的附件处理(Attachment handling)
- OLED displays how to understand the character sizes of 12*6, 16*8, 24*12, etc
- 代码随想录:刷题记录(更新中)
猜你喜欢

MySQL 用户管理

Set the ID field to increase automatically when creating tables in SQL Server (Navicat demo)

Makefile中在命令前加上- 则忽略该命令产生的错误,继续执行下一条命令

岚图梦想家的产品力到底如何?

组件化高级--插槽

MySQL 视图

【C语言】自定义类型初阶知识点

Latest fruit flstudio20.9 low version upgrade high version tutorial

银河麒麟v10-arm版离线安装Portainer

Fundamentals of C language -- 2-1 pointer and wild pointer
随机推荐
如何快速计算生成模型的FID、IS、sFID、Precision、Recall等关键评价指标?
Makefile中在命令前加上- 则忽略该命令产生的错误,继续执行下一条命令
【ACWing】947. text editor
记忆 lda LDA in blas level-3 SGEMM cublasGemmex cubulasSgemm
Interview questions - design test cases for:: memcpy function
How is MySQL data stored on disk?
C51 常见数据类型详解
代码随想录:刷题记录(更新中)
Express
Vector容器的系列操作( 详解 )
OpenCV模板
每日模型系列:2022.07.11
Flink小知识--任务调度slot的配置 slotSharingGroup
焱融科技入选北京市 2022 年度“专精特新”,领航混合云文件存储
AnyControl Demo演示
pip和pip3的区别用法详解
【愚公系列】2022年7月 Go教学课程 012-强制类型转换
Codeworks 5 questions per day (average 1500) - day 17
OLED显示如何理解 12*6、16*8、24*12等字符大小
面試題-給::memcpy函數設計測試用例