小说网站有源码了该怎么做,网站开发微信支付功能,wordpress4.8.0,软件开发模型的理解pair的作用
C 中的 std::pair 是标准模板库 (STL) 提供的一个容器#xff0c;它能够存储两个不同类型的数据作为一个整体#xff0c;其中first#xff1a;访问 pair 的第一个元素。second#xff1a;访问 pair 的第二个元素。
int main() {pairstring, int p;//通…pair的作用
C 中的 std::pair 是标准模板库 (STL) 提供的一个容器它能够存储两个不同类型的数据作为一个整体其中first访问 pair 的第一个元素。second访问 pair 的第二个元素。
int main() {pairstring, int p;//通过构造函数参数列表初始化p make_pair(张三, 18);coutp.first p.secondendl;//打印结果 张三18// 初始化的时候赋值pairstring, int pname(张三, 18);coutpname.first pname.secondendl;//打印结果 张三18return 0;
} 使用typedef
#include iostream
#include string
using namespace std;
typedef pairstring,int pp;
pp p1 make_pair(张三, 18);
pp p2(张三, 18);
int main() {coutp1.first p1.secondendl;coutp2.first p2.secondendl;return 0;
}
pair 用在结构体中
#include iostream
#include string
using namespace std;
struct config{pairstring, int p;// 构造函数初始化config() : p{张三, 18} {coutp.first p.secondendl;}};int main() {config c;return 0;
}
还可以pair 与结构体绑定
#include iostream
#include string
using namespace std;struct config{pairstring, int p;// 构造函数初始化config() : p{张三, 18} {coutp.first p.secondendl;}};int main() {config c;// 直接访问config结构体内的pair成员std::cout Integer value: c.p.second , String value: c.p.first std::endl;// 或者利用C17的结构化绑定来访问auto [strValue, intValue] c.p;std::cout Integer value: intValue , String value: strValue std::endl;return 0;
}
pair 还可以用来 拷贝、赋值和比较
std::pairint, std::string copyOfPair(myPair); // 拷贝构造
copyOfPair anotherPair; // 赋值操作if (myPair anotherPair) { // 使用内置的等于运算符进行比较// ...
}