STL common containers and comparison of overloaded operators

#include<iostream>
#include<algorithm>
#include<vector>
#include<set>//multiset在set头文件中
#include<map>
#include<unordered_map>

using namespace std;

int main()
{
    
    
	vector<int> res;
	res.push_back(1);
	res.push_back(2);
	res.push_back(3);
	vector<int>::iterator it = find(res.begin(), res.end(),3);
	//vector 内部没有find函数,只有algorithm中的find函数,时间复杂度是O(n)
	res.erase(it);
	
	set<int> s;//操作都是Log(n)
	s.insert(1);
	s.insert(2);
	set<int>::iterator itt = s.find(2);//set内部也是红黑树实现 时间复杂度是O(logn)
	s.erase(itt);//O(logn)

	//map 内部是红黑树实现,查找删除插入都是logn 但比较占空间,默认有序
	//unordered_map 内部是哈希表实现 查找删除插入最好O(1)最坏O(n) 查询多使用它,但内部无序

	map<char, int> cnt;//操作都是Log(n)
	cnt['a'] = 1;
	map<char, int>::iterator ittt = cnt.find('a');
	cnt.erase(ittt);
	cnt.insert(make_pair('c', 2));


	return 0;
}
#include<iostream>
#include<queue>
#include<algorithm>

using namespace std;

//优先队列里面的自定义排序都是相反的

struct cmp{
    
    
	 bool operator()(int a, int b)
	{
    
    
		return a < b;//返回大的int
	}
};

struct pp{
    
    
	int a, b;
	bool operator <(pp tem)
	{
    
    
		return a < tem.a;
	}
};

struct node{
    
    
	int x, y;
	friend bool operator < (node a, node b)//必须要加friend
	{
    
    
		return a.x > b.x;
	}
};

int main()
{
    
    
	priority_queue<int, vector<int>, greater<int>> q;//greater 是先出来小的
	q.push(1);
	q.push(4);
	q.push(9);
	//cout << q.top() << endl;

	priority_queue<int, vector<int>, cmp> p;
	p.push(1);
	p.push(3);
	p.push(2);
	//cout << p.top() << endl;

	priority_queue<node> s;
	s.push({
    
    2,4});
	s.push({
    
    3,5});
	//cout << s.top().x << endl;

	pp arr[3];
	arr[0] = {
    
    1, 2};
	arr[1] = {
    
    2, 3};
	sort(arr, arr + 2);
	cout << arr[0].a << endl;

	return 0;
}

Guess you like

Origin blog.csdn.net/qq_63092029/article/details/129997837