C++ vector container

Article directory

    • vector container
      • basic concept
      • Constructor
      • Assignment operation
      • size and capacity
      • Insertion and deletion
      • data access
      • Interchangeable container
      • Reserved space

vector container

Basic concepts

The vector data structure is very similar to an array, also called a single-ended array

The difference between vector and ordinary array: The difference is that array is a static space, while vector can be dynamically expanded.

Dynamic expansion: It does not follow the new space after the original space, but finds a larger memory space, and then copies the original data to the new space to release the original space.

Constructor

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

void printfv(vector<int> & amp;v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it + + ) {
cout << *it << ' ';
}
cout << endl;

}
//Constructor of vector
void test01() {
vector<int> v1; //Default construction

for (int i = 0; i < 10; i + + ) {
v1.push_back(i);
}

printfv(v1);

vector<int>v2(v1.begin(), v1.end());//Interval structure
printfv(v2);

vector<int>v3(10, 100);//Constructed by n elem methods, 10 100;
printfv(v3);

vector<int>v4(v3);//copy construction
printfv(v4);
}
int main()
{
test01();
}

Assignment operation

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

void printfv(vector<int> & amp;v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it + + ) {
cout << *it << ' ';
}
cout << endl;

}
//Assignment of vector
void test01() {
vector<int> v1;

for (int i = 0; i < 10; i + + ) {
v1.push_back(i);
};
printfv(v1);

vector<int>v2 = v1; //Equal sign = assignment
printfv(v2);


vector<int>v3;//assign interval assignment
v3.assign(v1.begin(), v1.end());
printfv(v3);

vector<int>v4;//n elem assignments
v4.assign(5, 100);
printfv(v4);
}
int main()
{
test01();
}

Size and Capacity

Determine whether it is empty – empty();

Returns the number of elements – size();

Returns the container capacity – capacity();

Resize – resize();

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

void printfv(vector<int> & amp;v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it + + ) {
cout << *it << ' ';
}
cout << endl;

}
//Vector size and capacity
void test01() {
vector<int> v1;

for (int i = 0; i < 10; i + + ) {
v1.push_back(i);
};
printfv(v1);
int a = v1.empty();//Whether it is empty 1=empty 0=not empty
int b = v1.size();//size
int c = v1.capacity();//Capacity

v1.resize(15);//Respecify it to be longer than the original size, default 0 padding
v1.resize(15, 100);//Respecify it to be longer than the original size, specify 100 padding
v1.resize(6);//Resize it to be smaller than the original size and tell the excess part
printfv(v1);
}
int main()
{
test01();
}

Insertion and deletion

push_back(ele); //Insert element ele at the end
pop_back(); //Delete the last element
insert(const_iterator pos, ele); //The iterator points to position pos to insert element ele
insert(const_iterator pos, int count,ele);//The iterator points to position pos and inserts count elements ele
erase(const_iterator pos); //Delete the element pointed to by the iterator
erase(const_iterator start, const_iterator end);//Delete the elements between the iterator from start to end
clear(); //Delete all elements in the container
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

void printfv(vector<int> & amp;v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it + + ) {
cout << *it << ' ';
}
cout << endl;

}
//Insertion and deletion of vector
void test01() {
vector<int> v1;

v1.push_back(10);//Tail insertion method
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
v1.push_back(60);
v1.push_back(70);
printfv(v1);

v1.pop_back();//Tail deletion method
printfv(v1);

v1.insert(v1.begin(), 100);//Use iterator to insert
v1.insert(v1.begin(), 2,1000);//Use iterator to insert. The overloaded version can insert multiple data
printfv(v1);

v1.erase(v1.begin());//Use iterator to delete
printfv(v1);

v1.erase(v1.begin(),v1.end());//Use iterator to delete a region

v1.clear();//Clear all
printfv(v1);
\t
}
int main()
{
test01();
}

Data access

at(int idx); //Return the data pointed to by index idx
operator[]; //Return the data pointed to by index idx
front(); //Return the first data element in the container
back(); //Return the last data element in the container
//Vector data access
void test01() {
vector<int> v1;
for (int i = 0; i < 10; i + + ) {
v1.push_back(i);
};

for (int i = 0; i < 10; i + + ) {
cout << v1[i] << ' ';//Reading method 1 []
cout << v1.at(i) << ' ';//Reading method two at()
 };
\t
cout << v1.front();//Getting the first element

cout << v1.back();//Getting the first element
}

Interchangeable containers

swap(a,b);

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

void printfv(vector<int> & amp; v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it + + ) {
cout << *it << ' ';
}
cout << endl;
}

//vector container interchange
void test01() {
vector<int> v1;
for (int i = 0; i < 10; i + + ) {
v1.push_back(i);
};
\t
vector<int>v2;
for (int i = 10; i > 0; i--) {
v2.push_back(i);
}

//1.Basic usage
v1.swap(v2);
printfv(v1);
printfv(v2);

\t
}

//2. Practical use: clever use of swap can shrink memory space
void test02() {
vector<int>v;
for (int i = 0; i < 10000; i + + )
{
v.push_back(i);
}
cout << "Capacity of v:" << v.capacity() << endl;

v.resize(3); //Resize
cout << "The size of v:" << v.size() << endl;
cout << "Capacity of v:" << v.capacity() << endl;//The space occupied remains unchanged

vector<int>(v).swap(v);//Smart use of swap to shrink memory
cout << "The size of v:" << v.size() << endl;
cout << "Capacity of v:" << v.capacity() << endl;

}
int main()
{
test02();
}

Analysis of shrinking memory space

vector<int>(v).swap(v);//Smart use of swap to shrink memory

vector<int>(v)//Anonymous object
.swap(v)//Exchange

Reserved space

If the amount of data is large, the size can be reserved directly to avoid re-opening.

reserve(int len); //Reserve space
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

void printfv(vector<int> & amp; v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it + + ) {
cout << *it << ' ';
}
cout << endl;
}

//vector container interchange
void test01() {
vector<int> v;

int num = 0;//Calculate the number of openings
int* p = NULL;
v.reserve(100000);//Use reserve to reserve space
for (int i = 0; i < 100000; i + + ) {
v.push_back(i);

if (p != & amp;v[0]) {//The first address changes
p = &v[0];
num + + ;
}
}
cout << num;
}

int main()
{
test01();
}