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 进阶的更多相关文章

  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. HashMap JDK1.8实现原理

    HashMap概述 HashMap存储的是key-value的键值对,允许key为null,也允许value为null.HashMap内部为数组+链表的结构,会根据key的hashCode值来确定数组 ...

  2. Scala语法(三)

    模式匹配 1)match val a = 1 val b=a match { *// a match { }返回值赋予变量 b case 1 => "red" case 2 ...

  3. hive 学习系列五(hive 和elasticsearch 的交互,很详细哦,我又来吹liubi了)

    hive 操作elasticsearch 一,从hive 表格向elasticsearch 导入数据 1,首先,创建elasticsearch 索引,索引如下 curl -XPUT '10.81.17 ...

  4. sorted() ,filter() ,map()的用法

    sorted() 排序函数. 语法: sorted(Iterable, key=None, reverse=False) Iterable: 可迭代对象 key: 排序规则(排序函数), 在sorte ...

  5. (数据科学学习手札24)逻辑回归分类器原理详解&Python与R实现

    一.简介 逻辑回归(Logistic Regression),与它的名字恰恰相反,它是一个分类器而非回归方法,在一些文献里它也被称为logit回归.最大熵分类器(MaxEnt).对数线性分类器等:我们 ...

  6. webDriver + Firefox 浏览器 完美兼容

    搞java最烦的就是不同版本的适配问题.现分享下实测成功的案例. Firefox:4.0.1 selenium:selenium-server-standalone-2.43.1.jar 下面这个链接 ...

  7. stm32--FatFs调试过程(SPIFlash)

    移植方法参见我的另一篇博客:<stm32--FatFs移植(SPIFlash)>. 本文仅记录在初次移植完成后,遇到的问题,和解决的过程. 调试记录: 问题1:f_open返回3,即磁盘没 ...

  8. 「日常训练」「小专题·图论」 Cow Contest (1-3)

    题意 分析 问题是要看出来这是个floyd闭包问题.我没看出来- - 分析之后补充. 代码 // Origin: // Theme: Graph Theory (Basic) // Date: 080 ...

  9. windows本地连接腾讯云的mysql服务器

    由于最近数据库需要用上Navicat作为数据库,但是我的mysql装在腾讯云的Ubuntu上,因此需要做些配置开放端口,和监听端口,因此略显麻烦,这里记录一下连接的具体步骤,方便以后又得装(flag) ...

  10. LeetCode 410——分割数组的最大值

    1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...