༺ཌༀ英国皇家笔记ༀད༻
2025-12-07 10:01:23
发布于:广东
༺ཌༀ tuple ༀད༻
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Stu {
string name, id;
double sc, ch, ma, en;
};
int main() {
/**
* 一个结构,可以存储很多不同类型的信息
*
* 可以用结构体来存储不同类型的信息,
* 也可能用 tuple 这个容器存储不同类型的信息
*/
Stu xiaoming;
// 第一个string:姓名
// 第二个string:学号
// 第三个double:总分
// 第四个double:语文
// 第五个double:数学
// 第六个double:英语
tuple<string, string, double, double, double, double> xiaohong;
// 赋值方式:
// xiaohong = make_tuple("xiaohong", "001", 3, 1, 1, 1);
xiaohong = {"xiaohong", "001", 3, 1, 1, 1};
cout << get<0>(xiaohong) << '\n';
cout << get<1>(xiaohong) << '\n';
cout << get<2>(xiaohong) << '\n';
cout << get<3>(xiaohong) << '\n';
cout << get<4>(xiaohong) << '\n';
cout << get<5>(xiaohong) << '\n';
get<1>(xiaohong) = "002";
cout << get<1>(xiaohong) << '\n'; // get 本身就是引用空间,可以直接修改
// /**
// * 把结果一次性取出来,值获取
// */
// string name, id;
// double sc, chinese, math, english;
// tie(name, id, sc, chinese, math, english) = xiaohong;
// cout << name << ' ' << id << ' ' << sc << ' ' << chinese << ' ' << math << ' ' << english << '\n';
string& name = get<0>(xiaohong);
string& id = get<1>(xiaohong);
double& sc = get<2>(xiaohong);
double& chinese = get<3>(xiaohong);
double& math = get<4>(xiaohong);
double& english = get<5>(xiaohong);
cout << name << ' ' << id << ' ' << sc << ' ' << chinese << ' ' << math << ' ' << english << '\n';
id = "114514";
cout << get<0>(xiaohong) << ' '
<< get<1>(xiaohong) << ' '
<< get<2>(xiaohong) << ' '
<< get<3>(xiaohong) << ' '
<< get<4>(xiaohong) << ' '
<< get<5>(xiaohong) << '\n';
return 0;
}
༺ཌༀ pair ༀད༻
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Pair
{
ll first, second;
};
int main() {
Pair p1 = {1, 2};
// pair<ll, ll> p2 = {3, 4}; // 初始化方式一
pair<ll, ll> p2 = make_pair(3, 4); // 初始化方式二
cout << p1.first << ' ' << p1.second << '\n';
cout << p2.first << ' ' << p2.second << '\n';
// 修改内部的值
p2.first = 5;
p2.second = 6;
cout << p2.first << ' ' << p2.second << '\n';
return 0;
}
array
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
/**
* 如果一个结构,存在很多相同类型的数据
*
* 那么我们完全可以用数组存储相同类型数组的多个元素
*
* 比如坐标类型:(x,y)
*/
struct Point {
ll x, y;
};
array<ll, 2> a[1000100];
int main() {
Point p1 = {1, 2};
array<ll, 2> arr = {3, 4}; // array 本身就是一个数组
arr[0] = 5;
arr[1] = 6;
cout << arr[0] << " " << arr[1] << endl;
return 0;
}
这里空空如也











有帮助,赞一个