STL基本用法

系统给某一程序分配空间时,所需时间与空间大小无关,与申请次数有关

vector

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	//size和empty是所有容器都有的,时间复杂度是O(1)的 
	vector<int> a(10);
	//相当与数组a[10],一个长度是10的vector 
	
	vector<int> a(10,3); 
	for(auto x:a)cout<<x<<" ";
	cout<<endl;
	//一个长度是10的vector,其中的每个数都是3
	
	 vector<int> a[10];
	 //定义了10个vector 
	 
	 vector<int> a;
	 a.size();
	 a.empty();//空返回true,否则返回false 
	 a.clear();//清空  
	 a.front();//返回第一个数
	 a.back();//返回最后一个数 
	 a.push_back();//在开头插入一个数 
	 a.pop_back();//在结尾插入一个数 
	 begin(),end();//迭代器,begin()是vector的第一个数,end()是vector的最后一个 
     数的后一个数 
	 a[]//可以这样用
	 
	 //遍历
	 for(int i=0;i<10;i++)a.push_back(i);
	 
	 for(int i=0;i<a.size();i++)
	 cout<<a[i]<<" ";
	 
	  for(vector<int>::iterator i=a.begin();i!=a.end();i++)
	  cout<<*i<<" ";
	  
	  for(auto i=a.begin();i!=a.end();i++)
	  cout<<*i<<" ";
	  
	  for(auto x:a)cout<<x<<" ";
	  cout<<endl;
	  
	  //vector支持比较运算,按字典序排序 
	  vector<int> a(4,3),b(3,4);
	  if(a<b)cout<<"a<b\n";
	   

	  return 0;
}

 pair的用法

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	pair<int,int> p;
	p=make_pair(10,"abc")
	p={20,"abc"}
	pair<int,pair<int,int>>p;
	//pair的构造
	 
	p.first;//取第一个元素 
	p.second;//取第二个元素
	
	//支持比较运算,以first为第一关键字,以second为第二关键字(字典序)
	 
	return 0;
}

 string

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	size();
	empty();
	clear();
    s.push_back()//尾部插入一个元素
    s.insert(pos,char);//在pos之前插入一个元素char
    s1.insert(s1.begin(),'1');
	
	string s="abc";
	s+="def";
	s+='g';
	
	cout<<s.substr(1,2)<<endl;//从字符串s的下标1开始,取2个字符(包括下标为1的元素,子串长度为2) 
	//如果第二个参数超出范围,就输出到最后一个字符 
	
	cout<<s.substr(3)<<endl;//返回下标从3开始的整个字串 
	
	printf("%s\n",s.c_str());//用printf输出 
	return 0;
}

 queue

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	queue q;//queue、priority_queue,stack没有clear操作 
	//如果想清空的话,就重新构造一个
	q=queue<int>(); 
	push();//向队尾插入一个元素 
	front();//返回队头元素 
	back();//返回 队尾元素 
	pop();//弹出队头元素 
	size();
	empty();
	return 0;
}

priority_queue

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	priority_queue//无clear函数 
	push;//插入一个元素 
	top;//返回堆顶元素 
	pop;//弹出堆顶元素 
	
	priority_queue<int> heap;//默认大根堆
	
	//小根堆的实现方式 
	heap.push(-x);//直接插入负数,将-x按照从大到小排序相当于x从小到大排序  
	priority_queue<int,vector<int>,greater<int>> heap;
	return 0;
}

stack

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	stack//没有clear函数 
	push;//向栈顶插入一个元素 
	top;//返回栈顶元素 
	pop;// 弹出栈顶元素  
	size();
	empty();
	
	return 0;
}

deque(双端队列)

#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
int main()
{
	deque<int> q;//比较慢 
	clear();
	size();
	empty();
	front();//返回第一个元素 
	back();//返回 最后一个元素 
	push_back();//向最后插入一个元素 
	pop_back();//弹出最后一个元素 
	push_front();//向队首插入一个元素 
	pop_front();//从队尾弹出一个元素 
	[];//支持随街寻址
	begin();
	end(); 
	return 0;
}

 set

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
	//O(logn)
	set<int> s;//不能有重复元素,有的话会被忽略掉 
	multiset<int> MS;//可以有重复元素
	
	insert();//插入 
	size();
	empty();
	clear();
	find();//查找,不存在返回end迭代器 
	count();//返回某一个数的个数 
	erase(x);//x是一个数,删除所有x;O(logn+K),K是x的个数 
	//x是迭代器,删除这个迭代器 
	lower_bound(x)//返回大于x的最小的数的迭代器 
	upper_bound(x)//返回大于等于x的最小的数的迭代器 
    begin();
    end();
	return 0;
}

 map

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
	map/multimap
	insert();//插入的数是一个pair 
	erase();//输入的参数是pair或迭代器 
	[]//O(logn)
	map<string,int> a;
	a["abc"]=1;
	cout<<a["abc"]<<endl; 
	lower_bound();
	upper_bound();
	return 0;
}

 其它

#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
int main()
{
	unordered_map<string,int> a;
	unordered_multimap<string,int> a;
	//与map,multimap操作基本一样,除了不支持lower_bound,upper_bound,迭代器的++,--;但是增删改查的时间复杂度是O(1) 
	
	
	bitset//压位 
	bitset<10000> s;
	//支持以下运算 
	//~,&,|,^
	//>>,<<
	//==,!=;
	//[];
	//count();//返回有多少个1
	
	ang();//判断是否至少有一个1
	none();// 判断是否全为0 
	
	set();//把所以位置变成1
	set(k,v);//把第k位变成v 
	reset();//把所有位变成0 
	flip();//取反 
	flip(k);//把第k为取反 
	return 0;
}

A - Watching TV

 Gym - 101498A 

Ahmed spends most of his day watching TV. His father noticed that his academic performance is degrading and decided to help him get back on track, for that he will allow him to choose only one frequency and watch the channels that share the same frequency band.

Can you help Ahmed choose the best frequency which is the one that displays the largest number of channels?

The first line of the input contains an integer T (1  ≤  T  ≤  100), where T is the number of test cases. Then T test cases follow.

The first line of each test case contains an integer N (1  ≤  N  ≤  104), where N is the total number of channels.

Then N lines follow, each line contains a string S and an integer F (11111  ≤  F  ≤  99999), where S is the name of the channel and F is its frequency.

All strings are non-empty consisting of lowercase English letters. The length of each of these strings does not exceed 50 characters.

It is possible that two different channels have the same name and frequency.

Print T lines, each line contains a single integer that represents the frequency Ahmed will choose. If there are several solutions, print the one with the minimum frequency.

Sample 1

InputcopyOutputcopy
3
3
mbcone 12015
mbctwo 12015
mbcthree 12014
2
channelone 11112
channelyou 21112
1
watchme 12345
12015
11112
12345

一个关于map的排序用法(map默认从小到大排序,根据第一个值)

map<int,int,greater<int>> mp;(从大到小)

#include<iostream>
#include<map>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
map<int,int> mp;
bool cmp(pair<int, int> a, pair<int, int> b)
{
	if (a.second == b.second)
		return a.first < b.first;
	else return a.second > b.second;
}
int ans=0;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int x;
			string s;
			cin >> s>> x;
			mp[x]++;
		}
		vector<pair<int, int>> v(mp.begin(), mp.end());
		sort(v.begin(), v.end(), cmp);
		vector<pair<int, int>>::iterator it = v.begin();
		cout << it->first<< endl;
		/*for (vector<pair<int, int>>::iterator it = v.begin(); it != v.end(); it++)
			cout << it->first << " " << it->second << endl;
		cout << "------------------------------" << endl;*/
		mp.clear();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值