Vector (code runtime environment: VS2017)
The data structure looks like an array, also known as a "single-ended array", but it is a little different from an array.
Its space is dynamically distributed, like a dynamic array.
Its dynamic expansion method is not to increase in the original position, but to find a larger space and then copy the original element here to release the original space.
vector iterators support random access.
Actual use case
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>c;
c.push_back(10);//容器中插入数据,尾插法
c.push_back(20);
c.push_back(30);
for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
{
cout << *it << endl;
}
return 0;
}
Method 2:
Use the for_each traversal function:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void pringtf(int i)
{
cout << i << endl;
}
int main()
{
vector<int>c;
c.push_back(10);//容器中插入数据,尾插法
c.push_back(20);
c.push_back(30);
/*
for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
{
cout << *it << endl;
}
*/
for_each(c.begin(), c.end(), pringtf);
return 0;
}
The underlying implementation of for_each() is shown in the figure below:
When the data in the vector is another type of data:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Person
{
public:
string name;
int age;
Person(string name, int age)
{
this->name = name;
this->age = age;
}
};
int main()
{
vector<Person>v;
Person p1("aaa",20);
Person p2("bbb", 21);
Person p3("ccc", 22);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it).name << " ";
cout << "年龄:" << (*it).age<< " ";
}
return 0;
}
Running result:
The vector container nested container
is similar to the operation of a two-dimensional array.
See code implementation:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
//创建小容器
vector<vector<int>>v;
vector<int>v1;
vector<int>v2;
vector<int>v3;
//小容器添加数据
for (int i = 0; i < 3; i++)
{
v1.push_back(i + 1);
v2.push_back(i + 1);
v3.push_back(i + 1);
}
//放入大容器
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
{
//注意看it,此时的it指的是<vector<int>>,类似的情况下it指的都是<>内的内容
for (vector<int>::iterator it1 = (*it).begin(); it1 != (*it).end(); it1++)
{
cout << *it1<< " ";
}
cout << endl;
}
}
int main()
{
test01();
return 0;
}
operation result:
String (code running environment: VS2017)
Essence:
string is a C++ style string, and string is essentially a class.
* The difference between string and char
1. char * is a pointer
2. string is a class, which encapsulates char * inside, manages this string, and is a container of char *.
Features:
String internally encapsulates many member methods such as search, insert, replace, etc.
String manages the memory allocated by char *, so there is no need to worry about copying out of bounds or value out of bounds, etc., and the class is responsible for it.
The actual case of the constructor of string
:
#include<iostream>
#include<string>
using namespace std;
void test02()
{
string s1;
const char* str = "hellow word";
string s2(str);
string s3(s2);
string s4(5, 'b');
cout << "s1=" << s1 << endl;
cout << "s2=" << s2 << endl;
cout << "s3=" << s3 << endl;
cout << "s4=" << s4 << endl;
}
int main(void)
{
test02();
}
Running result: The actual case of
assignment operation of string
:
#include<iostream>
#include<string>
using namespace std;
void test03()
{
string str1;
str1 = "hellow word";
string str2,str3,str4,str5,str6,str7;
str2 = str1;
str3 = 'a';
str4.assign("hellow c++");
str5.assign("hellow c++", 5);
str6.assign(str1);
str7.assign(5,'c');
cout << "str1=" << str1 << endl;
cout << "str2=" << str2 << endl;
cout << "str3=" << str3 << endl;
cout << "str4=" << str4 << endl;
cout << "str5=" << str5 << endl;
cout << "str6=" << str6 << endl;
cout << "str7=" << str7 << endl;
}
int main(void)
{
test03();
}
Running result: The actual case
of string string splicing
:
#include<iostream>
#include<string>
using namespace std;
void test04()
{
string str3 = "I";
string str2;
str3.append("love");
str3.append("game abcde", 4);
str2.append(str3);
str3.append(str2, 2, 5);
cout << "str2=" << str2 << endl;
cout << "str3=" << str3 << endl;
}
int main(void)
{
test04();
}
Running results:
String's find and replace
string comparison
Actual case:
#include<iostream>
#include<string>
using namespace std;
void test04()
{
string str3 = "I";
string str2;
str3.append("love");
str3.append("game abcde", 4);
str2.append(str3);
str3.append(str2, 2, 5);
//cout << "str2=" << str2 << endl;
//cout << "str3=" << str3 << endl;
int m;
m = str2.compare(str3);
cout << m << endl;
}
int main(void)
{
test04();
}
Running results:
string string insertion and deletion
to get the desired string