#include
#include
using namespace std;
int main() {
pair<int,double> p1(1,3.14);
pair<char,string> p2('a',"hello");
cout<<p1.first<<", "<<p1.second<<endl;
cout<<p2.first<<", "<<p2.second<<endl;
return 0;
}
#include
#include
using namespace std;
int main() {
pair<int,int> p1(1,2);
pair<int,pair<int,int>> p2(3,make_pair(4, 5));
pair<pair<int,int>,pair<int,int>> p3(make_pair(6, 7),make_pair(3, 5));
cout<<p1.first<<", "<<p1.second<<endl;
cout<<p2.first<<", "<<p2.second.first<<", "<<p2.second.second<< endl;
cout<<p3.first.first<<" ,"<<p3.first.second<<" ,"<<p3.second.first<<" ,"<<p3.second.second<<endl;
return 0;
}
#include
#include
#include
using namespace std;
struct Person{
string name;
int age;
};
int main() {
vector<Person> people;
people.push_back({"Alice",25});
people.push_back({"Bob",30});
people.push_back({"Charlie",20});
vector<pair<Person, int>> scores;
scores.push_back({people[0],90});
scores.push_back({people[1],85});
scores.push_back({people[2],95});
for(const auto& pair : scores){
cout<<"Name: "<<pair.first.name<<endl;
cout<<"Age: "<<pair.first.age<<endl;
cout<<"Score: "<<pair.second<<endl;
cout<<endl;
}
return 0;
}
一般用 i
push_back()
pop_back()
insert()
erase()
empty()
resize()
begin(),end()
clear()
sort()
unique()
排序并去重:vec.erase(unique(vec.begin(),vec.end()),vec.end());
#include
#include
#include
int main() {
std::vector<int> vec = {2,1,3,2,4,1,5,4};
std::sort(vec.begin(),vec.end());
auto last = unique(vec.begin(),vec.end());
vec.erase(last,vec.end());
for(const auto& num:vec){
std::cout<<num<<" ";
}
return 0;
}
以下都是 O(1)
push(x)
pop()
top()
empty()
size()
#include
priority_queue(int,vector,compare/greater) //小根堆
struct Compare{
bool operator()(int a,int b){
return a>b;
}
}

#include
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int m;cin>>m;
queue<string> V,N;
while(m--){
string op;cin>>op;
if(op=="IN"){
string name,q;cin>>name>>q;
if(q=="V")V.push(name);
else N.push(name);
}else{
string q;cin>>q;
if(q=="V")V.pop();
else N.pop();
}
}
while (V.size()) {
cout<<V.front()<<'\n';
V.pop();
}
while(N.size()){
cout<<N.front()<<"\n";
N.pop();
}
return 0;
}

#include
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin>>n;
priority_queue<ll,vector<ll>,greater<ll>> pq;
for(int i=1;i<=n;++i){
ll x;cin>>x;
pq.push(x);
}
ll ans = 0;
while(pq.size()>=2){
ll x = pq.top();pq.pop();
ll y = pq.top();pq.pop();
ans += x+y;
pq.push(x+y);
}
cout<<ans<<"\n";
return 0;
}
#include
#include
using namespace std;
int main( ){
set<int,greater<int>> mySet;
mySet.insert(25);
mySet.insert(17);
mySet.insert(39);
mySet.insert(42);
for(const auto& elem:mySet)cout<<elem<<" ";
cout<<endl;
return 0;
}
#include
#include
using namespace std;
struct MyCompare{
bool operator()(const int& a,const int& b)const{
return a>b;
}
};
int main( ){
set<int,MyCompare> mySet;
mySet.insert(25);
mySet.insert(17);
mySet.insert(39);
mySet.insert(42);
for(const auto& elem : mySet){
cout<< elem << " ";
}
cout<<endl;
return 0;
}
#include
#include
using namespace std;
int main() {
//创建并初始化multimap
multimap<int, string> myMultimap = {{1, "Apple"}, {2, "Banana"}, {3, "Orange"}};
//判断元素是否存在
if (myMultimap.count(2) == 0) {
cout << "Key 2 not found." << endl;
}
//插入元素
myMultimap.insert(make_pair(4, "Grapes"));
//清空multimap
myMultimap.clear();
//判断multimap是否为空
if (myMultimap.empty()) {
cout << "Multimap is empty." << endl;
} else {
//查找和访问元素
auto range = myMultimap.equal_range(3);
for (auto it = range.first; it != range.second; ++it) {
cout << "Key: " << it->first << ", Value: " << it->second << endl;
}
}
//遍历并打印multimap中的元素
for (const auto& pair : myMultimap) {
cout << "Key: " << pair.first << ", Value: " << pair.second << endl;
}
//删除元素
myMultimap.erase(3);
return 0;
}