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', ...
随机推荐
- Static关键字,遇到的问题_1
一.问题 父类代码: ...
- python递归函数(计算阶乘)
def f1(x,x1=1): if x == 1: return x1 #x1这个值为我们所需要的值,所以返回 x1 *= x r = f1(x-1,x1) #r接收返回值,并在下面接着返回 ret ...
- python2.7入门---元组
这次我们来学习下python中的元组.首先,基础认知点是,Python的元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号.元组创建很简单,只需要在括号中添加元素, ...
- DDL失败案例
问题描述 今天对线上某个业务的大表120G进行重建表操作时遇到报错,该表有个比较显著的特征是*写入量比较大,每天写入加更新的频率在数千万级别.大致的环境 1 版本:Percona 5.6.24 2 操 ...
- anaconda虚拟环境操作
1.首先在所在系统中安装Anaconda.可以打开命令行输入conda -V检验是否安装以及当前conda的版本. 2.conda常用的命令. 1)conda list 查看安装了哪些包. 2)con ...
- 使用fiddler和jmeter进行简单的接口测试。
初学接口测试,以下内容是记录首次使用fiddler和jmeter进行接口测试的步骤,可能步骤有点繁琐,如果有不对的地方,欢迎大家指正. 准备活动: 1.打开fiddler,打开fiddler以后会自动 ...
- 剑指offer-二进制中1的个数11
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. class Solution: def NumberOf1(self, n): # write code here coun ...
- 基于语音转录的ted演讲推荐
论文地址:https://arxiv.org/abs/1809.05350v1 二. 实现 我们从Kaggle[6]中获取了TED演讲数据集,其中包括2400个TED演讲的数据,包括标题.演讲者.标 ...
- matlab mex 小o -o 出错
https://github.com/kyamagu/mexopencv/issues/117 就是说2014a以后的版本mex -o 选项变成了 -output 蛋疼,这有什么好改的.找了好久才 ...
- ubuntu 14.04 安装opencv 3.0 测试
安装opencv 前面步骤参考 http://my.oschina.net/u/1757926/blog/293976 主要修改测试代码 1. 先从sourceforge上下载OpenCV的源码 2. ...