集合的转换
C++ STL集合的互相转换
1、遍历
Example:
for each(auto it : list){
//赋值操作
}
2、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
实现:
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));
网友评论