STL 之swap, iter_swap, swap_ranges
来源:互联网 发布:淘宝水果模特 编辑:程序博客网 时间:2024/05/15 14:41
作用:交换元素
声明:
- #include <algorithm>
- template<class Type>
- void swap(Type& objcet1, Type& objec2);
- template<class forwardItr1,class forwardItr2>
- void iter_swap(forwardItr1 first,forwardItr2 scecod);
- template<class forwardItr1, class forwardItr2>
- forwardItr2 swap_ranges(forwardItr1 first,forwardItr1 last, forwardItr2 first2);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- char cList[10] = {'A','B','C','D','F','G','H','I','J','K'};
- vector<char> charList(cList,cList+10);
- vector<char>::iterator charItr;
- ostream_iterator<char> screen(cout," ");
- cout << "charList:" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- // 容器内部元素交互
- swap(charList[0],charList[1]);
- cout << "charList.swap" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- // 用迭代器进行交互
- iter_swap(charList.begin() + 2,charList.begin() + 3);
- cout << "charList.iter_swap" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- charItr = charList.begin() + 4;
- iter_swap(charItr,charItr + 1);
- cout << "charList.iter_swap" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- int list[10] = {1,2,3,4,5,6,7,8,9,10};
- vector<int> intList(list,list + 10);
- ostream_iterator<int> screenInt(cout, " ");
- cout << "intList:" << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- swap_ranges(intList.begin(),intList.begin()+4,intList.begin()+5);
- cout << "intList.swap_ranges:" << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- // 不同容器之间元素交互
- swap_ranges(list,list+10,intList.begin());
- cout << "list:" << endl;
- copy(list,list+10,screenInt);
- cout << endl;
- cout << "intList: " << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- return 0;
- }
运行结果:
charList:
A B C D F G H I J K
charList.swap
B A C D F G H I J K
charList.iter_swap
B A D C F G H I J K
charList.iter_swap
B A D C G F H I J K
intList:
1 2 3 4 5 6 7 8 9 10
intList.swap_ranges:
6 7 8 9 5 1 2 3 4 10
list:
6 7 8 9 5 1 2 3 4 10
intList:
1 2 3 4 5 6 7 8 9 10
0 0
- STL 之swap, iter_swap, swap_ranges
- STL 之swap, iter_swap, swap_ranges
- STL algorithm算法swap,swap_ranges(56)
- 变异算法之交换(swap, swap_ranages, iter_swap)
- C++ STL算法系列之十六:iter_swap
- swap_ranges算法(STL)
- STL algorihtm算法iter_swap(29)
- swap_ranges
- 简单的程序诠释C++ STL算法系列之十七:swap_ranges
- 简单的程序诠释C++ STL算法系列之十六:iter_swap
- 简单的程序诠释C++ STL算法系列之十六:iter_swap
- STL排序算法之swap()
- c++ stl 数据交换算法swap_ranges
- iter_swap
- iter_swap
- 泛型算法系列14:random_shuffle()&&iter_swap()&&swap()
- C++标准库笔记:算法--min/max/swap/iter_swap
- C++ STL算法系列之swap
- 使用QQ账号,新浪微博账号登录第三方应用
- 开幕式,闭幕式
- WWDC2014之App Extensions学习笔记
- STL 之search,search_n,sort,binary_search
- uiview 动画旋转rolata
- STL 之swap, iter_swap, swap_ranges
- java.lang.OutOfMemoryError
- Oracle学习笔记(12)------------集合、序列
- Android学习笔记--文件存储数据
- Win7下安装HBase
- You need to use a Theme.AppCompat theme (or descendant) with this activity.
- 字符消除
- STL 之replace,replace_if,replace_copy,replace_copy_if
- 怎么样自定义ecplise中代码提示