Use the generic fill algorithms: Fill first 5 positions of vector1 with X's : fill « STL Algorithms Modifying sequence operations « C++ Tutorial






#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

int main()
{
  string s("Hello there");
  vector<char> vector1(s.begin(), s.end());

  // Fill first 5 positions of vector1 with X's:
  fill(vector1.begin(), vector1.begin() + 5, 'X');

  vector<char>::iterator pos;

  for (pos=vector1.begin(); pos!=vector1.end(); ++pos) {
        cout << *pos << ' ';
  }

  return 0;
}
X X X X X   t h e r e








24.1.fill
24.1.1.Use the generic fill algorithms: Fill first 5 positions of vector1 with X's
24.1.2.Use std::fill to fill vector with chars
24.1.3.Use fill function to overwrite all elements with 'again'
24.1.4.Use fill function to replace the second and up to the last element but one with 'hmmm'
24.1.5.Using fill to set initial values in a vector