美文网首页
集合的转换-C++

集合的转换-C++

作者: MR_Model | 来源:发表于2019-05-23 16:56 被阅读0次

    集合的转换

    C++ STL集合的互相转换

    1、遍历

    for each的特性介绍

    Example:

    for each(auto it : list){
        //赋值操作
    }
    

    2、std::transform

    定义:

    std::transform的介绍


    实现1:

    template<class _InIt,
    class _OutIt,
    class _Fn1> inline
    _OutIt transform(_InIt _First, _InIt _Last,
        _OutIt _Dest, _Fn1 _Func)
    {   // transform [_First, _Last) with _Func
    _DEPRECATE_UNCHECKED(transform, _Dest);
    return (_Transform_no_deprecate(_First, _Last, _Dest, _Func));
    } 
    

    Example:

    include <algorithm>
    std::transform(test1.begin(), test1.end(), 
    std::insert_iterator<std::list<int>>(test2, test2.begin()), [](const int & i) {return i;});
    

    实现2:

    template<class _InIt,
    class _OutIt,
    class _Fn1> inline
    _OutIt transform(_InIt _First, _InIt _Last,
        _OutIt _Dest, _Fn1 _Func)
    {   // transform [_First, _Last) with _Func
    _DEPRECATE_UNCHECKED(transform, _Dest);
    return (_Transform_no_deprecate(_First, _Last, _Dest, _Func));
    } 
    

    Example:

    include <algorithm>
    std::set<int> test1 = { 0,1,2,3,4,5,6 };
    std::vector<int> test2 = { 0,1,2,3,4,5,6,7,8 };
    std::list<int> test3;
    
    std::transform(test1.begin(), test1.end(),
        test2.begin(),
        std::insert_iterator<std::list<int>>(test3, test3.begin()),
        [](const int& i, const int& j) {return i*j; });
    

    3、std::copy

    std::copy的介绍

    实现:

    template<class _InIt,
    class _OutIt> inline
    _OutIt copy(_InIt _First, _InIt _Last,
        _OutIt _Dest)
    {   // copy [_First, _Last) to [_Dest, ...)
    _DEPRECATE_UNCHECKED(copy, _Dest);
    return (_Copy_no_deprecate(_First, _Last, _Dest));
    }
    

    Example:

    std::copy(set.begin(), set.end(), std::back_inserter(list));

    相关文章

      网友评论

          本文标题:集合的转换-C++

          本文链接:https://www.haomeiwen.com/subject/yiybzqtx.html