Use the fill_n algorithms: Fill 3 more positions with Y's : fill_n « STL Algorithms Modifying sequence operations « C++






Use the fill_n algorithms: Fill 3 more positions with Y's

 
 



#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_n(vector1.begin() + 5, 3, 'Y');

  vector<char>::iterator pos;

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


  return 0;
}

 /* 
H e l l o Y Y Y e r e 
 */       
  








Related examples in the same category

1.Use std::fill_n to fill the first five elements of chars with 'A'
2.Use fill_n function to insert 'hello' nine times
3.Use fill_n function to replace all but two elements with 'hi'