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', ...
随机推荐
- Hadoop(20)-MapReduce框架原理-OutputFormat
1.outputFormat接口实现类 2.自定义outputFormat 步骤: 1). 定义一个类继承FileOutputFormat 2). 定义一个类继承RecordWrite,重写write ...
- phpcms2008网站漏洞如何修复 远程代码写入缓存漏洞利用
SINE安全公司在对phpcms2008网站代码进行安全检测与审计的时候发现该phpcms存在远程代码写入缓存文件的一个SQL注入漏洞,该phpcms漏洞危害较大,可以导致网站被黑,以及服务器遭受黑客 ...
- "Mon Dec 31 00:00:00 CST 2012" java日期装换 "yyyy-MM-dd"
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import ja ...
- spring boot打包问题
java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories ...
- python中判断输入是否为数字(包括浮点数)
1.当num确定为数字后 num=123.4print(isinstance(num,float))#判断是否为浮点数 print(isinstance(num,int))#判断是否为整数 2.当nu ...
- TFTP & commons-net-3.3.jar
项目需求:上传文件到服务器,TFTP 了解TFTP http://wenku.baidu.com/link?url=MhRVgIySotFMkm5ar6B71zROPMoqC7cd5cSbKJo2kx ...
- 阿里的100TB Sort Benchmark排序比雅虎快了一倍还多,我的看法
如果我的判断正确,它们使用的软件和算法应该是HADOOP,MAP/REDUCE,或者类似的技术方案.如果这些条件一样,影响计算结果的还有三个因素: 1.CPU的数量和CPU的处理能力 CPU的 ...
- LeetCode - 35. Search Insert Position(48ms)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- HDU 5794 A Simple Chess Lucas定理+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 题意概述: 给出一个N*M的网格.网格上有一些点是障碍,不能经过.行走的方式是向右下角跳马步.求 ...
- ExtJS6.0扩展日期选择控件为也可以选择时间
PS:ExtJS自带的日期选择控件只能够选择日期,但是现在的需求需要精确到秒,所以在网上搜索了一些例子(大部分是4.0的)作为参考,然后改出了6.0可用的一个日期时间选择控件. 1.找到extjs6. ...