C++ Program to Illustrate Usage of Vector Container

This C++ program illustrates usage of the vector container. The strings can be pushed back onto the vector container and can be sorted using sort algorithm from the library. The sorted strings are then displayed by iterating through the strings in the vector container.

Here is the source code of the C++ program illustrates how to pass a variable to a function by value. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to illustrate usage of vector container
  3.  */
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <algorithm>
  8. #include <iomanip>
  10. int main()
  11. {
  12.     std::vector< std::string > StringVector;
  13.     std::vector< std::string >::const_iterator i;
  15.     // Pushing string elements in Container
  16.     StringVector.push_back("Python");
  17.     StringVector.push_back("Java");
  18.     StringVector.push_back("Haskell");
  19.     StringVector.push_back("C++");
  20.     StringVector.push_back("Ruby");
  21.     StringVector.push_back("JavaScript");
  22.     // Sorting container strings lexicographically
  23.     sort(StringVector.begin(), StringVector.end());
  24.     std::cout << "Sorted List of Programming Languages"
  25.         << std::endl;
  26.     // Printing sorted container elements
  27.     for (i = StringVector.begin(); i != StringVector.end(); i++)
  28.         std::cout << *i << "  ";
  29.     std::cout << std::endl;
  30.     return 0;
  31. }

$ ./a.out
Sorted List of Programming Languages
C++  Haskell  Java  JavaScript  Python  Ruby

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

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

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.