pair容器

pair容器
#include "iostream"
#include "string"
#include <tuple> //std::tie
using namespace std;
pair<string, int> getPreson() {
    return make_pair("Sven", 25);
}

int main(){

    //初始化
    string name;
    int ages;
    string v1 = "Hello";
    int v2 = 1;
    pair<string, int> p1(v1, v2);
    pair<string, int> p2 = make_pair(v1+"o", v2+1);
    pair<string, string> author("James","Joy");
    pair<string, int> name_age("Tom", 18);
    pair<string, int> name_age2(name_age);
    tie(name, ages) = getPreson();

    //访问
    cout << (p1 < p2) << endl;
    cout << (p1 == p2) << endl;
    cout << p1.first << endl;
    cout << p1.second << endl;
    cout << p2.first << endl;
    cout << p2.second << endl;

    for (auto &item : p1.first)
        cout << item << ' ';
    cout << endl << endl;
    p2 = p1;
    cout << p2.first << endl;
    cout << p2.second << endl << endl;
    cout << "name: " << name << ", ages: " << ages << endl;

    return 0;
}
posted @ 2021-12-06 21:02  ayanyuki  阅读(57)  评论(0)    收藏  举报