Common operations of C++ standard template library vector
- 2020-06-12 10:14:37
- OfStack
1:
vector is the C++ standard template library. It is a container with an array as the bottom layer and continuous memory.
The namespace is std and the header file belongs to is < vector > Note: No < vector.h >
When vector stores data, it allocates 1 storage space. If it continues to store data and the allocated space is full, it allocates a larger block of memory and copies the original data to continue to store. These performance will also be damaged to a certain extent
2: Common operation
Capacity:
a.vector Size: vector.size() Actual size of memory occupied by ES26en. capacity()Modification:
a. tail add element: ES33en. push_back() b. tail delete element: vector. pop_back() c. Swap two vector elements: ES42en.swap () d. Empty the vector element: ES46en.clear () e. Removes the specified element: ES49en. erase(it)The iterator:
Start pointer: ES56en. begin() Note: The next position of the last element, similar to NULL, is not the last element of the containerAccess elements:
a. Subscript access: vector[1] // does not check for overbounds b. at method access: ES69en. at(1) // Automatically checks if a boundary is crossed and throws an exception c. Access the first element: ES72en.front () d. Access the last element: ES75en. back()3: store
Simple storage
// storage 1
vector<int> v1(10);
for (int i=0; i<10; i++)
{
v1[i] = i;
}
// storage 2
vector<int> v2;
for (int i=0; i<10; i++)
{
v2.push_back(i);
}
Stores structs and structs Pointers
struct Student
{
char name[32];
int age;
};
// Storage structure
vector<Student> vStu1;
for (int i=0; i<10; i++)
{
Student stu;
strcpy(stu.name, "woniu201");
stu.age = 30 + i;
vStu1.push_back(stu);
}
// Store the pointer to the structure
vector<Student*> vStu2;
for (int i=0; i<10; i++)
{
Student* pStu = (Student*)malloc(sizeof(Student));
strcpy(pStu->name, "woniu201");
pStu->age = 30 + i;
vStu2.push_back(pStu);
}
4: vector traversal
vector<int> v;
for (int i=0; i<100; i++)
{
v.push_back(i);
}
// Traverse the way 1
for (int i=0; i<100; i++)
{
int& a = v[i];
printf("%d ", a);
}
// Traverse the way 2
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
int&a = *it;
printf("%d ", a);
}
5: sorting
Sort the vector shaping
#include "stdlib.h"
#include <vector>
#include <algorithm>
using namespace std;
// The ascending comparison function
int compare1(const int &a, const int &b)
{
return a < b;
}
// Descending comparison functions
int compare2(const int &a, const int &b)
{
return a > b;
}
int main()
{
vector<int> v;
for (int i=0; i<10; i++)
{
v.push_back(rand() % 10);
}
// Traverse the output
printf(" Pre-sorted data: ");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
}
// Ascending order
sort(v.begin(), v.end(), compare1);
// Traverse the output
printf("\n Ascending data: ");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
}
// Descending order
sort(v.begin(), v.end(), greater<int>());
// Traverse the output
printf("\n Descending data: ");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
}
getchar();
return 1;
}
Sort the variables that hold the class members
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
Student(string n, int c) :name(n), core(c) {}
string name;
int core;
};
// The ascending comparison function
bool compare1(const Student& s1, const Student& s2)
{
return s1.core < s2.core;
}
// Descending comparison functions
bool compare2(const Student& s1, const Student& s2)
{
return s1.core > s2.core;
}
int main()
{
vector<Student> v;
Student s1("aaaa", 97);
Student s2("bbbb", 99);
Student s3("cccc", 95);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
printf(" Pre-sorted data: \n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
}
// Ascending order
sort(v.begin(), v.end(), compare1);
printf("\n Ascending data: \n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
}
// Descending order
sort(v.begin(), v.end(), compare2);
printf("\n Data after descending order: \n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
}
getchar();
return 1;
}
6: to find the
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if(it != v.end())
{
cout << "found";
}
else
{
cout << "not found";
}
7: remove
for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
{
if(*it == 8)
{
it = v.erase(it);//it will ++1 time
it--; // After deleting -- Otherwise the cycle will eventually cross the line
}
}
8: Free memory
Store plastic vector release
// To store the integer
vector<int> v;
for (int i=0; i<100; i++)
{
v.push_back(i);
}
// Free memory
{
vector<int> vEmpty;
v.swap(vEmpty);
}
Store structure vector release
// Storage structure
vector<Student> vStu1;
for (int i=0; i<10; i++)
{
Student stu;
strcpy(stu.name, "woniu201");
stu.age = 30 + i;
vStu1.push_back(stu);
}
// Free memory
{
vector<Student>
}
vector<Student> vEmpty;
vStu1.swap(vEmpty);
Store structure pointer vector release
// Store the pointer to the structure
vector<Student*> vStu2;
for (int i=0; i<10; i++)
{
Student* pStu = (Student*)malloc(sizeof(Student));
strcpy(pStu->name, "wangpengfei");
pStu->age = 30 + i;
vStu2.push_back(pStu);
}
// Free memory
for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++)
{
if (NULL != *it)
{
delete *it;
*it = NULL;
}
}
conclusion