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. }

advertisement
$ 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
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn