STL container class

STL

1. First introduction to STL

1.1 Iterator

1.1.1 Native pointers are also iterators

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
void test01() {
	int arr[5] = { 1,2,3,4,5 };
	int* p = arr;
	for (int i = 0; i < 5; i++) {
		cout << arr[i] << endl;
		cout << *(p+i) << endl;
	}
};
int main() {
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501182726266

1.1.2 Loop iteration

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int i) {
    
    

	cout <<i << endl;
}
void test02() {
    
    
	vector<int >v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	vector<int>::iterator itbegin = v.begin();
	vector<int>::iterator itend = v.end();
	while (itbegin != itend) {
    
    
		cout << *itbegin << endl;
		itbegin++;
	}
	for (vector<int>::iterator i = itbegin; i!=v.end();i++) {
    
    
		cout << *i << endl;
	}
	for_each(v.begin(), v.end(), print);
}


int main() {
    
    
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501192746580

1.1.3 Custom data structure

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Person {
public:
	Person(string name,int age) {
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};
void test03() {
	vector<Person> v;
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);
	Person p6("fff", 60);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	v.push_back(p6);
	for (vector<Person>::iterator it = v.begin(); it!=v.end(); it++) {
		cout << (*it).name<< endl;
	}
}
int main() {
	test03();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501195143536

1.2 string container

1.2.1 string initialization

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test01() {
	string str;// 初始化对象
	string str2(str); // 用另外一个对象初始化
	string str3 = "abc"; // 把值赋值给当前字符串
	string str4(10, 'q'); // 用n个字符赋值给当前字符串
	cout << "str3= " << str3 << endl;
	cout << "str4= " << str4 << endl;
}

int main() {
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501201634698

1.2.2 String assignment

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test02() {
	string str;
	str = "abc";

	str.assign("abcde", 3);
	cout << str << endl;

	string str2;
	str2.assign(str); //字符串赋值
	cout << str2 << endl;
	str2.assign(str, 0, 2); // 将s从start开始n个字符赋值给字符串
	cout << str2 << endl;

}

int main() {
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501201942683

1.2.3 at value check

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test03() {
	string str="hello world!";
	for (int i = 0; i < str.size(); i++) {
		cout << str[i] << endl;
	}
	// [] 和at 区别,[] 访问越界直接挂掉,at访问越界,抛出out_of_range 异常
	try {
		cout << str.at(100) << endl;
	}
	catch (out_of_range& e) {
		cout << e.what() << endl;
	}
	catch (...) {
		cout << "异常捕获"<<endl;
	}

}

int main() {
	test03();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501203038084

1.2.4 Add, delete, modify and check

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test04() {
	// 字符串拼接
	string  str1 = "我";
	string str2 = "爱天安门";
	cout <<"1:" << str1 + str2 << endl;
	cout <<"2:" << str1.append(str2) << endl;

	string str = "abcdefgde";
	int pos = str.find("de");

	if (pos == -1) {
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到了字符串,位置为:" << pos << endl;
	}
	str.replace(1, 3, "11111");// 替换从pos开始n个字符的字符串为str
	cout << str << endl;
}

int main() {
	test04();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501204621438

1.2.5 String comparison

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test05() {
	string str1 = "abcde";
	string str2 = "abcdef";
	if (str1.compare(str2) == 0) {
		cout << "str1==str2" << endl;
	}
	else if (str1.compare(str2) > 0) {
		cout << "str1>str2" << endl;
	}
	else
	{
		cout << "str1<str2" << endl;

	}
}

int main() {
	test05();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501205006747

1.2.6 String index

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test06() {
	string email = "[email protected]";
	int pos = email.find("@");
	// [start:end]
	string userName = email.substr(0, pos);
	cout << userName << endl;
}

int main() {
	test06();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501205256320

1.2.7 Parsing strings

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test07() {
	string str = "www.baidu.com";
	vector<string> v;
	int start = 0;
	int pos = -1;
	while (true) {
		pos = str.find(".", start);
		if (pos == -1) {
			string tempStr = str.substr(start, str.size() - start);
			v.push_back(tempStr);
			break;
		}
		string tempStr = str.substr(start, pos - start);
		v.push_back(tempStr);
		start = pos + 1;
	}
	for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << endl;
	}
		
}

int main() {
	test07();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501213945866

1.2.8 Insertion and deletion

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test08() {
	string str = "hello";
	str.insert(1, "111");

	cout << str << endl;

	str.erase(1, 3);// 从pos开始的n个字符
	cout << str << endl;

}

int main() {
	test08();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501214139969

1.2.9 Conversion between char and string

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test09() {
	const char* str = "abcd";
	// 转str
	string s(str);

	//str 转const
	const char* str2 = s.c_str();
	// const char* 可以隐式转换string,反之不可

}
int main() {
	test09();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501214428988

1.2.10 String conversion

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test10() {
	string s = "abcdefg";
	char& a = s[2];
	char& b = s[3];

	a = '1';
	b = '2';
	cout << s << endl;
	cout << (int*)s.c_str() <<endl;
	s = "ppppppppppppppp";

	cout << s << endl;
	cout << (int*)s.c_str() << endl;


}
int main() {
	test10();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501214651331

1.3. vector container

1.3.1 capacity

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++){
		v.push_back(i);
		cout << v.capacity() << endl;  // v.capacity()容器的容量
	}
}

int main() {
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501225522551

1.3.2 Assignment

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

void printVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	vector<int>v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	//vector& operator=(const vector & vec);//重载等号操作符
	//assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
	//assign(n, elem);//将n个elem拷贝赋值给本身。
	//vector& operator=(const vector & vec);//重载等号操作符
	vector<int>v2(v1.begin(), v1.end());
	printVector(v2);
	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);
	vector<int>v4(10, 100);
	printVector(v4);
	cout << "v3和v4互换后:" << endl;
	v3.swap(v4);
	printVector(v3);
	printVector(v4);
}

int main() {
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501230444646

1.3.3 Status judgment and operation

  • size();//returns the number of elements in the container
  • empty(); // Determine whether the container is empty
  • resize(int num);//Respecify the length of the container to num. If the container becomes longer, fill the new position with the default value. If the container becomes shorter, elements at the end that exceed the length of the container are removed.
  • resize(int num, elem);//Respecify the length of the container to num. If the container becomes longer, fill the new position with the elem value. If the container becomes shorter, the elements at the end that exceed the length of the container are removed.
  • capacity();//Container capacity
  • reserve(int len);//The container reserves len elements in length, the reserved position is not initialized, and the elements are inaccessible.
  • at(int idx); //Return the data pointed to by index idx. If idx is out of bounds, an out_of_range exception will be thrown.
  • operator[];//Returns the data pointed to by index idx. When out of bounds, an error will be reported directly during operation.
  • front();//Return the first data element in the container
  • back();//Return the last data element in the container
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void printVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test03()
{
	vector<int>v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	if (v1.empty())
	{
		cout << "v1为空" << endl;
	}
	else
	{
		cout << "v1不为空 ,大小为: " << v1.size() << endl;
	}

	v1.resize(10, 100); //第二个参数代表默认填充值

	printVector(v1);

	v1.resize(3);

	printVector(v1);

	cout << "v1的front = " << v1.front() << endl;
	cout << "v1的back  = " << v1.back() << endl;

}

int main() {
	test03();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501231011665

1.3.4 Insertion and deletion

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void printVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test04()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	v.insert(v.begin(), 100);

	printVector(v);

	v.push_back(1000);

	printVector(v);

	v.pop_back();

	printVector(v);


	v.erase(v.begin());
	printVector(v);

	//v.erase(v.begin(), v.end()); 等价于  v.clear();
	v.clear();
	//v.erase(v.begin(), v.end());
	printVector(v);

}



int main() {
	test04();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230501231145772

1.4 and

1.4.1 Constructor

  • deque deqT; //Default construction form
  • deque(beg, end);//The constructor copies the elements in the [beg, end) interval to itself.
  • deque(n, elem);//The constructor copies n elem to itself.
  • deque(const deque &deq);//Copy constructor.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<deque>
#include<algorithm>
#include<string>
using namespace std;
void printDeque(const deque<int>& d)
{
	//iterator普通迭代器
	//reverse_iterator 反转迭代器
	//const_iterator  只读迭代器

	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//*it = 1000;
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(40);
	deque<int>d2;
	d2 = d;
	printDeque(d2);

	if (d2.empty())
	{
		cout << "d2为空" << endl;
	}
	else
	{
		cout << "d2不为空 size = " << d2.size() << endl;
	}
}
void test02() {
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(100);
	d.push_back(200);
	d.push_back(300);
	printDeque(d);
	d.pop_back(); // 尾删
	d.pop_front(); // 头删
	printDeque(d);
	cout << "第一个元素为:" << d.front() << endl;
	cout << "最后一个元素为:" << d.back() << endl;
	
}
int main() {
	test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230502103418754

1.4.2 Assignment operation

  • assign(beg, end);//Copy and assign the data in the [beg, end) interval to itself.
  • assign(n, elem);//Assign n elem copies to itself.
  • deque& operator=(const deque &deq); // overloaded equal sign operator
  • swap(deq);// Swap deq with its own elements
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<deque>
#include<algorithm>
#include<string>
using namespace std;
void printDeque(const deque<int>& d)
{
	//iterator普通迭代器
	//reverse_iterator 反转迭代器
	//const_iterator  只读迭代器

	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//*it = 1000;
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(40);
	deque<int>d2;
	d2 = d;
	printDeque(d2);

	if (d2.empty())
	{
		cout << "d2为空" << endl;
	}
	else
	{
		cout << "d2不为空 size = " << d2.size() << endl;
	}
}
void test02() {
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(100);
	d.push_back(200);
	d.push_back(300);
	printDeque(d);
	d.pop_back(); // 尾删
	d.pop_front(); // 头删
	printDeque(d);
	cout << "第一个元素为:" << d.front() << endl;
	cout << "最后一个元素为:" << d.back() << endl;
	
}
void test03(){
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(100);
	d.push_back(200);
	d.push_back(300);
	d.insert(++d.begin(), 2, 1000);
	printDeque(d);
	d.erase(++d.begin());
	d.erase(++d.begin());
	deque<int>::iterator it1 = d.begin();
	it1 = it1 + 1;
	deque<int>::iterator it2 = d.begin();
	it2 = it2 + 3;
	d.erase(it1, it2);
	printDeque(d);
	d.clear();
	printDeque(d);
}
bool myComare(int v1, int v2) {
	return v1 < v2;
}
int main() {
	// test01();
	// test02();
	test03();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230502103936561

1.4.3 Size operations

  • deque.size();//Return the number of elements in the container
  • deque.empty();//Determine whether the container is empty
  • deque.resize(num);//Respecify the length of the container to num. If the container becomes longer, fill the new position with the default value. If the container becomes shorter, elements at the end that exceed the length of the container are removed.
  • deque.resize(num, elem); //Respecify the length of the container to num. If the container becomes longer, the new position will be filled with the elem value. If the container becomes shorter, the elements at the end that exceed the length of the container will be deleted.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<deque>
#include<algorithm>
#include<string>
using namespace std;
void printDeque(const deque<int>& d)
{
	//iterator普通迭代器
	//reverse_iterator 反转迭代器
	//const_iterator  只读迭代器

	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//*it = 1000;
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(40);
	deque<int>d2;
	d2 = d;
	printDeque(d2);

	if (d2.empty())
	{
		cout << "d2为空" << endl;
	}
	else
	{
		cout << "d2不为空 size = " << d2.size() << endl;
	}
}
void test02() {
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(100);
	d.push_back(200);
	d.push_back(300);
	printDeque(d);
	d.pop_back(); // 尾删
	d.pop_front(); // 头删
	printDeque(d);
	cout << "第一个元素为:" << d.front() << endl;
	cout << "最后一个元素为:" << d.back() << endl;
	
}
void test03(){
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(100);
	d.push_back(200);
	d.push_back(300);
	d.insert(++d.begin(), 2, 1000);
	printDeque(d);
	d.erase(++d.begin());
	d.erase(++d.begin());
	deque<int>::iterator it1 = d.begin();
	it1 = it1 + 1;
	deque<int>::iterator it2 = d.begin();
	it2 = it2 + 3;
	d.erase(it1, it2);
	printDeque(d);
	d.clear();
	printDeque(d);
}
bool myComare(int v1, int v2) {
	return v1 < v2;
}
void test04() {
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_front(100);
	d.push_front(200);
	d.push_front(300);
	// 默认排序从小到大
	//sort(d.begin(), d.end());
	sort(d.begin(), d.end(), myComare);
	printDeque(d);
}
int main() {
	// test01();
	// test02();
	// test03();
	test04();
	system("pause");
	return EXIT_SUCCESS;
}

image-20230502104600016

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<deque>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class Player
{
public:
	Player(string name, int score) {
		this->m_Name = name;
		this->m_Score = score;
	};
	string m_Name; // 姓名
	int m_Score; // 平均分
};
void createPlayer(vector<Player>& v) {
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5; i++) {
		string name = "选手";
		name += nameSeed[i];
		int score = 0;
		Player player(name, score);
		v.push_back(player);
	}
};
void setScore(vector<Player>& v) {
	for (vector<Player>::iterator it = v.begin(); it != v.end(); it++) {
		deque<int> d;
		for (int i = 0; i < 10; i++) {
			int score = rand() % 41 + 60;
			d.push_back(score);
		}
		sort(d.begin(), d.end());
		
		d.pop_back();
		d.pop_front();

		int sum = 0;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++) {
			sum += *dit;
		}
		int avg = sum / d.size();
		it->m_Score = avg;
	}
}
void showScore(vector<Player>&v){
	for (vector<Player>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "姓名:" << (*it).m_Name << "平均分数:" << it->m_Score << endl;
	}

}
int main() {
	srand((unsigned int)time(NULL));
	vector<Player> v;
	createPlayer(v);

	setScore(v);

	showScore(v);
	
	

	system("pause");
	return EXIT_SUCCESS;
}

image-20230502112016665

1.5 Stack container

  • stack& operator=(const stack &stk);//overload equal sign operator
  • push(elem);//Add elements to the top of the stack
  • pop();//Remove the first element from the top of the stack
  • top();//Return the top element of the stack
  • empty(); // Determine whether the stack is empty
  • size();//Return the size of the stack
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <stack>

void test01()
{
	stack<int>S;
	//入栈
	S.push(10);
	S.push(20);
	S.push(30);
	S.push(40);

	cout << "size  = " << S.size() << endl;

	while (!S.empty())
	{
		//访问栈顶元素
		cout << S.top() << endl;
		//出栈
		S.pop();
	}
	cout << "size  = " << S.size() << endl;

}


int main() {

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

image-20230502112311130

1.6 queuequeue

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <queue>
#include <string>
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

void test01()
{
	queue<Person> Q; //队列容器

	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);


	//入队
	Q.push(p1);
	Q.push(p2);
	Q.push(p3);
	Q.push(p4);

	cout << "size = " << Q.size() << endl;

	while ( !Q.empty())
	{
		cout << "队头元素--- 姓名:  " << Q.front().m_Name << " 年龄: " << Q.front().m_Age << endl;
		cout << "队尾元素--- 姓名:  " << Q.back().m_Name << " 年龄: " << Q.back().m_Age << endl;

		//出队
		Q.pop();
	}

	cout << "size = " << Q.size() << endl;

}

int main(){

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

image-20230502112530044

1.7 list container

1.7.1 Constructor

  • list lstT;//list is implemented using a template class, and the default construction form of the object is:
  • list(beg, end);//The constructor copies the elements in the [beg, end) range to itself.
  • list(n,elem);//The constructor copies n elems to itself.
  • list(const list &lst);//Copy constructor.
  • push_back(elem);//Add an element to the end of the container pop_back();//Delete the last element in the container
  • push_front(elem);//Insert an element at the beginning of the container
  • pop_front();//Remove the first element from the beginning of the container
  • insert(pos,elem);//Insert a copy of the elem element at the pos position and return the position of the new data.
  • insert(pos,n,elem);//Insert n elem data at pos position, no return value.
  • insert(pos,beg,end);//Insert the data in [beg,end) range at position pos, no return value.
  • clear();//Remove all data from the container
  • erase(beg,end);//Delete the data in [beg,end) range, and return the position of the next data.
  • erase(pos);//Delete the data at position pos and return the position of the next data.
  • remove(elem);//Delete all elements in the container that match the value of elem.
list<T> lstT;//list采用采用模板类实现,对象的默认构造形式:
list(beg,end);//构造函数将[beg, end)区间中的元素拷贝给本身。
list(n,elem);//构造函数将n个elem拷贝给本身。
list(const list &lst);//拷贝构造函数。

1.7.2 Data element insertion and deletion operations

push_back(elem);//在容器尾部加入一个元素
pop_back();//删除容器中最后一个元素
push_front(elem);//在容器开头插入一个元素
pop_front();//从容器开头移除第一个元素
insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
clear();//移除容器的所有数据
erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos);//删除pos位置的数据,返回下一个数据的位置。
remove(elem);//删除容器中所有与elem值匹配的元素。

1.7.3 List size operation

size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(num);//重新指定容器的长度为num,
若容器变长,则以默认值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem);//重新指定容器的长度为num,
若容器变长,则以elem值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。

1.7.4 Access to list data

front();//返回第一个元素。
back();//返回最后一个元素。

1.7.5 List reverse sorting

reverse();//反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
sort(); //list排序

1.8 set/multiset

  • The characteristics of Set are. All elements are automatically sorted based on the element's key value. Unlike map, the elements of Set can have both real values ​​and key values. The elements of Set are both key values ​​and real values. Set does not allow two elements to have the same key value.
  • The characteristics and usage of multiset are exactly the same as set. The only difference is that it allows duplication of key values. The underlying implementation of set and multiset is a red-black tree, which is a type of balanced binary tree.

1.8.1 Constructor

set<T> st;//set默认构造函数:
mulitset<T> mst; //multiset默认构造函数: 
set(const set &st);//拷贝构造函数

1.8.2 Assignment operation

set& operator=(const set &st);//重载等号操作符
swap(st);//交换两个集合容器

1.8.3 Set size operation

size();//返回容器中元素的数目
empty();//判断容器是否为空

1.8.4 Insertion and deletion operations

insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);//删除容器中值为elem的元素。

1.8.5 Search operation

find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//查找键key的元素个数
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。

1.9 map/multimap container

1.9.1 Basic concepts of map/multimap

The characteristic of Map is that all elements are automatically sorted according to the key value of the element. All elements of Map are pairs, with both real values ​​and key values. The first element of a pair is regarded as a key value, and the second element is regarded as a real value. Map does not allow two elements to have the same key value.

The key value of the map is related to the arrangement rules of the map elements. Any change of the map key value will seriously damage the map organization. If you want to modify the actual value of an element, you can.

Map and list have some of the same properties. When adding or deleting its container elements, all iterators before the operation are still valid after the operation is completed. Of course, the iterator of the deleted element must be exception.

Multimap and map operations are similar, the only difference is that multimap key values ​​can be repeated.

Both Map and multimap use red-black trees as the underlying implementation mechanism.

1.9.2 Constructor

map<T1, T2> mapTT;//map默认构造函数: 
map(const map &mp);//拷贝构造函数

1.9.3 Assignment

map& operator=(const map &mp);//重载等号操作符
swap(mp);//交换两个集合容器

1.9.4 Map size operation

size();//返回容器中元素的数目
empty();//判断容器是否为空

1.9.5 Insert element operation

map.insert(...); //往容器插入元素,返回pair<iterator,bool>
map<int, string> mapStu;
// 第一种 通过pair的方式插入对象
mapStu.insert(pair<int, string>(3, "小张"));
// 第二种 通过pair的方式插入对象
mapStu.inset(make_pair(-1, "校长"));
// 第三种 通过value_type的方式插入对象
mapStu.insert(map<int, string>::value_type(1, "小李"));
// 第四种 通过数组的方式插入值
mapStu[3] = "小刘";
mapStu[5] = "小王";

1.9.6 map deletion operation

clear();//删除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(keyElem);//删除容器中key为keyElem的对组。

1.9.7 map search operation

find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end();
count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。

Guess you like

Origin blog.csdn.net/weixin_42917352/article/details/130468454