STL 日积月累

1、std::copy

template <class InputIterator, class OutputIterator>
OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result );
 

把[first,last)范围内的元素拷贝到以result为起点的另一块内存区域里。

返回值:目标区域尾部的迭代器,这个迭代器指向最后一个元素后面的位置。

该函数模板行为等价于:

template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
  {
  while (first!=last) *result++ = *first++;
  return result;
  }

如果源区域和目的区域有重叠,即result指向的是[first,last)之间的区域,则应该调用copy_backward,正如其名,这个copy是从反向进行的。

template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                         BidirectionalIterator1 last,
                                         BidirectionalIterator2 result )
{
  while (last!=first) *(--result) = *(--last);
  return result;
}

copy和输出流迭代器结合使用的例子:

#include <iostream>
#include <iterator>
#include <vector>

#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
    vector<int> test;
    for(int i=0;i<10;i++)
        test.push_back(i);
    vector<int>::iterator iter;
    copy(test.begin(),test.end(),ostream_iterator<int>(cout,"\n"));
    system("pause");
    return 0;
}

2、std::transform

版本1:

template < class InputIterator, class OutputIterator, class UnaryOperator >
OutputIterator transform ( InputIterator first1, InputIterator last1,
                           OutputIterator result, UnaryOperator op );

版本2:

template < class InputIterator1, class InputIterator2, 
           class OutputIterator, class BinaryOperator >
OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
                           InputIterator2 first2, OutputIterator result,
                           BinaryOperator binary_op );

第一个版本针对一元操作符。对[first1,last1)之间的所有元素应用操作op,把结果存储在result指向的内存区域里。

第二个版本针对二元操作符,第二个操作数的起始位置为first2。

存储结果和存储操作数可以用同一块区域。即result和first1或first2可以相同。

返回值:result所指区域的最后一个元素的后面位置。

例子:

// transform algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int op_increase (int i) { return ++i; }
int op_sum (int i, int j) { return i+j; }

int main () {
  vector<int> first;
  vector<int> second;
  vector<int>::iterator it;

  // set some values:
  for (int i=1; i<6; i++) first.push_back (i*10); //  first: 10 20 30 40 50

  second.resize(first.size());     // allocate space
  transform (first.begin(), first.end(), second.begin(), op_increase);
                                                  // second: 11 21 31 41 51

  transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum);
                                                  //  first: 21 41 61 81 101

  cout << "first contains:";
  for (it=first.begin(); it!=first.end(); ++it)
    cout << " " << *it;

  cout << endl;
  return 0;
}

3、std::distance

template< class InputIterator >

typename std::iterator_traits<InputIterator>::difference_type distance( InputIterator first, InputIterator last );

返回值:first和last之间的元素个数。

如果first和last是随机访问的迭代器,这个函数使用减法操作符来计算元素个数。否则该函数通过不断移动迭代器来计算。

4、std::back_inserter

template <class Container>

back_insert_iterator<Container> back_inserter (Container& x);

back_inserter为容器类对象x创建一个尾部插入迭代器(back_insert_iterator)。

有时候一个容器里的元素还需要使用,又要在后面继续插入新元素,back_inserter就是专门为这种情形设计的一类特殊的向尾部输出的迭代器。

返回值:容器对象x的back_insert_iterator。

例子:

// back_inserter example
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;

int main () {
  vector<int> firstvector, secondvector;
  for (int i=1; i<=5; i++)
  { firstvector.push_back(i); secondvector.push_back(i*10); }

  copy (secondvector.begin(),secondvector.end(),back_inserter(firstvector));

  vector<int>::iterator it;
  for ( it = firstvector.begin(); it!= firstvector.end(); ++it )
	  cout << *it << " ";
  cout << endl;

  return 0;
}

运行结果:1 2 3 4 5 10 20 30 40 50

posted on 2012-02-06 14:07  youthlion  阅读(527)  评论(0编辑  收藏  举报

导航