Vector to string objects, push_back(), and [] - C++ STL

C++ examples for STL:vector

Description

Vector to string objects, push_back(), and []

Demo Code

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()/*from   w  w  w.j  a v  a 2s.c  om*/
{
   vector<string> vectStrings;
   string word;
   char ch;
   do {
      cout << "Enter a word: ";
      cin >> word;
      vectStrings.push_back(word);
      cout << "Enter another ('y' or 'n'): ";
      cin >> ch;
   } while(ch == 'y');
   sort( vectStrings.begin(), vectStrings.end() );
   for(int k=0; k<vectStrings.size(); k++)
      cout << vectStrings[k] << endl;
   return 0;
}

Result


Related Tutorials