声明:

    1. // pair::pair example
    2. #include <utility> // std::pair, std::make_pair
    3. #include <string> // std::string
    4. #include <iostream> // std::cout
    5. int main () {
    6. std::pair <std::string,double> product1; // default constructor
    7. std::pair <std::string,double> product2 ("tomatoes",2.30); // value init
    8. std::pair <std::string,double> product3 (product2); // copy constructor
    9. // product1 = std::make_pair(std::string("lightbulbs"),0.99); // using make_pair (move)
    10. product1 = {"lightbulbs", 0.99};
    11. product2.first = "shoes"; // the type of first is string
    12. product2.second = 39.90; // the type of second is double
    13. std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
    14. std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
    15. std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
    16. return 0;
    17. }

    可用操作符:
    > < >= <= == !=
    pair采用双关键字排序

    可用函数:

    first() 获取第一个元素
    second()

    获取第二个元素