pair
這裏沒有什麼可以說的了
#include <iostream>
using namespace std;
typedef pair<int,int> P;//定義pair類型
P a[10],b[10];
int main()
{
for(int i = 1; i <= 9; i++){
a[i].first = i;
a[i].second = i + 1;
b[i].first = i + 2;
b[i].second = i + 3;
}
//遍歷
for(auto x : a)
cout << x.first << " " << x.second << " ";
cout << endl;
for(auto x : b)
cout << x.first << " " << x.second << " ";
return 0;
}
輸出
0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
0 0 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12
注意下標是從0 開始的
map
#include <bits/stdc++.h>
using namespace std;
map<int,int> s;//默認排序規則,根據第一個數從小到大(第一個數是唯一的)
int main()
{
//插入
s[5]++;
s[1]++;
s[6]++;
s[4]++;
s[5]++;
//刪除
s[1]--;
//查找
//找到
if (s[4] != 0)
cout << "Yes" << endl;
//找不到
else cout << "No" << endl;
//遍歷
for(auto y : s)
cout << y.first << ' ' << y.second << "\n";
return 0;
}
輸出樣例
Yes
1 0
4 1
5 2
6 1
set
#include <bits/stdc++.h>
using namespace std;
set<int> s;//默認排序規則,從小到大
int main()
{
//插入
s.insert(2);
s.insert(1);
s.insert(4);
s.insert(3);
//刪除
s.erase(3);
//大小
cout << s.size() << endl;
//查找
//找到
if (s.find(3) != s.end())
cout << "Yes" << endl;
//找不到
else cout << "No" << endl;
//遍歷
for(auto y : s)
cout << y << " ";
return 0;
}