vector 进阶
http://classfoo.com/ccby/article/jnevK
- #include <iostream>
- #include <vector>
- #include <algorithm> // sort, max_element, random_shuffle, remove_if, lower_bound
- #include <functional> // greater, bind2nd
- // 用在此处是为了方便简洁, 在实际编程中慎用
- using namespace std;
- int main()
- {
- int arr[] = { , , , };
- // 用上述数组初始化向量
- vector<int> foo(arr, arr + );
- // 插入更多的元素
- foo.push_back();
- foo.push_back();
- foo.push_back();
- foo.push_back();
- // 此时的向量内容为 {1, 2, 3, 4, 5, 6, 7, 8}
- // 随机移动元素
- random_shuffle(foo.begin(), foo.end());
- // 定位最大的元素, O(n)
- vector<int>::const_iterator largest =
- max_element(foo.begin(), foo.end());
- cout << "当前最大元素是: " << *largest << "\n";
- cout << "它的索引位置是: " << largest - foo.begin() << "\n";
- // 排序元素
- sort(foo.begin(), foo.end());
- // 用二分查找法找出向量中值为5的元素
- vector<int>::const_iterator five =
- lower_bound(foo.begin(), foo.end(), );
- cout << "值为5的元素的索引位置是: " << five - foo.begin() << "\n";
- // 删除所有值大于4的元素
- foo.erase(remove_if(foo.begin(), foo.end(),
- bind2nd(greater<int>(), )), foo.end());
- // 打印所有剩余元素的值
- for (vector<int>::const_iterator it = foo.begin(); it != foo.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << "\n";
- return ;
- }
对象
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <functional>
- #include <sstream>
- namespace ClassFoo{
- using namespace std;
- class Person {
- private:
- std::string name;
- static int n;
- public:
- Person() {
- std::stringstream ss;
- std::string snum;
- ss << n++;
- ss >> snum;
- name = "foo" + snum;
- }
- void print() const {
- std::cout << name << std::endl;
- }
- void printWithPrefix(std::string prefix) const {
- std::cout << prefix << name << std::endl;
- }
- };
- int Person::n = ;
- void foo(const std::vector<Person>& coll)
- {
- // 对每个元素对象调用成员函数 print()
- for_each(coll.begin(), coll.end(), mem_fun_ref(&Person::print));
- // 对每个元素对象调用成员函数 printWithPrefix()
- // - "person: " 作为参数传递给成员函数
- for_each(coll.begin(), coll.end(),
- bind2nd(mem_fun_ref(&Person::printWithPrefix), "person: "));
- }
- void ptrfoo(const std::vector<Person*>& coll)
- {
- // 对每个指针指向的元素对象调用成员函数 print()
- for_each(coll.begin(), coll.end(),
- mem_fun(&Person::print));
- // 对每个指针指向的元素对象调用成员函数 printWithPrefix()
- // - "person: " 作为参数传递给成员函数
- for_each(coll.begin(), coll.end(),
- bind2nd(mem_fun(&Person::printWithPrefix), "person: "));
- }
- }
- int main()
- {
- std::cout << "当向量的元素是对象时:" << std::endl;
- std::vector<ClassFoo::Person> coll();
- ClassFoo::foo(coll);
- std::cout << "当向量的元素是指向对象的指针时:" << std::endl;
- std::vector<ClassFoo::Person*> coll2;
- coll2.push_back(new ClassFoo::Person);
- coll2.push_back(new ClassFoo::Person);
- coll2.push_back(new ClassFoo::Person);
- coll2.push_back(new ClassFoo::Person);
- coll2.push_back(new ClassFoo::Person);
- ClassFoo::ptrfoo(coll2);
- }
vector 进阶的更多相关文章
- Android Vector曲折的兼容之路
Android Vector曲折的兼容之路 两年前写书的时候,就在研究Android L提出的Vector,可研究下来发现,完全不具备兼容性,相信这也是它没有被广泛使用的一个原因,经过Google的不 ...
- Android Drawable Mipmap Vector使用及Vector兼容
原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/artic ...
- 机器学习基础 --- numpy的基本使用
一.numpy的简介 numpy是Python的一种开源的数值计算扩展库.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该 ...
- Java进阶(四十六)简述ArrayList、Vector与LinkedList的异同点
简述ArrayList.Vector与LinkedList的异同点 Collection类的继承图如下: 从图中可以看出,LinkedList与ArrayList.ArrayDeque这三者都 ...
- STL进阶--vector vs deque
vector class Dog; // 例 1: vector<Dog> vec(6); // vec.capacity() == 6, vec.size() == 6, // 默认构造 ...
- C++进阶 STL(1) 第一天 [容器,算法,迭代器] string容器 vector容器 deque容器
课程大纲 02实现基本原理 容器,算法,迭代器 教室:容器 人:元素 教室对于楼:容器 序列式容器: 容器元素在容器中的位置是由进入容器的时间和地点来决定 序列式容器 关联式容器: 教室中 按年龄排座 ...
- 【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 ...
- 学习RaphaelJS矢量图形包--Learning Raphael JS Vector Graphics中文翻译(一)
(原文地址:http://www.cnblogs.com/idealer3d/p/LearningRaphaelJSVectorGraphics.html) 前面3篇博文里面,我们讲解了一本叫做< ...
- Matlab 进阶学习记录
最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal = proposal_config('image_means', ...
随机推荐
- vi/vim连续注释
知识点: 1-可视块模式方法 2-替换方法 3-自定义快捷键方式 今天刚好重新在linux上手工搭建完Lamp环境,用来下vi操作,一段时间不用就有些生疏了,正好经常要注释,回顾下自己会的方法,小结一 ...
- Hadoop(4)-Hadoop集群环境搭建
准备工作 开启全部三台虚拟机,确保hadoop100的机器已经配置完成 分发脚本 操作hadoop100 新建一个xsync的脚本文件,将下面的脚本复制进去 vim xsync #这个脚本使用的是rs ...
- Linux 内核之api_man 手册安装
开发环境:Ubuntu18.04,虚拟机virtual box 1.安装XML格式转换 sudo apt install xmlto 2.在内核目录执行 make mandocs 大概持续了半小时 ...
- 001---Linux系统的启动过程
Linux系统的启动过程 按下电源 开机自检(BIOS):检查cpu.内存.硬盘是否有问题,找到启动盘. MBR引导(master boot record):主引导记录,读取存储设备的512bytes ...
- 003---设计首页index页面
在项目的urls.py文件添加一条url from django.contrib import admin from django.urls import path, re_path from app ...
- Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String
题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- struts2学习笔记一
一.框架概述 1.框架的意义与作用: 所谓框架,就是把一些繁琐的重复性代码封装起来,使程序员在编码中把更多的经历放到业务需求的分析和理解上面. 特点:封装了很多细节,程序员在使用的时候会非常简单. 2 ...
- Verilog 初级入门概念
首先我们要理解两种变量类型 Net Type(连线型)和 Register Type (寄存器型): Net Type(连线型),从名字上理解就是“导线”呗,导线的这头和导线的另一头始终是直接连通的, ...
- Java集合类面试题
java.util包中包含了一系列重要的集合类,而对于集合类,主要需要掌握的就是它的内部结构,以及遍历集合的迭代模式. 1.Java集合框架是什么?说出一些集合框架的优点? 每种编程语言中都有集合,最 ...
- .NET基础知识之八——深拷贝,浅拷贝
目录 1.概念 2.使用赋值符号"=" 3.浅复制 4.深复制 5.问题一:如果类里面嵌套有多个类,然后嵌套类里面又嵌套类,那么像上面实现深拷贝的方法还能用吗? 6.问题二:实现深 ...