要点
对
- 对类型表示一组两个值 用
pair<値1の型, 値2の型> 変数名;声明変数名.first和第一个值, 您可以使用変数名.second访问第二个值 您可以使用make_pair(値1, 値2)创建一对 您可以使用tie(変数1, 変数2) = pair型の値;分解对
元组
- 元组类型表示一组多个值。 用
tuple<値1の型, 値2の型, 値3の型, (...)> 変数名;声明(只写你需要的类型) 元组可以使用make_tuple(値1, 値2, 値3, (...))生成 您可以使用tie(変数1, 変数2, 変数3, (...)) = tuple型の値;分解元组
对/元组比较
- 比较第一个值,如果相等,则比较下一个值。
自动
- 通过在变量声明和 range for 语句中使用 auto,您可以省略类型。
- 您还可以使用 auto 创建引用
对/元组
对
pair 是表示一对两个值的类型。
示例代码
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<string, int> p("abc", 3);
cout << p.first << endl; // abc
p.first = "hello";
cout << p.first << endl; // hello
cout << p.second << endl; // 3
p = make_pair("*", 1);
string s;
int a;
tie(s, a) = p;
cout << s << endl; // *
cout << a << endl; // 1
}
実行結果
abc hello 3 * 1
声明/初始化
pair<型1, 型2> 変数名; pair<型1, 型2> 変数名(値1, 値2);
访问
変数名.first // 1つ目の値 変数名.second // 2つ目の値
对的生成
make_pair(値1, 値2)
您可以使用 make_pair 获取pair类型值。
您还可以通过执行以下操作来生成一对。
pair<型1, 型2>(値1, 値2)
无论您使用哪一个,make_pair不要求您显式编写类型,因此
您可能想使用make_pair。
处理细节类型别名 当使用给pair类型起一个简单的名字时,后者可能更容易写。
对的分解
型1 変数1; 型2 変数2; tie(変数1, 変数2) = pair型の値;
该对的第一个和第二个值分别分配给变量 1 和变量 2。
元组
元组是对的泛化,并且是表示“多个值的集合”的类型。
示例代码
#include <bits/stdc++.h>
using namespace std;
int main() {
tuple<int, string, bool> data(1, "hello", true);
get<2>(data) = false;
cout << get<1>(data) << endl; // hello
data = make_tuple(2, "WORLD", true);
int a;
string s;
bool f;
tie(a, s, f) = data;
cout << a << " " << s << " " << f << endl; // 2 WORLD 1
}
実行結果
hello 2 WORLD 1
声明/初始化
tuple<型1, 型2, 型3, ...(必要な分だけ型を書く)> 変数名; tuple<型1, 型2, 型3, ...(必要な分だけ型を書く)> 変数名(値1, 値2, 値3, ...); // 初期化
元组可以表示零个或多个对。
访问
get<K>(tuple型の変数) // K(定数)番目にアクセス
请注意,K 必须是常数,并且 K 是从 0 开始的数字。
变量不能用作 K,如下所示。
int i = 2; tuple<int, int, int> data(1, 2, 3); cout << get<i>(data) << endl; // コンパイルエラー
元组生成
make_tuple(値1, 値2, 値3, ...)
也可以按如下方式生成。
tuple<型1, 型2, 型3, ...(必要な分だけ書く)>(値1, 値2, 値3, ...)
元组分解
型1 変数1; 型2 変数2; 型3 変数3; ︙ tie(変数1, 変数2, 変数3, ...) = tuple型の値;
对/元组比较
可以比较相同类型的对和元组。 比较
例如
当计算像pair<int, int>时,根据第一个值进行比较,如果第一个值相等,则根据第二个值进行比较。
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, int> a(3, 1);
pair<int, int> b(2, 10);
// 3 > 2 なので a < b は false
if (a < b) {
cout << "a < b" << endl;
}
else {
cout << "a >= b" << endl;
}
pair<int, int> c(5, 1);
pair<int, int> d(5, 10);
// 5 == 5 であり 1 < 10 なので c < d は true
if (c < d) {
cout << "c < d" << endl;
}
else {
cout << "c >= d" << endl;
}
}
実行結果
a >= b c < d
比较元组时,将第一个值与最高优先级进行比较,如果相等,则比较第二个值。如果第二个值也相等,则从第一个值开始比较第三个值...''。
此外,
像==如果所有值都相等,
如果存在一个或多个不同的值,则!=为 true。
对pair或tuple数组进行排序时,会按照上面的比较顺序进行排序。
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<tuple<int, int, int>> a;
a.push_back(make_tuple(3, 1, 1));
a.push_back(make_tuple(1, 2, 100));
a.push_back(make_tuple(3, 5, 1));
a.push_back(make_tuple(1, 2, 3));
sort(a.begin(), a.end());
for (tuple<int, int, int> t : a) {
int x, y, z;
tie(x, y, z) = t;
cout << x << " " << y << " " << z << endl;
}
}
実行結果
1 2 3 1 2 100 3 1 1 3 5 1
自动
当通过初始化或范围 for 语句声明变量时,类型部分为
您可以通过编写auto来省略类型。
变量声明中的
示例代码
#include <bits/stdc++.h>
using namespace std;
string concat(string a, string b) {
return a + b;
}
int main() {
string a = "Hello,";
string b = "World";
auto ab = concat(a, b); // string型
cout << ab << endl;
vector<int> c = {1, 2, 3};
auto d = c; // vector<int>型
for (auto elem : d) {
// elemはint型
cout << elem << endl;
}
}
実行結果
Hello,World 1 2 3
auto
auto 変数名 = 初期値;
auto in range for 语句
for (auto 変数名 : 配列変数) {
// 変数名 を使う
}
自动参考
int a = 12345;
auto &ref = a; // aへの参照 `int &ref = a;`と書くのと同じ
vector<int> b = {1, 2, 3};
// 範囲for文でも参照にすることができる
for (auto &elem : b) {
elem *= 2;
}
cout << b.at(0) << endl; // 2
cout << b.at(1) << endl; // 4
cout << b.at(2) << endl; // 6
详细信息
忽略
如果你想在分解对或元组时丢弃不必要的元素,
通过将ignore传递给 tie 参数,您可以丢弃相应位置的值。
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, int> p(3, 5);
int right;
tie(ignore, right) = p; // 2番目の値だけ取り出す
cout << right << endl; // 5
tuple<int, string, bool> tpl(2, "hello", false);
int x;
string s;
tie(x, s, ignore) = tpl; // 1番目の値、2番目の値だけ取り出す
cout << x << " " << s << endl; // 2 hello
}
実行結果
5 2 hello
类型别名 当使用
类型别名 当使用允许您为类型指定不同的名称。 当类型名称变长(例如对或元组)时,使用类型别名非常有用。
#include <bits/stdc++.h>
using namespace std;
int main() {
using pii = pair<int, int>;
// これ以降 pii という型名はpair<int, int> と同じ意味で使える
pii p;
p = make_pair(1, 2);
cout << "(" << p.first << ", " << p.second << ")" << endl;
p = pii(3, 4); // 別のpairを作って代入
cout << "(" << p.first << ", " << p.second << ")" << endl;
}
実行結果
(1, 2) (3, 4)
您可以按如下方式声明类型别名:
声明类型别名
using 新しい型名 = 型名;
声明的类型别名可以用作类型名称。
您还可以使用类型别名轻松编写多维数组的类型。
using vi = vector<int>; // intの1次元の型に vi という別名をつける using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける int N = 10, M = 20; vvi data(N, vi(M)); // N * M の2次元配列
到目前为止,它都写在程序的第二行。using namespace std;还
出现了关键字using,但是这个using和类型别名
请注意,using具有不同的含义。using namespace std;将在第 4 章中进行解释。
在 C++ 中,类似的函数是typedef已提供声明,但基本上
尝试使用using。
#include <bits/stdc++.h>
using namespace std;
int main() {
typedef pair<int, int> pii; // pair<int, int> に pii という別名を付ける
pii p = make_pair(1, 2);
cout << "(" << p.first << ", " << p.second << ")" << endl;
}
実行結果
(1, 2)
演習問題
请解决链接目的地的问题。
ABC问题
我们将介绍使用本次介绍的功能可以解决的AtCoder初学者竞赛问题。 如果您对练习题不满意,请尝试一下。