C++ Tutorial/STL Algorithms Iterator/insert iterator

Материал из C\C++ эксперт
Версия от 10:29, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create insert_iterator to element 2

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
   
int main()
{
  vector<int> v;
  vector<int>::iterator itr;
  int i;
   
  for(i=0; i<5; i++)
    v.push_back(i);
   
  itr = v.begin();
  while(itr != v.end())
    cout << *itr++ << endl;
   
  itr = v.begin();
  itr += 2; // point to element 2
   
  // create insert_iterator to element 2
  insert_iterator<vector<int> > i_itr(v, itr);
   
  // insert rather than overwrite
  *i_itr++ = 100;
  *i_itr++ = 200;
   
  cout << "Array after insertion: ";
  itr = v.begin();
  while(itr != v.end())
    cout << *itr++ << " ";
   
  return 0;
}

Demonstrate insert_iterator

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main() {
  vector<int> v;
  vector<int>::iterator itr;
  for(int i=0; i<5; i++)
    v.push_back(i);
  cout << "Original array: ";
  itr = v.begin();
  while(itr != v.end())
    cout << *itr++ << " ";
  cout << endl;
  itr = v.begin();
  itr += 2; // point to element 2
  // create insert_iterator to element 2
  insert_iterator<vector<int> > i_itr(v, itr);
  // insert rather than overwrite
  *i_itr++ = 100;
  *i_itr++ = 200;
  cout << "Array after insertion: ";
  itr = v.begin();
  while(itr != v.end())
    cout << *itr++ << " ";
  return 0;
}
Original array: 0 1 2 3 4
Array after insertion: 0 1 100 200 2 3 4

insert iterator and remove_copy_if

#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char** argv)
{
  vector<int> vectorOne;
  set<int> setOne;
  vectorOne.push_back(1);
  vectorOne.push_back(2);
  vectorOne.push_back(3);
  vectorOne.push_back(4);
  
  insert_iterator<set<int> > inserter(setOne, setOne.begin());
  remove_copy_if(vectorOne.begin(), vectorOne.end(), inserter,bind2nd(equal_to<int>(), 100));
  copy(setOne.begin(), setOne.end(), ostream_iterator<int>(cout, " "));
  cout << endl;
  return (0);
}

insert_iterator from vector: set the position

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
   
int main()
{
  vector<int> v;
  vector<int>::iterator itr;
  int i;
   
  for(i=0; i<5; i++)
    v.push_back(i);
   
  itr = v.begin();
  while(itr != v.end())
    cout << *itr++ << endl;
   
  itr = v.begin();
  itr += 2; // point to element 2
   
  // create insert_iterator to element 2
  insert_iterator<vector<int> > i_itr(v, itr);
   
  // insert rather than overwrite
  *i_itr++ = 100;
  *i_itr++ = 200;
   
  itr = v.begin();
  while(itr != v.end())
    cout << *itr++ << " ";
   
  return 0;
}