//Cách 1 : Giá trị của first và second là mặc định
pair<first_data_type, second_data_type> pair_name;
//Cách 2 : Giá trị của first là value1, second là value2
pair<first_data_type, second_data_type> pair_name = make_pair(value1, value2)
//Cách 3 : Giá trị của first là value1, second là value2
pair<first_data_type, second_data_type> pair_name(value1, value2)
//Cách 4 : Giá trị của first là value1, second là value2
pair<first_data_type, second_data_type> pair_name = {value1, value2}
#include <iostream>
#include <utility>
using namespace std;
int main(){
pair<int, int> a = make_pair(28, 100);
cout << a.first << ' ' << a.second << endl;
pair<char, int> b = {'@', 28};
cout << b.first << ' ' << b.second << endl;
pair<char, char> c('#', '$');
cout << c.first << ' ' << c.second << endl;
return 0;
}
28 100
@ 28
# $
#include <iostream>
#include <utility>
using namespace std;
int main(){
pair<int, pair<char, int> > p1 = make_pair(28, make_pair('@', 100));
cout << p1.first << endl;
cout << p1.second.first << ' ' << p1.second.second << endl;
pair<pair<int, int>, pair<int, int>> p2 = {{10, 20}, {30, 40}};
cout << p2.first.first << ' ' << p2.first.second << endl;
cout << p2.second.first << ' ' << p2.second.second << endl;
return 0;
}
28
@ 100
10 20
30 40
#include <iostream>
#include <utility>
using namespace std;
int main(){
pair<string, int> p = make_pair("28tech", 28);
pair<string, int> p1 = p;
cout << p1.first << ' ' << p1.second << endl;
return 0;
}
28tech 28
#include <iostream>
#include <utility>
using namespace std;
int main(){
pair<int, int> p1 = {10, 20};
pair<int, int> p2 = {10, 21};
pair<int, int> p3 = {5, 35};
cout << boolalpha << (p1 == p2) << endl;
cout << boolalpha << (p1 != p2) << endl;
cout << boolalpha << (p1 < p2) << endl;
cout << boolalpha << (p1 > p3) << endl;
return 0;
}
false
true
true
true
#include <iostream>
#include <utility>
using namespace std;
int main(){
pair<int, int> p1 = {10, 20};
pair<int, int> p2 = {30, 40};
cout << "Ban dau : \n";
cout << "p1 = {" << p1.first << ", " << p1.second << "}\n";
cout << "p2 = {" << p2.first << ", " << p2.second << "}\n";
p1.swap(p2);
cout << "Sau khi swap : \n";
cout << "p1 = {" << p1.first << ", " << p1.second << "}\n";
cout << "p2 = {" << p2.first << ", " << p2.second << "}\n";
return 0;
}
Ban dau :
p1 = {10, 20}
p2 = {30, 40}
Sau khi swap :
p1 = {30, 40}
p2 = {10, 20}
Tác giả bài viết: Thanh Sơn
Nguồn tin: blog.28tech.com.vn
Ý kiến bạn đọc
Những tin mới hơn
Những tin cũ hơn