zoukankan      html  css  js  c++  java
  • 整理下STL algorithms(2)

    copy:

    #include <algorithm>
    template< class InputIterator, class OutputIterator >
    OutputIterator copy( InputIterator first, InputIterator last, OutputIterator d_first );
    该方法会将first到last之间的元素拷贝到d_first之中~~
    相当于如下实现:
    template<class InputIterator, class OutputIterator>
    OutputIterator copy(InputIterator first, InputIterator last, OutputIterator d_first)
    {
    while (first != last) {
    *d_first++ = *first++;
    }
    return d_first;
    }
    copy_backward:
    是从后边拷贝~~
    #include <algorithm>
    #include
    <iostream>

    int main()
    {
    std::vector
    <int> from_vector;
    for (int i = 0; i < 10; i++) {
    from_vector.push_back(i);
    }

    std::vector
    <int> to_vector(15);

    std::copy_backward(from_vector.begin(), from_vector.end(), to_vector.end());

    std::cout
    << "to_vector contains: ";
    for (unsigned int i = 0; i < to_vector.size(); i++) {
    std
    ""cout << to_vector[i] << " ";
    }

    return 0;
    }

    to_vector contains:
    0 0 0 0 0 0 1 2 3 4 5 6 7 8 9
    fill:
    #include <algorithm>
     
    template< class ForwardIterator, class T >
    void fill( ForwardIterator first, ForwardIterator last, const T& value );
    使用值value填充first和last之间的元素~~
    fill_n:
    template < class OutputIterator, class Size, class T >
      void fill_n ( OutputIterator first, Size n, const T& value );
    填充n个value值~~~
    transform:
    template< class InputIterator, class OutputIterator, class UnaryOperation >
    OutputIterator transform( InputIterator first1, InputIterator last1,
                              OutputIterator d_first, UnaryOperation unary_op );
     
    template< class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation >
    OutputIterator transform( InputIterator first1, InputIterator last1,
                              InputIterator first2, OutputIterator d_first, BinaryOperation binary_op );
    将unary_op定义的操作用于first1和last1之间的元素,将结果存放在d_first开始的迭代器~~
    first2是第二个序列的起始元素~
    如下的字符串转大写方法~:
    #include <string>
    #include
    <algorithm>
    #include
    <iostream>

    int main()
    {
    std::
    string s("hello");
    std::transform(s.begin(), s.end(), s.begin(), (
    int (*)(int))toupper);
    std::cout
    << s;

    return 0;
    }
    注意对函数的转换~~
    generate:
    template< class ForwardIterator, class Generator >
    void generate( ForwardIterator first, ForwardIterator last, Generator g );
    使用g产生的元素填充first到last之间的元素
    可以使用如下代码产生随机序列~
    #include <algorithm>
    #include
    <iostream>
    #include
    <cstdlib>

    int main()
    {
    std::vector
    <int> v(5);
    std::generate(v.begin(), v.end(), std::rand);
    // Using the C function rand()

    std::cout
    << "v: ";
    for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
    std::cout
    << v[i] << ' ';
    }

    return 0;
    }

    类似的是generate_n:
    template< class OutputIterator, class Size, class Generator >
    void generate_n( OutputIterator first, Size count, Generator g );
    remove:
    template < class ForwardIterator, class T >
      ForwardIterator remove ( ForwardIterator first, ForwardIterator last,
                               const T& value );
    删除first和last之间值为value的元素~~
    类似的有remove_if:
    template< class ForwardIterator, class UnaryPredicate >
    ForwardIterator remove_if( ForwardIterator first, ForwardIterator last, UnaryPredicate p );
    还有remove_copy:
    template< class InputIterator, class OutputIterator, class T >
    OutputIterator remove_copy( InputIterator first,
                                InputIterator last,
                                OutputIterator d_first,
                                const T& value );
    还有remove_copy_if
    replace:
    template< class ForwardIterator, class T >
    void replace( ForwardIterator first, ForwardIterator last,
                  const T& old_value, const T& new_value );
    替换操作,类似的有:replace_ifreplace_copyreplace_copy_if.
    swap:
    template <class T> void swap ( T& a, T& b );
    交换a和b的值~~
    template <class T> void swap ( T& a, T& b )
    {
    T c(a); a
    =b; b=c;
    }
    类似的有swap_ranges:
    template < class ForwardIterator1, class ForwardIterator2 >
      ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,
                                     ForwardIterator2 first2 );
    vector<int> first (5,10); // first: 10 10 10 10 10
    vector<int> second (5,33); // second: 33 33 33 33 33
    vector<int>::iterator it;

    swap_ranges(first.begin()
    +1, first.end()-1, second.begin());

    结果为:
    first contains: 10 33 33 33 10
    second contains: 10 10 10 33 33
    iter_swap:
    template <class ForwardIterator1, class ForwardIterator2>
      void iter_swap ( ForwardIterator1 a, ForwardIterator2 b );
    交换两个迭代器的值~~
    partition
    template <class BidirectionalIterator, class Predicate>
      BidirectionalIterator partition ( BidirectionalIterator first,
                                        BidirectionalIterator last, Predicate pred );
    将first和last之间的元素划分为两部分,前一部分是满足pred的,后一部分的不满足的。返回值是中间的分隔元素
    该方法会打乱同类元素的顺序,而stable_partition能保持顺序~
    reverse(反转):
    template< class BidirectionalIterator >
    void reverse( BidirectionalIterator first, BidirectionalIterator last );
    reverse_copy:
    template <class BidirectionalIterator, class OutputIterator>
      OutputIterator reverse_copy ( BidirectionalIterator first,
                                    BidirectionalIterator last, OutputIterator result );
    该方法不会改变原来的序列~~
    rotate(旋转):
    template <class ForwardIterator>
      void rotate ( ForwardIterator first, ForwardIterator middle,
                    ForwardIterator last );
    类似的有rotate_copy
    random_shuffle(随机打乱):
    template <class RandomAccessIterator>
      void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last );
    
    template <class RandomAccessIterator, class RandomNumberGenerator>
      void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
                            RandomNumberGenerator& rand );
    unique(去除重复元素):
    template< class ForwardIterator >
    ForwardIterator unique( ForwardIterator first, ForwardIterator last );
     
    template< class ForwardIterator, class BinaryPredicate >
    ForwardIterator unique( ForwardIterator first, ForwardIterator last, BinaryPredicate p );
    相应的有unique_copy





  • 相关阅读:
    [Android]Parcelable encountered IOException writing serializable object (name = xxx)
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    poj3070--Fibonacci(矩阵的高速幂)
    Java泛型解析(03):虚拟机运行泛型代码
    PHP中的替代语法
    在ubuntu上部署hadoop时出现的问题
    js闭包(函数内部嵌套一个匿名函数:这个匿名函数可将所在函数的局部变量常驻内存)
    js匿名函数(变量加括号就是函数)
    javascript进阶课程--第三章--匿名函数和闭包
    js实现科学计算机
  • 原文地址:https://www.cnblogs.com/macula7/p/2014172.html
Copyright © 2011-2022 走看看