use_pair 以你之姓@ 2022-06-08 13:44 243阅读 0赞 //pair常见的使用方式 #include<iostream> #include <utility> #include <string> #include <tuple> using namespace std; int main(){ pair<string,double> product1; pair<string,double> product2("tomatoes",2.30);; pair<string,double> product3(product2); product1 = make_pair(string("lightbulbs"),0.99); product2.first = "Shoes"; product2.second = 39.09; cout << product1.first << " " << product1.second << endl; cout << product2.first << " " << product2.second << endl; cout << product3.first << " " << product3.second << endl; pair<string,int> planet,homeplanet; planet = make_pair("Earth",6371); homeplanet = planet; cout << "Home Planet: " << homeplanet.first << endl; cout << "Planet size: " << homeplanet.second << endl; planet.swap(homeplanet); cout << "Planet Planet: " << planet.first << endl; cout << "Planet size: " << planet.second << endl; pair<int,char> foo(10,'x'); get<0>(foo) = 50; cout << "foo contains "; cout << get<0>(foo) << " and " << get<1>(foo) << endl; tuple<int,char> mytuple(10,'a'); get<0>(mytuple) = 20; cout << "mytuple contains: "; cout << get<0>(mytuple) << " and " << get<1>(mytuple) << endl; cout << endl; return 0; }
还没有评论,来说两句吧...