C++ Program to Implement Pairs in STL

This C++ Program demonstrates implementation of Pairs in STL.

Here is source code of the C++ Program to demonstrate Pairs in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Pairs in Stl
  3.  */
  4. #include <iostream>
  5. #include <utility>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     pair <string,double> product1;
  12.     pair <string,double> product2 ("tomatoes",2.30);
  13.     pair <string,double> product3 (product2);
  14.     product1 = make_pair(string("lightbulbs"),0.99);
  15.  
  16.     product2.first = "shoes";       
  17.     product2.second = 39.90; 
  18.     cout<<"The price of "<<product1.first<<" is $"<<product1.second <<endl;
  19.     cout<<"The price of "<<product2.first<<" is $"<<product2.second <<endl;
  20.     cout<<"The price of "<<product3.first<<" is $"<<product3.second <<endl;
  21.     return 0;
  22. }

$ g++ pairs.cpp
$ a.out
The price of lightbulbs is $0.99
The price of shoes is $39.9
The price of tomatoes is $2.3
 
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.