Insert characters into vector. An iterator to the inserted object is returned. - C++ STL

C++ examples for STL:vector

Description

Insert characters into vector. An iterator to the inserted object is returned.

Demo Code

#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<char> vect);
int main() {//w  w  w. j  a  va2 s  .c o  m
   // Declare an empty vector that can hold char objects.
   vector<char> v;
   // Declare an iterator to a vector<char>.
   vector<char>::iterator itr;
   // Obtain an iterator to the start of v.
   itr = v.begin();
   // Insert characters into v. An iterator to the inserted object is returned.
   itr = v.insert(itr, 'A');
   itr = v.insert(itr, 'B');
   v.insert(itr, 'C');
   // Display the contents of v.
   show("The contents of v: ", v);
   return 0;
}
// Display the contents of a vector<char> by using an iterator.
void show(const char *msg, vector<char> vect) {
   vector<char>::iterator itr;
   cout << msg;
   for(itr=vect.begin(); itr != vect.end(); ++itr)
      cout << *itr << " ";
   cout << "\n";
}

Result


Related Tutorials