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.
/*
* C++ Program to illustrate usage of vector container
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
int main()
{
std::vector< std::string > StringVector;
std::vector< std::string >::const_iterator i;
// Pushing string elements in Container
StringVector.push_back("Python");
StringVector.push_back("Java");
StringVector.push_back("Haskell");
StringVector.push_back("C++");
StringVector.push_back("Ruby");
StringVector.push_back("JavaScript");
// Sorting container strings lexicographically
sort(StringVector.begin(), StringVector.end());
std::cout << "Sorted List of Programming Languages"
<< std::endl;
// Printing sorted container elements
for (i = StringVector.begin(); i != StringVector.end(); i++)
std::cout << *i << " ";
std::cout << std::endl;
return 0;
}
$ ./a.out
Sorted List of Programming Languages
C++ Haskell Java JavaScript Python Ruby
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs