容器C++ ——STL常用容器

news2025/4/3 5:52:50

string容器

 string构造函数

#include<iostream>
using namespace std;
#include<string.h>
void test01() {
	string s1;//默认构造

	const char* str = "hello world";
	string s2(str);//传入char*
	cout << "s2=" << s2 << endl;

	string s3(s2);//传入string
	cout << "s3=" << s3 << endl;

	string s4(5, 'a');//5个a
	cout << "s4=" << s4 << endl;
}
int main() {
	test01();
	return 0;
}

 赋值操作

#include<iostream>
using namespace std;
#include<string.h>
//赋值操作
void test() {
	string str1;
	str1 = "hello";//=赋值
	cout << str1 << endl;

	string str2;
	str2 = str1;
	cout << str2 << endl;

	string str3;
	str3 = 'a';//可以把单个字符赋值给字符串
	cout << str3 << endl;

	string str4;
	str4.assign("hello c++");//assign赋值
	cout << str4 << endl;

	string str5;
	str5.assign("study", 3);//把字符串的前3个字符赋值给str5
	cout << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << str6 << endl;

	string str7;
	str7.assign(6, 'w');
	cout << str7 << endl;
}
int main() {
	test();
	return 0;
}

字符串拼接

字符串末尾追加字符串

#include<iostream>
using namespace std;
#include<string.h>
//字符串拼接
void test() {
	string s1="hello";
	s1 += " world";//追加字符串
	cout << s1 << endl;

	s1 += '!';//追加字符
	cout << s1 << endl;

	string s2 = " study";
	s1 += s2;//追加字符串
	cout << s1 << endl;

	s1.append(" up up");
	cout << s1 << endl;

	s1.append(" eat food", 3);//把前n个字符拼接进来
	cout << s1 << endl;

	s1.append(s2);
	cout << s1 << endl;

	s1.append(s2, 0, 2);//从第0个位置截取2个字符,追加上
	cout << s1 << endl;
}
int main() {
	test();
	return 0;
}

string查找和替换

void test() {
	string s1 = "abcdefgcd";
	int pos = s1.find("cd");//3  返回d的位置。没有这个字串返回-1
	if (pos == -1) {
		cout << "未找到字符串" << endl;
	}
	else {
		cout << pos << endl;//pos=2   position 位置
	}

	//rfind
	//rfind从右往左查找,find从左往右查找
	pos = s1.rfind("cd");//7
	cout << pos << endl;
}

//替换
void test02() {
	string s1 = "abcdefg";
	s1.replace(1, 3, "1111");//从1号位置起 3个字符,替换成1111
	cout << s1 << endl;//a1111efg
}

string的字符串比较

void test() {
	string s1 = "hello";
	string s2 = "hello";
	if (s1.compare(s2) == 0)
		cout<<"相等" << endl;
	else if(s1.compare(s2) > 0)
		cout << "s1>s2" << endl;
	else if(s1.compare(s2)<0)
		cout << "s1<s2" << endl;
}

string字符读取

void test() {
	string s1 = "hello";
	//cout << s1 << endl;
	for (int i = 0; i < s1.size(); i++)
		cout << s1[i] << " ";
	cout << endl;
	for (int i = 0; i < s1.size(); i++)
		cout << s1.at(i) << " ";
	s1[2] = 'a';//可以修改字符
	s1.at(1) = 'b';
}

string插入和删除

void test() {
	string str = "hello";
	str.insert(1, "111");//插入111
	cout << str << endl;

	str.erase(1, 3);//删除111.从1号位置删除3个字符
}

string字串

从字符串中获取想要的字串

void test02() {
	string email = "zhangsan@qq.com";
	//从邮箱中获取用户名
	int pos = email.find("@");
	cout << email.substr(0, pos) << endl;
}

vector  (可动态扩展的单端数组)

相似于数组,也称为单端数组。

和数组不同的是,vector可以动态扩展

#include<iostream>
using namespace std;
#include<vector>//vector头文件
//iterator迭代器
void printVector(vector<int>&v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	cout << endl;
}

void test() {
	vector<int>v1;//默认构造 无参构造
	for (int i = 0; i < 10; i++)
		v1.push_back(i);
	printVector(v1);

	//通过区间方式进行构造
	vector<int>v2(v1.begin(), v1.end());
	printVector(v2);

	//n个 elem方式构造
	vector<int>v3(3, 5);//3个5
	printVector(v3);

	//拷贝构造
	vector<int>v4(v3);
	printVector(v4);
}
int main() {
	test();
	return 0;
}

vector赋值操作

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

void test() {
	vector<int>v1;
	for (int i = 0; i < 10; i++)
		v1.push_back(i);
	printVector(v1);

	//operator= 赋值
	vector <int>v2;
	v2 = v1;
	printVector(v2);

	//assign
	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	//n个 elem方式赋值
	vector<int>v4;
	v4.assign(5, 100);//5个100
	printVector(v4);
}

vector容器  容量和大小

#include<iostream>
using namespace std;
#include<vector>//vector头文件

void printVector(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	cout << endl;
}
void test() {
	vector<int>v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	printVector(v1);

	if (v1.empty())
		cout << "v1为空" << endl;
	else {
		cout << "v1不为空" << endl;
		cout << v1.capacity() << endl;
		cout << v1.size() << endl;
	}

	//重新指定大小 
	v1.resize(15,100);//指定用100填充   v1.resize(15);   
	printVector(v1);//如果重新指定的比原来长,默认用0填充

	v1.resize(5);//指定的比原来短,超出的部分会删掉
	printVector(v1);
}

int main() {
	test();
	return 0;
}

 插入和删除

void test() {
	vector<int>v1;
	//尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	//遍历
	printVector(v1);

	//尾删
	v1.pop_back();
	printVector(v1);

	//插入 第一个参数是迭代器
	v1.insert(v1.begin(), 100);//起始迭代器
	printVector(v1);

	v1.insert(v1.begin(), 2, 1000);//在起始迭代器处插入2个1000
	printVector(v1);

	//删除
	v1.erase(v1.begin());//在起始迭代器处删除
	printVector(v1);

	//清空
	//v.clear();
	v1.erase(v1.begin(), v1.end());//提供一个区间
	printVector(v1);
}

vector数据存取

void test() {
	vector<int>v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);//尾插法
	}

	for (int i = 0; i < v1.size(); i++) {
		cout << v1[i] << " ";
	}
	cout << endl;
	
	for (int i = 0; i < v1.size(); i++)
		cout << v1.at(i) << " ";
	cout << endl;

	//获取第一个元素
	cout << v1.front() << endl;

	//获取最后一个元素
	cout << v1.back() << endl;
}

vector互换容器

void test() {
	vector<int>v;
	int num = 0;//统计开辟次数
	int* p = NULL;
	for (int i = 0; i < 10000; i++) {
		v.push_back(i);
		if (p != &v[0]) {
			p = &v[0];
			num++;
		}
	}
	cout << num << endl;
}

deque容器 (双端数组)

#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
		cout << *it << " ";
	cout << endl;
}
void test() {
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);//插数
	}
	printDeque(d1);

	deque<int>d2(d1.begin(), d1.end());
	printDeque(d2);

	deque<int>d3(5, 100);
	printDeque(d3);

	deque<int>d4(d3);
	printDeque(d4);
}
int main() {
	test();
	return 0;
}

赋值

void printDeque(const deque<int>&d) {//const使其只读
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
		cout << *it << " ";
	cout << endl;
}
void test() {
	deque<int>d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);
	}
	printDeque(d1);

	//operator= 赋值
	deque<int>d2;
	d2 = d1;
	printDeque(d2);

	//assign 赋值
	deque<int>d3;
	d3.assign(d1.begin(), d1.end());
	printDeque(d3);

	deque<int>d4;
	d4.assign(5, 6);
	printDeque(d4);
}

deque容器大小

void test() {
	deque<int>d1;
	for (int i = 0; i < 10; i++)
		d1.push_back(i);
	if (d1.empty())
		cout << "d1为空" << endl;
	else {
		cout << "d1不为空" << endl;
		cout << d1.size() << endl;
		//deque没有容量的概念,可以无限放

		//重新指定大小
		//d1.resize(15);
		d1.resize(15, 3);//多的用3填充
		printDeque(d1);
	}
}

deque容器的插入和删除

#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>d) {//const防止误修改
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
		cout << *it << " ";
	cout << endl;
}
void test() {
	deque<int>d1;
	//尾插
	d1.push_back(10);
	d1.push_back(20);
	//头插
	d1.push_front(30);
	d1.push_front(40);
	printDeque(d1);//40 30 10 20

	//尾删
	d1.pop_back();
	printDeque(d1);//40 30 10

	//头删
	d1.pop_front();
	printDeque(d1);//30 10
}

void test02() {
	deque<int>d1;
	//尾插
	d1.push_back(10);
	d1.push_back(20);
	//头插
	d1.push_front(30);
	d1.push_front(40);
	printDeque(d1);//40 30 10 20

	//insert插入
	d1.insert(d1.begin(), 1000);
	printDeque(d1);//1000 40 30 10 20

	d1.insert(d1.begin(), 3, 9);
	printDeque(d1);//9 9 9 1000 40 30 10 20

	//删除指定位置
	deque<int>::iterator it = d1.begin();
	it+=5;
	d1.erase(it);
	printDeque(d1);//9 9 9 1000 40 10 20

	//按照区间的形式删除
	d1.erase(d1.begin(), it);//10 20
	printDeque(d1);

	//按照区间进行插入
	deque<int>d2;
	d2.push_back(1);
	d2.push_back(2);
	d2.push_back(3);

	d1.insert(d1.begin(), d2.begin(), d2.end());
	printDeque(d1);//1 2 3 10 20

	//清空
	d1.clear();
	printDeque(d1);//只剩一个换行了
}
int main() {
	test();
	test02();
	return 0;
}

数据存取

void test() {
	deque<int>d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_back(30);
	d1.push_front(100);
	d1.push_front(200);
	d1.push_front(300);

	//[]方式
	for (int i = 0; i < d1.size(); i++)
		cout << d1[i] << endl;
	//at方式
	for (int i = 0; i < d1.size(); i++)
		cout << d1.at(i) << endl;

	cout << "第一个元素:" << d1.front() << endl;
	cout << "最后一个元素:" << d1.back() << endl;
}

排序操作

对于支持随机访问的迭代器的容器,都可以利用sort算法直接对其进行排序
vector容器也可以利用sort进行排序

#include<deque>//双端数组
#include<algorithm>//标准算法

sort(d.begin(), d.end());//默认升序

stack (栈)

//栈不允许有遍历
//栈可以判断是否为空 empty
//栈可以计算大小 size

#include<iostream>
using namespace std;
#include<stack>
//栈 stack容器
void test() {
	//先进后出
	stack<int>s;//默认构造

	s.push(10);//入栈
	s.push(20);
	s.push(30);

	while (!s.empty()) {
		cout << s.top() << " ";//输出栈顶元素
		s.pop();//出栈
	}
	cout << endl;
	cout << s.size() << endl;
}
int main() {
	test();
	return 0;
}

queue (队列)

先进先出

只有队头和队尾被看到,不允许遍历

#include<iostream>
using namespace std;
#include<queue>
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
void test() {
	//先进先出
	//创建队列
	queue<Person>q;

	//准备数据
	Person p1("唐僧", 30);
	Person p2("孙悟空", 200);
	Person p3("猪八戒", 100);

	//入队
	q.push(p1);
	q.push(p2);
	q.push(p3);

	while (!q.empty()) {
		cout << q.front().m_Name << q.front().m_Age<< " ";//查看队头元素
		cout << q.back().m_Name << q.back().m_Age << " ";//查看队尾元素
		q.pop();//出队
	}
	cout << endl;
	cout << q.size() << endl;
}
int main() {
	test();
	return 0;
}

list链表

list容器构造

#include<iostream>
using namespace std;
#include<list>
//链表由节点构成
//节点包括 数据域,指针域

//list容器构造函数
void printList(const list<int>&L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test() {
	//创建 list容器
	list<int>L1;

	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历容器
	printList(L1);

	//区间方式构造
	list<int>L2(L1.begin(), L1.end());
	printList(L2);

	//拷贝构造
	list<int>L3(L2);
	printList(L3);

	//n个 elem
	list<int>L4(5, 100);
	printList(L4);
}
int main() {
	test();
	return 0;
}

list赋值和交换

#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>&L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
//赋值
void test() {
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);

	list<int> L2;
	L2 = L1;
	printList(L2);

	list<int>L3;
	L3.assign(L2.begin(), L2.end());
	printList(L3);

	list<int>L4;
	L4.assign(5, 3);
	printList(L4);
}

//交换
void test02() {
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);

	list<int>L2;
	L2.assign(5, 100);
	printList(L2);

	//交换
	L1.swap(L2);
	printList(L1);
	printList(L2);
}
int main() {
	test();
	test02();
	return 0;
}

list大小

if (L1.empty()) {
	cout << "L1为空" << endl;
}
else {
	cout << "L1不为空" << endl;
	cout << "L1的元素个数" << L1.size() << endl;
}

//重新指定大小
L1.resize(10,10000);
printList(L1);

L1.resize(3);
printList(L1);

list插入和删除

void test() {
	list<int>L1;
	L1.push_back(10);//尾插
	L1.push_back(20);
	L1.push_back(30);
	L1.push_front(100);//头插
	L1.push_front(200);
	L1.push_front(300);
	printList(L1);//300 200 100 10 20 30

	//尾删
	L1.pop_back();
	printList(L1);//300 200 100 10 20

	//头删
	L1.pop_front();
	printList(L1);//200 100 10 20

	//insert插入
	list<int>::iterator it = L1.begin();//创建一个迭代器
	L1.insert(++it, 1000);
	printList(L1);//200 1000 100 10 20

	//删除
	it = L1.begin();
	L1.erase(++it);
	printList(L1);//200 100 10 20

	L1.push_back(10);

	//移除,可以删除所有与 elem值匹配的元素
	L1.remove(10);
	printList(L1);//200 100 20

	//清空
	L1.clear();
	printList(L1);
}

list的数据存取

不支持随机访问

//list容器中的数据存取
void test() {
	list<int>L1;
	L1.push_back(1);
	L1.push_back(2);
	L1.push_back(3);
	L1.push_back(4);
	cout << L1.front() << " " << L1.back() << endl;//1 4
}
//验证迭代器是不支持随机访问的
list<int>::iterator it = L1.begin();
it++;//支持双向
it--;
//it+=2; 错误 不能随机访问

list反转和排序

bool myCompare(int v1,int v2) {//实现排序降序
	return v1 > v2;//让L1>L2
}
void test() {
	list<int>L1;
	L1.push_back(1);
	L1.push_back(3);
	L1.push_back(2);
	L1.push_back(4);
	printList(L1);

	L1.reverse();//反转
	printList(L1);

	L1.sort();//排序  默认升序
	printList(L1);

	L1.sort(myCompare);//降序
	printList(L1);
}

list排序案例

以年龄做升序,年龄相同做降序

#include<iostream>
using namespace std;
#include<list>
#include<string>
//自定义数据类型
class Person {
public:
	Person(string name,int age,int height) {
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}
	string m_Name;
	int m_Age;
	int m_Height;
};

//指定排序规则
bool comparePerson(Person &p1,Person &p2) {
	//按照年龄升序
	if (p1.m_Age == p2.m_Age)
	{
		//年龄相同,按照升高降序
		return p1.m_Height > p2.m_Height;
	}
	return p1.m_Age < p2.m_Age;
}
void test() {
	list<Person>L1;//创建容器

	//准备数据
	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("张飞", 35, 160);
	Person p6("关羽", 35, 200);

	//插入数据
	L1.push_back(p1);
	L1.push_back(p2);
	L1.push_back(p3);
	L1.push_back(p4);
	L1.push_back(p5);
	L1.push_back(p6);

	for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++) {
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高:" << (*it).m_Height << endl;
	}

	cout << "----------------------------------------" << endl;
	//排序
	L1.sort(comparePerson);
	for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++) {
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高:" << (*it).m_Height << endl;
	}
}
int main() {
	test();
	return 0;
}

set/multiset

元素在插入时自动排序

属于关联式容器,底层是二叉树结构

set不允许有重复的元素

multiset允许有重复的元素

set容器构造和赋值

#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>&s) {
	for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test() {
	//set容器构造和赋值
	set<int>s1;
	s1.insert(10);//插入
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(30);
	printSet(s1);//10 20 30 40 自动升序排列 不允许插入重复值

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);//10 20 30 40

	//赋值
	set<int>s3;
	s3 = s2;
	printSet(s3);//10 20 30 40

	//大小
	if (s1.empty()) {
		cout << "s1为空" << endl;
	}
	else {
		cout << "s1不为空" << endl;
		cout << "s1的大小" << s1.size() << endl;
	}
	
	set<int>s4;
	s1.insert(4);
	s1.insert(1);

	//交换
	s1.swap(s4);
	printSet(s1);//1 4
	printSet(s4);//10 20 30 40

	//删除
	s2.erase(s2.begin());
	printSet(s2);//20 30 40

	//删除的重载版本————删除指定值
	s2.erase(30);
	printSet(s2);//20 40

	//清空
	s2.clear();
	printSet(s2);

	//查找
	set<int>::iterator pos = s3.find(30);//返回元素的迭代器,若未找到返回end
	if (pos != s3.end()) {
		cout << "找到元素" << *pos << endl;
	}
	else {
		cout << "未找到" << endl;
	}

	//统计30的个数
	int num = s3.count(30);
	//set统计结果 只有为0或1
	cout << "num=" << num << endl;

}
int main() {
	test();
	return 0;
}

 大小和交换

pair对组创建

成对出现的数据,利用对组可以返回两个数据

void test() {
	//第一种方式
	pair<string, int>p("tom", 20);
	cout << "姓名:" << p.first << " 年龄:" << p.second << endl;

	//第二种方式
	pair<string, int>p2 = make_pair("jerry", 30);
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}

内置类型指定排序规则

#include<iostream>
using namespace std;
#include<set>
//set容器排序

//利用仿函数指定set容器排序规则
class myCompare {
public:
	bool operator()(int v1,int v2)const {
		return v1 > v2;//做降序
	}
};
void test() {
	set<int,myCompare>s1;

	s1.insert(4);
	s1.insert(2);
	s1.insert(6);
	s1.insert(3);
	s1.insert(5);
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
		cout << *it << " ";
	cout << endl;
}
int main() {
	test();
	return 0;
}

set存放自定义数据类型 指定规则

#include<iostream>
using namespace std;
#include<set>
#include<string>
//自定义数据类型
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
//指定排序规则
//仿函数
class comparePerson {
public:
	bool operator()(const Person& p1, const Person& p2)const {//使用了Person,要把Person写到其前面
		//按照年龄 降序
		return p1.m_Age > p2.m_Age;
	}
};
void test() {
	set<Person,comparePerson>s;
	Person p1("刘备", 35);
	Person p2("曹操", 45);
	Person p3("孙权", 40);
	Person p4("赵云", 25);
	Person p5("张飞", 35);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);

	for (set<Person,comparePerson>::iterator it = s.begin(); it != s.end(); it++) {
		cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age <<endl;
	}
}
int main() {
	test();
	return 0;
}

map/multimap

插入和删除

#include<iostream>
using namespace std;
#include<map>
//map容器 构造和赋值
void printMap(map<int, int>& m) {
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key=" << (*it).first << " value="<<it->second << endl;
	}
	cout << endl;
}
void test() {
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(4, 40));
	printMap(m);

	//拷贝构造
	map<int, int>m2(m);
	printMap(m2);

	//赋值
	map<int, int>m3;
	m3 = m2;
	printMap(m3);

	//大小
	if (m.empty()) {
		cout << "map容器为空" << endl;
	}
	else {
		cout << "map的大小为:" << m.size() << endl;
	}

	map<int, int>m4;
	m4.insert(pair<int, int>(8, 100));
	m4.insert(pair<int, int>(6, 300));
	m4.insert(pair<int, int>(7, 400));
	m4.insert(pair<int, int>(9, 500));
	printMap(m4);

	//交换
	m.swap(m4);
	cout << "交换后" << endl;
	printMap(m);
	printMap(m4);

	//插入
	map<int, int>m5;
	//第一种
	m5.insert(pair<int, int>(10, 100));
	//第二种
	m5.insert(make_pair(20, 200));
	//第三种
	m5.insert(map<int, int>::value_type(30, 300));
	//第四种
	m5[40] = 400;//不建议使用
	cout << "输出m5" << endl;
	printMap(m5);

	//删除
	m5.erase(m5.begin());
	printMap(m5);

	m5.erase(20);//按照 key删除
	printMap(m5);

	//清空
	m5.erase(m5.begin(), m5.end());
	printMap(m5);
	//清空
	m4.clear();
}
int main() {
	test();
	return 0;
}

查找和统计

#include<iostream>
using namespace std;
#include<map>
void test() {
	//查找
	map<int, int>m;
	m.insert(pair<int,int>(1,10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	map<int,int>::iterator pos = m.find(3);//按 key查找     find返回的是迭代器
	if (pos != m.end())
		cout << "查找到了元素 key=" << (*pos).first << " value=" << pos->second << endl;
	else
		cout << "未找到元素" << endl;

	//统计
	//map不允许插入重复key 元素,count统计而言 结果要么是0,要么是1
	//multimap的count统计可能大于1
	int num = m.count(3);
	cout << "num=" << num << endl;
}
int main() {
	test();
	return 0;
}

排序

#include<iostream>
using namespace std;
#include<map>
class myCompare {
public:
	bool operator()(int v1,int v2)const{//第一个括号是要重载的运算符,第二个小括号是参数列表
		//降序
		return v1 > v2;
	}
};
void test() {
	map<int, int,myCompare>m;
	//两种插入方法
	m.insert(pair<int, int>(1, 10));//存储两个值
	m.insert(make_pair(2, 20));//无需显式指定类型(自动推导)
	m.insert(make_pair(6, 60));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(5, 50));

	for (map<int, int,myCompare>::iterator it = m.begin(); it != m.end(); it++)
		cout << "key=" << it->first << " value=" << it->second << endl;
}
int main() {
	test();
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2326318.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

npu踩坑记录

之前使用qwen系列模型在ascend 910a卡进行了一些生成任务, 贴出踩坑过程也许对遇到类似问题的同学有帮助: ) 目录 千问 qwq32环境配置 代码部署 生成内容清洗 已生成内容清洗 生成过程优化 Failed to initialize the HCCP process问题 assistant 的历史回答丢失 推理执…

Linux信号——信号的产生(1)

注&#xff1a;信号vs信号量&#xff1a;两者没有任何关系&#xff01; 信号是什么&#xff1f; Linux系统提供的&#xff0c;让用户&#xff08;进程&#xff09;给其他进程发送异步信息的一种方式。 进程看待信号的方式&#xff1a; 1.信号在没有发生的时候&#xff0c;进…

【机器学习】——机器学习思考总结

摘要 这篇文章深入探讨了机器学习中的数据相关问题&#xff0c;重点分析了神经网络&#xff08;DNN&#xff09;的学习机制&#xff0c;包括层级特征提取、非线性激活函数、反向传播和梯度下降等关键机制。同时&#xff0c;文章还讨论了数据集大小的标准、机器学习训练数据量的…

JMeter进行分布式压测

从机&#xff1a; 1、确认防火墙是否关闭&#xff1b; 2、打开网络设置&#xff0c;关闭多余端口&#xff1b;&#xff08;避免远程访问不到&#xff09; 3、打开JMeter/bin 目录底下的jmeter.properties&#xff1b; remove_hosts设置当前访问地址&#xff0c;192.XXXXX&…

快速入手-基于Django-rest-framework的第三方认证插件(SimpleJWT)权限认证扩展返回用户等其他信息(十一)

1、修改serializer.py&#xff0c;增加自定义类 # 自定义用户登录token等返回信息 class MyTokenObtainPair(TokenObtainPairView): def post(self, request, *args, **kwargs): serializer self.get_serializer(datarequest.data) try: serializer.is_valid(raise_exceptio…

关于IP免实名的那些事

IP技术已成为个人与企业保护隐私、提升网络效率的重要工具。其核心原理是通过中介服务器转发用户请求&#xff0c;隐藏真实IP地址&#xff0c;从而实现匿名访问、突破地域限制等目标。而“免实名”代理IP的出现&#xff0c;进一步简化了使用流程&#xff0c;用户无需提交身份信…

【SQL性能优化】预编译SQL:从注入防御到性能飞跃

&#x1f525; 开篇&#xff1a;直面SQL的"阿喀琉斯之踵" 假设你正在开发电商系统&#x1f6d2;&#xff0c;当用户搜索商品时&#xff1a; -- 普通SQL拼接&#xff08;危险&#xff01;&#xff09; String sql "SELECT * FROM products WHERE name "…

SQL Server从安装到入门一文掌握应用能力。

本篇文章主要讲解,SQL Server的安装教程及入门使用的基础知识,通过本篇文章你可以快速掌握SQL Server的建库、建表、增加、查询、删除、修改等基本数据库操作能力。 作者:任聪聪 日期:2025年3月31日 一、SQL Server 介绍: SQL Server 是微软旗下的一款主流且优质的数据库…

力扣HOT100之矩阵:54. 螺旋矩阵

这道题之前在代码随想录里刷过类似的&#xff0c;还有印象&#xff0c;我就按照当初代码随想录的思路做了一下&#xff0c;结果怎么都做不对&#xff0c;因为按照代码随想录的边界条件设置&#xff0c;当行数和列数都为奇数时&#xff0c;最后一个元素无法被添加到数组中&#…

5.1 WPF路由事件以及文本样式

一、路由事件 WPF中存在一种路由事件&#xff08;routed event&#xff09;&#xff0c;该事件将发送到包含该控件所在层次的所有控件&#xff0c;如果不希望继续向更高的方向传递&#xff0c;只要设置e.Handled true即可。 这种从本控件-->父控件->父的父控件的事件&am…

Python数据可视化-第1章-数据可视化与matplotlib

环境 开发工具 VSCode库的版本 numpy1.26.4 matplotlib3.10.1 ipympl0.9.7教材 本书为《Python数据可视化》一书的配套内容&#xff0c;本章为第1章 数据可视化与matplotlib 本文主要介绍了什么是数据集可视化&#xff0c;数据可视化的目的&#xff0c;常见的数据可视化方式…

Flutter敏感词过滤实战:基于AC自动机的高效解决方案

Flutter敏感词过滤实战&#xff1a;基于AC自动机的高效解决方案 在社交、直播、论坛等UGC场景中&#xff0c;敏感词过滤是保障平台安全的关键防线。本文将深入解析基于AC自动机的Flutter敏感词过滤实现方案&#xff0c;通过原理剖析实战代码性能对比&#xff0c;带你打造毫秒级…

Odoo/OpenERP 和 psql 命令行的快速参考总结

Odoo/OpenERP 和 psql 命令行的快速参考总结 psql 命令行选项 选项意义-a从脚本中响应所有输入-A取消表数据输出的对齐模式-c <查询>仅运行一个简单的查询&#xff0c;然后退出-d <数据库名>指定连接的数据库名&#xff08;默认为当前登录用户名&#xff09;-e回显…

Vue中使用antd-table组件时,树形表格展开配置不生效-defaultExpandedRowKeys-默认展开配置不生效

defaultExpandedRowKeys属性 defaultExpandAllRows这个属性仅仅是用来设置默认值的,只在第一次渲染的时候起作用,后续再去改变,无法实现响应式 解决方案一 a-table表格添加key属性,当每次获取值时,动态改变key,以达到重新渲染的效果 <a-table:key="tableKey"…

VRRP交换机三层架构综合实验

题目要求&#xff1a; 1&#xff0c;内网Ip地址使用172.16.0.0/16分配 说明可以划分多个子网&#xff0c;图中有2个VLAN&#xff0c;可以根据VLAN划分 2&#xff0c;sw1和SW2之间互为备份 互为备份通常通过VRRP&#xff08;虚拟路由冗余协议&#xff09;来实现。VRRP会在两个…

基于卷积神经网络的眼疾识别系统,resnet50,efficentnet(pytorch框架,python代码)

更多图像分类、图像识别、目标检测、图像分割等项目可从主页查看 功能演示&#xff1a; 眼疾识别系统resnet50&#xff0c;efficentnet&#xff0c;卷积神经网络&#xff08;pytorch框架&#xff0c;python代码&#xff09;_哔哩哔哩_bilibili &#xff08;一&#xff09;简介…

基于srpingboot智慧校园管理服务平台的设计与实现(源码+文档+部署讲解)

技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;免费功能设计、开题报告、任务书、中期检查PPT、系统功能实现、代码编写、论文编写和辅导、论…

【力扣hot100题】(026)合并两个有序链表

可以创建一个新链表记录答案&#xff1a; /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *…

TCP网络编程与多进程并发实践

一、引言 在网络编程中&#xff0c;TCP&#xff08;传输控制协议&#xff09;是一种面向连接的、可靠的、基于字节流的传输层通信协议。而多进程并发则是一种提高服务器处理能力的有效手段&#xff0c;允许服务器同时处理多个客户端的请求。本文将详细介绍如何使用 TCP 协议进…

visio导出pdf公式变形

情况描述导出为pdf后&#xff0c;mathtype写的公式就变形了 但是导出为png和jpg就是正常 解决方法就是 需要下载一个Adobe Acrobat