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. 虚拟机服务没有启动的 CentOS 和 Ubuntu 无法上网

    测试用 vmware 安装 OSX,安装补丁时要停止 vmware 的服务.如下图: 结果忘记启动了,导致 centos\ubuntu 等所有虚拟机都无法上网...所有的 启动这四个服务后,一切恢复正 ...

  2. 谭浩强第四版第九章课后习题12>>>建立一个链表,每个节点包括:学号、姓名、性别、年龄。输入一个年龄,若链表 中的结点所包含的年龄等于此年龄,则删除此结点。

    #include<stdio.h> #include<stdlib.h> #define N sizeof(link) typedef struct lin { struct ...

  3. python2.7练习小例子(二十五)

        25):题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁.问第4个人岁数,他说比第3个人大2岁.问第三个人,又说比第2人大两岁.问第2个人,说比第一个人大两岁.最后问第一个人,他 ...

  4. js字符串操作函数

    js字符串函数 JS自带函数 concat 将两个或多个字符的文本组合起来,返回一个新的字符串. var a = "hello"; var b = ",world&quo ...

  5. Mysql双主操作

    MySQL双主(主主)架构方案   在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果 ...

  6. Coap协议学习笔记-第一篇

    1. 物联网应用上一般使用单片机(或者其他SOC),单片机的RAM内存一般只有20KB~~128KB左右,然而一个TCP协议栈可能就20KB,所以只能用UDP,因为UDP相对小很多,然后在UDP上加了 ...

  7. javac一次性编译多个包下的.java文件

    如题是我想要知道的,然后在网上搜了一下 下面是在某些帖子里看到别人说的只言片语 =========================================================== ...

  8. springmvc基础篇—处理图片静态资源文件

    当我们在web.xml中对DispatcherServlet的过滤设置为/ 的时候,表示对所有的路径进行拦截过滤,那么不可避免的就会产生一个问题,那就是像图片这种静态资源文件我明明引用路径有,但就是加 ...

  9. webpack loader之css、scss、less、stylus安装

    1.打包css,需要安装css-loader和style-loader yarn add --dev css-loader style-loader 或者 npm install --save-dev ...

  10. 虚拟现实-VR-UE4-创建C++版工程

    首先,创建C++版本的UE4 项目工程,我使用的是4.12.3版本,据了解,新版本后面的编译都是vs2015 所以,想要创建C++版本的工程,就需要安装vs2015 至于vs2015的安装,自己百度吧 ...