作用:交换元素

声明:

  1. #include <algorithm>
  2. template<class Type>
  3. void swap(Type& objcet1, Type& objec2);
  4. template<class forwardItr1,class forwardItr2>
  5. void iter_swap(forwardItr1 first,forwardItr2 scecod);
  6. template<class forwardItr1, class forwardItr2>
  7. forwardItr2 swap_ranges(forwardItr1 first,forwardItr1 last, forwardItr2 first2);

示例代码:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <vector>
  7. #include <functional>
  8. #include <algorithm>
  9. using namespace std;
  10. int main() {
  11. char cList[10] = {'A','B','C','D','F','G','H','I','J','K'};
  12. vector<char> charList(cList,cList+10);
  13. vector<char>::iterator charItr;
  14. ostream_iterator<char> screen(cout," ");
  15. cout << "charList:" << endl;
  16. copy(charList.begin(),charList.end(),screen);
  17. cout << endl;
  18. // 容器内部元素交互
  19. swap(charList[0],charList[1]);
  20. cout << "charList.swap" << endl;
  21. copy(charList.begin(),charList.end(),screen);
  22. cout << endl;
  23. // 用迭代器进行交互
  24. iter_swap(charList.begin() + 2,charList.begin() + 3);
  25. cout << "charList.iter_swap" << endl;
  26. copy(charList.begin(),charList.end(),screen);
  27. cout << endl;
  28. charItr = charList.begin() + 4;
  29. iter_swap(charItr,charItr + 1);
  30. cout << "charList.iter_swap" << endl;
  31. copy(charList.begin(),charList.end(),screen);
  32. cout << endl;
  33. int list[10] = {1,2,3,4,5,6,7,8,9,10};
  34. vector<int> intList(list,list + 10);
  35. ostream_iterator<int> screenInt(cout, " ");
  36. cout << "intList:" << endl;
  37. copy(intList.begin(),intList.end(),screenInt);
  38. cout << endl;
  39. swap_ranges(intList.begin(),intList.begin()+4,intList.begin()+5);
  40. cout << "intList.swap_ranges:" << endl;
  41. copy(intList.begin(),intList.end(),screenInt);
  42. cout << endl;
  43. // 不同容器之间元素交互
  44. swap_ranges(list,list+10,intList.begin());
  45. cout << "list:" << endl;
  46. copy(list,list+10,screenInt);
  47. cout << endl;
  48. cout << "intList: " << endl;
  49. copy(intList.begin(),intList.end(),screenInt);
  50. cout << endl;
  51. return 0;
  52. }

运行结果:

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

STL 之swap, iter_swap, swap_ranges相关推荐

  1. C++STL中swap函数操作与内存地址改变的简析

    写在前面 这篇文章主要讨论了STL中swap函数在交换2个容器的内容的时候是交换内存还是交换元素的问题.由于博主对C++的学习并不好,如果有什么错误恳请大家提出.下面会有一些代码展示一下swap函数在 ...

  2. stl swap函数_C ++ STL | vector :: swap()函数与示例

    stl swap函数 C ++ STL vector :: swap()函数 (C++ STL vector::swap() function) vector::swap() function is ...

  3. stl string 源代码分析

    STL中string的源码解读 Ryan peng cutezero@163.com Sunday, June 03, 2007 这是个人最近比较闲暇之余,对SGI STL中string分析,如果有任 ...

  4. 【C++ STL学习笔记】C++ STL序列式容器(array,vector,deque,list)

    文章目录 C++ STL容器是什么? 迭代器是什么,C++ STL迭代器(iterator)用法详解 迭代器类别 迭代器的定义方式 C++序列式容器(STL序列式容器)是什么 容器中常见的函数成员 C ...

  5. c++高级编程学习笔记4

    C++运算符重载 运算符重载概述 根据第 1 章的描述,C++中的运算符是一些类似于+.<.*和<<的符号.这些运算符可应用于内建类型,例如 int 和 double,从而实现算术操 ...

  6. TLD(Tracking-Learning-Detection)学习与源码理解之(四)

    TLD(Tracking-Learning-Detection)学习与源码理解之(四) zouxy09@qq.com http://blog.csdn.net/zouxy09 下面是自己在看论文和这些 ...

  7. Palabos用户手册翻译及学习(四)非本地操作的数据处理器和块之间的耦合

    非本地操作的数据处理器和块之间的耦合 Palabos用户文档 的第十六章 (#)前言 原始文档 Dynamics classes and data processors are the nuts an ...

  8. 《Effective C++》》阅读笔记

    第一章    Accustoming Yourself to C++ 1           View C++ as a federation of language (1).             ...

  9. Palabos User Guide中文解读 | 第十六章 | 非局部操作的数据处理器和Block之间耦合

    作者的话:本人在学习palabos时,发现国内中文资料甚少,恰好网上可以直接搜到palabos user guide这种英文资料,加之时间充裕,便打算开始翻译,翻了一节后发现这可能算侵权,就比较伤脑筋 ...

最新文章

  1. 细节:js 创建对象的几种模式举例
  2. 7-7 字符串修改 (15 分)
  3. YARP(Yet Another Reverse Proxy)是使用 .NET 构建的高度可定制的反向代理
  4. 前端开发——移动端及响应式布局解决办法总结(适配)
  5. Asp.net中的时区
  6. 作者:​高晨旭(1990-),男,北京系统工程研究所研究实习员。
  7. 矩阵的运算及其运算规则
  8. 机器学习 Machine Learning中多元线性回归的学习笔记~
  9. 微软面试智力题 (附答案)
  10. 电力行业巡检对讲通信系统
  11. android 微博sdk 集成 文件不存在(8998) 您所访问的站点在新浪微博的认证失败,错误码 21322
  12. css中reset属性详解,css中如何使用counter-reset属性
  13. 谷歌浏览器webGL实验
  14. 服务器被劫持,恶意注入js
  15. P quant与Q quant
  16. 用python和flask框架写MongoDB数据库网页(通院大三软件设计)运营商信息管理系统(1)环境配置
  17. 马克扎克伯格节俭_马克·扎克伯格的垃圾实在很难抢
  18. excel学习笔记:数据格式,单元格格式(快捷键,自动调整行列宽,条件格式,自定义格式,日期星期时间的设置,特殊数字,多种填充方式,从下拉列表选择,数据验证。)
  19. PCI、PCI-X、PCI-E、PCI-E Card、Mini PCI-E、M.2、Add-in Card 它们有啥区别?这些概念你搞清楚了吗?
  20. 字节跳动P0级事故:实习生删除GB以下所有模型,差点没上头条......

热门文章

  1. kafka高可用集群原理
  2. 文件上传之传统方式上传代码回顾
  3. 访问数组元素进行获取
  4. SpringBoot高级消息-RabbitMQ运行机制
  5. 原型模式coding-克隆破坏单例
  6. ubuntu和python快速换源
  7. 201205阶段二FFmpeg编码
  8. 65.4. Other GUI - phpOraAdmin
  9. Saltstack-6:模块
  10. bzoj2442codevs4654 单调队列优化dp