http://classfoo.com/ccby/article/jnevK

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm> // sort, max_element, random_shuffle, remove_if, lower_bound
  4. #include <functional> // greater, bind2nd
  5.  
  6. // 用在此处是为了方便简洁, 在实际编程中慎用
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int arr[] = { , , , };
  12. // 用上述数组初始化向量
  13. vector<int> foo(arr, arr + );
  14.  
  15. // 插入更多的元素
  16. foo.push_back();
  17. foo.push_back();
  18. foo.push_back();
  19. foo.push_back();
  20.  
  21. // 此时的向量内容为 {1, 2, 3, 4, 5, 6, 7, 8}
  22.  
  23. // 随机移动元素
  24. random_shuffle(foo.begin(), foo.end());
  25.  
  26. // 定位最大的元素, O(n)
  27. vector<int>::const_iterator largest =
  28. max_element(foo.begin(), foo.end());
  29.  
  30. cout << "当前最大元素是: " << *largest << "\n";
  31. cout << "它的索引位置是: " << largest - foo.begin() << "\n";
  32.  
  33. // 排序元素
  34. sort(foo.begin(), foo.end());
  35.  
  36. // 用二分查找法找出向量中值为5的元素
  37. vector<int>::const_iterator five =
  38. lower_bound(foo.begin(), foo.end(), );
  39.  
  40. cout << "值为5的元素的索引位置是: " << five - foo.begin() << "\n";
  41.  
  42. // 删除所有值大于4的元素
  43. foo.erase(remove_if(foo.begin(), foo.end(),
  44. bind2nd(greater<int>(), )), foo.end());
  45.  
  46. // 打印所有剩余元素的值
  47. for (vector<int>::const_iterator it = foo.begin(); it != foo.end(); ++it)
  48. {
  49. cout << *it << " ";
  50. }
  51.  
  52. cout << "\n";
  53. return ;
  54. }

对象

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <functional>
  6. #include <sstream>
  7.  
  8. namespace ClassFoo{
  9. using namespace std;
  10. class Person {
  11. private:
  12. std::string name;
  13. static int n;
  14. public:
  15. Person() {
  16. std::stringstream ss;
  17. std::string snum;
  18. ss << n++;
  19. ss >> snum;
  20. name = "foo" + snum;
  21. }
  22. void print() const {
  23. std::cout << name << std::endl;
  24. }
  25. void printWithPrefix(std::string prefix) const {
  26. std::cout << prefix << name << std::endl;
  27. }
  28. };
  29.  
  30. int Person::n = ;
  31.  
  32. void foo(const std::vector<Person>& coll)
  33. {
  34. // 对每个元素对象调用成员函数 print()
  35. for_each(coll.begin(), coll.end(), mem_fun_ref(&Person::print));
  36.  
  37. // 对每个元素对象调用成员函数 printWithPrefix()
  38. // - "person: " 作为参数传递给成员函数
  39. for_each(coll.begin(), coll.end(),
  40. bind2nd(mem_fun_ref(&Person::printWithPrefix), "person: "));
  41. }
  42.  
  43. void ptrfoo(const std::vector<Person*>& coll)
  44. {
  45. // 对每个指针指向的元素对象调用成员函数 print()
  46. for_each(coll.begin(), coll.end(),
  47. mem_fun(&Person::print));
  48.  
  49. // 对每个指针指向的元素对象调用成员函数 printWithPrefix()
  50. // - "person: " 作为参数传递给成员函数
  51. for_each(coll.begin(), coll.end(),
  52. bind2nd(mem_fun(&Person::printWithPrefix), "person: "));
  53. }
  54.  
  55. }
  56. int main()
  57. {
  58. std::cout << "当向量的元素是对象时:" << std::endl;
  59. std::vector<ClassFoo::Person> coll();
  60. ClassFoo::foo(coll);
  61.  
  62. std::cout << "当向量的元素是指向对象的指针时:" << std::endl;
  63. std::vector<ClassFoo::Person*> coll2;
  64. coll2.push_back(new ClassFoo::Person);
  65. coll2.push_back(new ClassFoo::Person);
  66. coll2.push_back(new ClassFoo::Person);
  67. coll2.push_back(new ClassFoo::Person);
  68. coll2.push_back(new ClassFoo::Person);
  69. ClassFoo::ptrfoo(coll2);
  70. }

vector 进阶的更多相关文章

  1. Android Vector曲折的兼容之路

    Android Vector曲折的兼容之路 两年前写书的时候,就在研究Android L提出的Vector,可研究下来发现,完全不具备兼容性,相信这也是它没有被广泛使用的一个原因,经过Google的不 ...

  2. Android Drawable Mipmap Vector使用及Vector兼容

    原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/artic ...

  3. 机器学习基础 --- numpy的基本使用

    一.numpy的简介 numpy是Python的一种开源的数值计算扩展库.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该 ...

  4. Java进阶(四十六)简述ArrayList、Vector与LinkedList的异同点

    简述ArrayList.Vector与LinkedList的异同点   Collection类的继承图如下:   从图中可以看出,LinkedList与ArrayList.ArrayDeque这三者都 ...

  5. STL进阶--vector vs deque

    vector class Dog; // 例 1: vector<Dog> vec(6); // vec.capacity() == 6, vec.size() == 6, // 默认构造 ...

  6. C++进阶 STL(1) 第一天 [容器,算法,迭代器] string容器 vector容器 deque容器

    课程大纲 02实现基本原理 容器,算法,迭代器 教室:容器 人:元素 教室对于楼:容器 序列式容器: 容器元素在容器中的位置是由进入容器的时间和地点来决定 序列式容器 关联式容器: 教室中 按年龄排座 ...

  7. 【c++进阶:c++ 顺序容器vector,string,deque,list,forward_list,array常用性质】

    常用5种顺序容器性质: https://blog.csdn.net/oil_you/article/details/82821833 关于deque https://www.cnblogs.com/L ...

  8. 学习RaphaelJS矢量图形包--Learning Raphael JS Vector Graphics中文翻译(一)

    (原文地址:http://www.cnblogs.com/idealer3d/p/LearningRaphaelJSVectorGraphics.html) 前面3篇博文里面,我们讲解了一本叫做< ...

  9. Matlab 进阶学习记录

    最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal  =  proposal_config('image_means', ...

随机推荐

  1. vi/vim连续注释

    知识点: 1-可视块模式方法 2-替换方法 3-自定义快捷键方式 今天刚好重新在linux上手工搭建完Lamp环境,用来下vi操作,一段时间不用就有些生疏了,正好经常要注释,回顾下自己会的方法,小结一 ...

  2. Hadoop(4)-Hadoop集群环境搭建

    准备工作 开启全部三台虚拟机,确保hadoop100的机器已经配置完成 分发脚本 操作hadoop100 新建一个xsync的脚本文件,将下面的脚本复制进去 vim xsync #这个脚本使用的是rs ...

  3. Linux 内核之api_man 手册安装

    开发环境:Ubuntu18.04,虚拟机virtual box 1.安装XML格式转换 sudo apt  install xmlto 2.在内核目录执行 make mandocs  大概持续了半小时 ...

  4. 001---Linux系统的启动过程

    Linux系统的启动过程 按下电源 开机自检(BIOS):检查cpu.内存.硬盘是否有问题,找到启动盘. MBR引导(master boot record):主引导记录,读取存储设备的512bytes ...

  5. 003---设计首页index页面

    在项目的urls.py文件添加一条url from django.contrib import admin from django.urls import path, re_path from app ...

  6. Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String

    题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...

  7. struts2学习笔记一

    一.框架概述 1.框架的意义与作用: 所谓框架,就是把一些繁琐的重复性代码封装起来,使程序员在编码中把更多的经历放到业务需求的分析和理解上面. 特点:封装了很多细节,程序员在使用的时候会非常简单. 2 ...

  8. Verilog 初级入门概念

    首先我们要理解两种变量类型 Net Type(连线型)和 Register Type (寄存器型): Net Type(连线型),从名字上理解就是“导线”呗,导线的这头和导线的另一头始终是直接连通的, ...

  9. Java集合类面试题

    java.util包中包含了一系列重要的集合类,而对于集合类,主要需要掌握的就是它的内部结构,以及遍历集合的迭代模式. 1.Java集合框架是什么?说出一些集合框架的优点? 每种编程语言中都有集合,最 ...

  10. .NET基础知识之八——深拷贝,浅拷贝

    目录 1.概念 2.使用赋值符号"=" 3.浅复制 4.深复制 5.问题一:如果类里面嵌套有多个类,然后嵌套类里面又嵌套类,那么像上面实现深拷贝的方法还能用吗? 6.问题二:实现深 ...