fork download
  1. #include <utility> // std::pair, std::make_pair
  2. #include <string> // std::string
  3. #include <iostream> // std::cout
  4.  
  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.  
  10. product1 = std::make_pair(std::string("lightbulbs"),0.99); // using make_pair (move)
  11.  
  12. product2.first = "shoes"; // the type of first is string
  13. product2.second = 39.90; // the type of second is double
  14.  
  15. std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
  16. std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
  17. std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
  18. return 0;
  19. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
The price of lightbulbs is $0.99
The price of shoes is $39.9
The price of tomatoes is $2.3