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', ...
随机推荐
- PHP(YII2实现) 微信网页授权
参考地址 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842 实现步骤分析: 获取code->access ...
- 使用MapReduce读取HBase数据存储到MySQL
Mapper读取HBase数据 package MapReduce; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hba ...
- spring配置jackson不返回null值
#json不返回null spring.jackson.default-property-inclusion=non_null
- python2.7入门---网络编程(socket)
Python 提供了两个级别访问的网络服务: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法. 高级别 ...
- MVC中路由的修改和浏览器的地址参数
在 ASP.NET MVC 应用程序中,它是更常见的做法在作为路由数据 (像我们一样与身份证上面) 比将它们作为查询字符串传递的参数中传递. ) { return HttpUtility.HtmlEn ...
- OpenCV代码提取: threshold函数的实现
threshold algorithm: The simplest image segmentation method. All thresholding algorithms take a sour ...
- 1511: [POI2006]OKR-Periods of Words
1511: [POI2006]OKR-Periods of Words https://www.lydsy.com/JudgeOnline/problem.php?id=1511 题意: 对于一个串的 ...
- MyEclipse - 问题集 - Java compiler level does not match the version of the installed Java project facet
右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面. 在页面中的“Java”下拉列表中,选择相应 ...
- windows系统下npm升级的正确姿势以及原理
本文来自网易云社区 作者:陈观喜 网上关于npm升级很多方法多种多样,但是在windows系统下不是每种方法都会正确升级.其中在windows系统下主要的升级方法有以下三种: 首先最暴力的方法删掉no ...
- 系统学习Docker 践行DevOps理念
Docker代表的容器技术是近两年的大热技术,和人工智能.区块链等热点不同,容器技术的门槛并不高,每一个开发.测试.运维人员都能在日常工作中掌握和使用,是当今IT从业人员的必备技能之一.本课程会带大家 ...