/* generates: original element sequence of first container: 0 1 2 3 4 5 6 7 8 9 original element sequence of second container: 5 6 7 8 9 array after swap_ranges() in middle of array: 5 6 7 8 9 0 1 2 3 4 first container after swap_ranges() of two vectors: 5 6 7 8 9 5 6 7 8 9 second container after swap_ranges() of two vectors: 0 1 2 3 4 */ int main() { int ia[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int ia2[] = { 5, 6, 7, 8, 9 }; vector< int, allocator > vec( ia, ia+10 ); vector< int, allocator > vec2( ia2, ia2+5 ); ostream_iterator< int > ofile( cout, " " ); cout << "original element sequence of first container:\n"; copy( vec.begin(), vec.end(), ofile ); cout << '\n'; cout << "original element sequence of second container:\n"; copy( vec2.begin(), vec2.end(), ofile ); cout << '\n'; // swap within the same sequence swap_ranges( &ia[0], &ia[5], &ia[5] ); cout << "array after swap_ranges() in middle of array:\n"; copy( ia, ia+10, ofile ); cout << '\n'; // swap across containers vector< int, allocator >::iterator last = find( vec.begin(), vec.end(), 5 ); swap_ranges( vec.begin(), last, vec2.begin() ); cout << "first container after swap_ranges() of two vectors:\n"; copy( vec.begin(), vec.end(), ofile ); cout << '\n'; cout << "second container after swap_ranges() of two vectors:\n"; copy( vec2.begin(), vec2.end(), ofile ); cout << '\n'; }
static range<I> rotate_gcd(I begin, I middle, I end) { auto const m1 = middle - begin; auto const m2 = end - middle; if(m1 == m2) { swap_ranges(begin, middle, middle); return {middle, end}; } auto const g = rotate_fn::gcd(m1, m2); for (I p = begin + g; p != begin;) { iterator_value_t<I> t = iter_move(--p); I p1 = p; I p2 = p1 + m1; do { *p1 = iter_move(p2); p1 = p2; auto const d = end - p2; if(m1 < d) p2 += m1; else p2 = begin + (m1 - d); } while(p2 != p); *p1 = std::move(t); } return {begin + m2, end}; }
void pure_numeric_algo(){ cout<<endl<<"pure_numeric_algo :"<<endl; int ia[11] = {0, 1, 2, 3, 4, 5, 6,6,6, 7, 8 }; vector<int> iv(ia,ia+11); vector<int> iv2(ia+6,ia+8); vector<int>::iterator itr; itr = adjacent_find(iv.begin(),iv.end(), equal_to<int>()); //找到相邻元素相等的第一个元素 cout<<"adjacent_find: "<<*itr<<endl; cout<<"count: "<<count(iv.begin(),iv.end(), 6)<<endl; //找到元素值等于6的个数 cout<<"count_if: "<<count_if(iv.begin(),iv.end(), bind2nd(less<int>() , 7))<<endl; //找到小于7的元素个数 itr = find(iv.begin(),iv.end(), 4); //找到元素等于4的第一个元素位置 cout<<"find: "<<*itr<<endl; itr = find_if(iv.begin(),iv.end(), bind2nd(greater<int>() , 2)); //找到元素大于2的第一个元素位置 cout<<"find_if: "<<*itr<<endl; itr = find_end(iv.begin(),iv.end(), iv2.begin(),iv2.end()); //找到iv序列中最后子序列匹配出现的位置 cout<<"find_end: "<<*(itr+3)<<endl; itr = find_first_of(iv.begin(),iv.end(), iv2.begin(),iv2.end()); //找到iv序列中最先子序列匹配出现的位置 cout<<"find_end: "<<*(itr+3)<<endl; remove(iv.begin(),iv.end(), 6); //删除元素,向前移,但是容器size不变,后面会剩余数据 cout<<"remove: "<<iv<<endl; vector<int> iv3(12,-1); remove_copy(iv.begin(),iv.end(), iv3.begin(), 6); //删除元素,将数据拷贝到新容器,后面会剩余数据 cout<<"remove_copy: "<<iv3<<endl; remove_if(iv.begin(),iv.end(), bind2nd(less<int>(), 6)); //删除小于6的元素,后面会剩余数据 cout<<"remove_if: "<<iv<<endl; remove_copy_if(iv.begin(),iv.end(), iv3.begin(), bind2nd(less<int>(), 7)); //删除小于7的元素,并拷贝到新容器 cout<<"remove_copy_if: "<<iv3<<endl; replace(iv.begin(),iv.end(), 6, 3); //将所有元素值为6的改为3 cout<<"replace: "<<iv<<endl; replace_copy(iv.begin(),iv.end(),iv3.begin(), 3, 5); //将所有元素值为3的改为5,结果保存在新容器中 cout<<"replace_copy: "<<iv3<<endl; replace_if(iv.begin(),iv.end(), bind2nd(less<int>(),5), 2); //将所有元素值小于5的改为2 cout<<"replace_if: "<<iv<<endl; replace_copy_if(iv.begin(),iv.end(),iv3.begin(), bind2nd(equal_to<int>(),8), 9); //将所有元素值为8的改为9,结果保存在新容器中 cout<<"replace_copy_if: "<<iv3<<endl; reverse(iv.begin(),iv.end()); cout<<"reverse: "<<iv<<endl; //反转 reverse_copy(iv.begin(),iv.end(),iv3.begin()); cout<<"reverse_copy: "<<iv3<<endl; //反转,结果保存在新容器 rotate(iv.begin(),iv.begin() + 4, iv.end()); cout<<"rotate: "<<iv<<endl; //互换元素 rotate_copy(iv.begin(),iv.begin() + 5,iv.end(),iv3.begin()); cout<<"rotate_copy: "<<iv3<<endl; //互换元素,结果保存在新容器 int ia2[] = {2, 8}; vector<int> iv4(ia2,ia2+2); cout<<"search: "<<*search(iv.begin(),iv.end(),iv4.begin(),iv4.end())<<endl; //查找子序列出现的第一次出现地点 swap_ranges(iv4.begin(),iv4.end(),iv.begin()); //按区域交换 cout<<"swap_ranges: "<<iv<<endl<<iv4<<endl; transform(iv.begin(),iv.end(),iv.begin(),bind2nd(minus<int>(), 2)); //所有元素减2 cout<<"transform: "<<iv<<endl; transform(iv4.begin(),iv4.end(),iv.begin(),iv4.begin(),plus<int>()); //区间对应元素相加 cout<<"transform: "<<iv4<<endl; /************************************************************************/ vector<int> iv5(ia,ia+11); vector<int> iv6(ia+4,ia+8); vector<int> iv7(15); cout<<"max_element: "<<*max_element(iv5.begin(), iv5.end())<<endl; //最大元素游标 cout<<"min_element: "<<*min_element(iv5.begin(), iv5.end())<<endl; cout<<"includes: "<<includes(iv5.begin(),iv5.end(),iv6.begin(),iv6.end())<<endl; //iv6中元素是不是都在iv5中,这两个必须排过序 merge(iv5.begin(),iv5.end(),iv6.begin(),iv6.end(),iv7.begin()); //两个排序号的容器合并 cout<<"merge: "<<iv7<<endl; partition(iv7.begin(),iv7.end(),bind2nd(equal_to<int>(), 5)); //满足条件的放在左边,不满足条件的放在右边 cout<<"partition: "<<iv7<<endl; unique(iv5.begin(),iv5.end()); //去重,重复的元素放在后面 cout<<"unique: "<<iv5<<endl; unique_copy(iv5.begin(),iv5.end(),iv7.begin()); //去重,结果保存在新容器 cout<<"unique_copy: "<<iv7<<endl; }
void RunSwapRanges() { { // seq SwapRangesAlgoTest<_IterCat> _Alg; swap_ranges(seq, _Alg.begin_in(), _Alg.end_in(), _Alg.begin_dest()); } { //par SwapRangesAlgoTest<_IterCat> _Alg; swap_ranges(par, _Alg.begin_in(), _Alg.end_in(), _Alg.begin_dest()); } { //vec SwapRangesAlgoTest<_IterCat> _Alg; swap_ranges(vec, _Alg.begin_in(), _Alg.end_in(), _Alg.begin_dest()); } }
void swap(array<U, N>& that) { swap_ranges(begin(), end(), that.begin()); }