stl中常用的函数

copy

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

source code

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

accumulate

  • first version
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );

source code

template<class InputIt, class T>
T accumulate(InputIt first, InputIt last, T init)
{
    for (; first != last; ++first) {
        init = init + *first;
    }
    return init;
}
  • second version
template< class InputIt, class T, class BinaryOperation >
T accumulate( InputIt first, InputIt last, T init,
              BinaryOperation op );

source code

template<class InputIt, class T, class BinaryOperation>
T accumulate(InputIt first, InputIt last, T init, 
             BinaryOperation op)
{
    for (; first != last; ++first) {
        init = op(init, *first);
    }
    return init;
}

iota

template< class ForwardIterator, class T >
void iota( ForwardIterator first, ForwardIterator last, T value );

source code

template<class ForwardIterator, class T>
void iota(ForwardIterator first, ForwardIterator last, T value)
{
    while(first != last) {
        *first++ = value;
        ++value;
    }
}
阅读更多
文章标签: stl
个人分类: C++关键字
上一篇C++转移构造函数与转移赋值函数
下一篇C++ STL
想对作者说点什么? 我来说一句

C++ STL(竞赛常用部分)

haha

ccDLlyy ccDLlyy

2016-11-21 21:51:48

阅读数:1291

没有更多推荐了,返回首页

关闭
关闭