std pair example

Solutions on MaxInterview for std pair example by the best coders in the world

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