- 浏览: 498816 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
jkxydp:
算法运行的结果根本就不对。
BM算法. -
soarwindzhang:
感谢博主的分享,我今天看了您的UFSET非递归的路径压缩时感觉 ...
并查集 -
zhangning290:
楼主好像只考虑了坏字符规则,。没有考虑好后缀
BM算法. -
lsm0622:
文字描述有错误 误导新学者
求有向图的强连通分量(scc):Tarjan算法 -
knightchen:
博主,你太强了!这篇文章对我学习C++多线程很有帮助!谢谢
并发学习之一_windows下ZThread在CodeBlocks上的安装与配置
1,deque特点:
(1)将元素放在多个连续的存储块.
(2)允许在序列两端快速地插入元素,并且在扩展存储区的时候不需要拷贝和析构原来的对象.
(3)deque支持随机访问[],但是速度没有vector快.
2,deque和vector的区别:微不足道.
deque有接口push_front()和pop_front(),而vector没有.
3,序列容器间的转换
4,
5,at比索引操作符[]代价更大.
(1)将元素放在多个连续的存储块.
(2)允许在序列两端快速地插入元素,并且在扩展存储区的时候不需要拷贝和析构原来的对象.
(3)deque支持随机访问[],但是速度没有vector快.
2,deque和vector的区别:微不足道.
deque有接口push_front()和pop_front(),而vector没有.
#include <cstddef> #include <ctime> #include <deque> #include <fstream> #include <iostream> #include <iterator> #include <sstream> #include <string> #include <vector> using namespace std; int main() { ifstream in("happy.in"); vector<string> vstrings; deque<string> dstrings; string line; // Time reading into vector: clock_t ticks = clock(); while(getline(in, line)) vstrings.push_back(line); ticks = clock() - ticks; cout << "Read into vector: " << ticks << endl; // Repeat for deque: ifstream in2("happy.in"); ticks = clock(); while(getline(in2, line)) dstrings.push_back(line); ticks = clock() - ticks; cout << "Read into deque: " << ticks << endl; //deque插入的速度更快 // Now compare indexing: 加入行号 ticks = clock(); for(size_t i = 0; i < vstrings.size(); i++) { ostringstream ss; ss << i; vstrings[i] = ss.str() + ": " + vstrings[i]; } ticks = clock() - ticks; cout << "Indexing vector: " << ticks << endl; ticks = clock(); for(size_t j = 0; j < dstrings.size(); j++) { ostringstream ss; ss << j; dstrings[j] = ss.str() + ": " + dstrings[j]; } ticks = clock() - ticks; cout << "Indexing deque: " << ticks << endl; //vector索引的速度更快 // Compare iteration ofstream tmp1("tmp1.tmp"), tmp2("tmp2.tmp"); ticks = clock(); copy(vstrings.begin(), vstrings.end(), ostream_iterator<string>(tmp1, "\n")); ticks = clock() - ticks; cout << "Iterating vector: " << ticks << endl; ticks = clock(); copy(dstrings.begin(), dstrings.end(), ostream_iterator<string>(tmp2, "\n")); ticks = clock() - ticks; cout << "Iterating deque: " << ticks << endl; //vector使用迭代器更快 }
3,序列容器间的转换
#include <algorithm> #include <cstdlib> #include <deque> #include <iostream> #include <iterator> #include <vector> using namespace std; //读入deque,然后转化为vector class Noisy { static long create, assign, copycons, destroy; long id; public: Noisy() : id(create++) //create:记录构造函数执行的次数 { cout << "d[" << id << "]" << endl; } Noisy(const Noisy& rv) : id(rv.id) //copycons:记录拷贝函数执行的次数. { cout << "c[" << id << "]" << endl; ++copycons; } Noisy& operator=(const Noisy& rv) //assign:记录赋值操作执行的次数. { cout << "(" << id << ")=[" << rv.id << "]" << endl; id = rv.id; ++assign; return *this; } //容器对象,必须提供比较操作符 friend bool operator<(const Noisy& lv, const Noisy& rv) { return lv.id < rv.id; } friend bool operator==(const Noisy& lv,const Noisy& rv) { return lv.id == rv.id; } ~Noisy() //destroy:记录析构函数调用的次数 { cout << "~[" << id << "]" << endl; ++destroy; } friend ostream& operator<<(ostream& os, const Noisy& n) { return os << n.id; } friend class NoisyReport; }; struct NoisyGen { Noisy operator()() { return Noisy(); } }; //单件类,报告类的统计信息. class NoisyReport { static NoisyReport nr; NoisyReport() {} // Private constructor NoisyReport & operator=(NoisyReport &); // Disallowed NoisyReport(const NoisyReport&); // Disallowed public: ~NoisyReport() { cout << "\n-------------------\n" << "Noisy creations: " << Noisy::create << "\nCopy-Constructions: " << Noisy::copycons << "\nAssignments: " << Noisy::assign << "\nDestructions: " << Noisy::destroy << endl; } }; long Noisy::create = 0, Noisy::assign = 0,Noisy::copycons = 0, Noisy::destroy = 0; NoisyReport NoisyReport::nr; int main() { int size = 25; deque<Noisy> d; generate_n(back_inserter(d), size, NoisyGen()); cout << "\n Converting to a vector(1)" << endl; vector<Noisy> v1(d.begin(), d.end());//v1不会导致多次内存分配. cout << "\n Converting to a vector(2)" << endl; vector<Noisy> v2; v2.reserve(d.size());//先预先分配好大小,实际上和v1完全一致. v2.assign(d.begin(), d.end()); cout << "\n Cleanup" << endl; }
4,
#include <cstdlib> #include <deque> #include <iostream> using namespace std; //读入deque,然后转化为vector class Noisy { static long create, assign, copycons, destroy; long id; public: Noisy() : id(create++) //create:记录构造函数执行的次数 { cout << "d[" << id << "]" << endl; } Noisy(const Noisy& rv) : id(rv.id) //copycons:记录拷贝函数执行的次数. { cout << "c[" << id << "]" << endl; ++copycons; } Noisy& operator=(const Noisy& rv) //assign:记录赋值操作执行的次数. { cout << "(" << id << ")=[" << rv.id << "]" << endl; id = rv.id; ++assign; return *this; } //容器对象,必须提供比较操作符 friend bool operator<(const Noisy& lv, const Noisy& rv) { return lv.id < rv.id; } friend bool operator==(const Noisy& lv,const Noisy& rv) { return lv.id == rv.id; } ~Noisy() //destroy:记录析构函数调用的次数 { cout << "~[" << id << "]" << endl; ++destroy; } friend ostream& operator<<(ostream& os, const Noisy& n) { return os << n.id; } friend class NoisyReport; }; struct NoisyGen { Noisy operator()() { return Noisy(); } }; //单件类,报告类的统计信息. class NoisyReport { static NoisyReport nr; NoisyReport() {} // Private constructor NoisyReport & operator=(NoisyReport &); // Disallowed NoisyReport(const NoisyReport&); // Disallowed public: ~NoisyReport() { cout << "\n-------------------\n" << "Noisy creations: " << Noisy::create << "\nCopy-Constructions: " << Noisy::copycons << "\nAssignments: " << Noisy::assign << "\nDestructions: " << Noisy::destroy << endl; } }; long Noisy::create = 0, Noisy::assign = 0,Noisy::copycons = 0, Noisy::destroy = 0; NoisyReport NoisyReport::nr; int main() { int size = 100; deque<Noisy> dn; Noisy n; for(int i = 0; i < size; i++) dn.push_back(n); cout << "\n cleaning up "<< endl; return 0; }
5,at比索引操作符[]代价更大.
#include <ctime> #include <deque> #include <iostream> #include <vector> using namespace std; int main() { long count = 1000; int sz = 1000; vector<int> vi(sz); clock_t ticks = clock(); for(int i1 = 0; i1 < count; i1++) for(int j = 0; j < sz; j++) vi[j]; cout << "vector[] " << clock() - ticks << endl; ticks = clock(); for(int i2 = 0; i2 < count; i2++) for(int j = 0; j < sz; j++) vi.at(j); cout << "vector::at() " << clock()-ticks <<endl; deque<int> di(sz); ticks = clock(); for(int i3 = 0; i3 < count; i3++) for(int j = 0; j < sz; j++) di[j]; cout << "deque[] " << clock() - ticks << endl; ticks = clock(); for(int i4 = 0; i4 < count; i4++) for(int j = 0; j < sz; j++) di.at(j); cout << "deque::at() " << clock()-ticks <<endl; try { di.at(vi.size() + 1); } catch(...) { //捕捉所有异常 cerr << "Exception thrown" << endl; } return 0; }
发表评论
-
关于priority_queue
2010-06-18 15:11 35591,关于STL中的priority_queue:确定用top( ... -
深入理解模板4
2010-03-15 12:28 8631,函数模板的半有序: 生成模板函数的时候,编译器将从这些模板 ... -
通用容器3_vector
2010-03-12 16:52 7521,vector特点: (1)将内容放在一段连续的存储区域,索 ... -
深入理解模板3
2010-03-12 09:57 7121,类模板和函数模板的 ... -
通用容器2
2010-03-11 11:15 6121,所有基本序列容器(vect ... -
深入理解模板2
2010-03-11 10:41 7001,typename关键字: (1)若一个模板代码内部的某个类 ... -
通用容器1
2010-03-10 12:19 7691,一个容器描述了一个持有其他对象的对象. 2,c++处理容器 ... -
深入理解模板1
2010-03-10 11:38 8491,模版参数可以有三种类型:(1)类型;(2)编译时常量;(3 ... -
设计模式之观察者模式
2010-03-04 11:54 8311,解决的问题: 当某些其他对象改变状态时,如果一组对象需要进 ... -
STL算法合集
2010-03-04 10:14 23371,简要描述迭代器的各种形式: (1)InputIterato ... -
通用算法之可调整的函数对象
2010-03-02 16:51 8461,实例代码: #include <algorith ... -
通用算法笔记4
2010-03-02 10:15 5871,generate_n(序列起点,个数,函数发生器) 2,t ... -
设计模式之构建器模式
2010-03-01 11:08 6951,目标:将对象的创建和它的表示法分开. 2,实例代码: ... -
设计模式之虚构造函数模式
2010-02-27 19:39 10591,虚构造函数模式:"现在还不知道需要什么类型的对象 ... -
设计模式之工厂模式
2010-02-22 23:26 761特征:封装对象的创建. 1,给一个对象添加新类型时,类的创建必 ... -
设计模式之职责链模式
2010-02-20 22:31 10501,职责链的本质:尝试多个解决方法,直到找到一个起作用的方法. ... -
设计模式之策略模式
2010-02-20 21:52 6941,策略(strategy)模式特征:运行时选择算法,可以用多 ... -
设计模式之模板方法模式
2010-02-20 21:41 7361,模板方法模式的重要特征:它的定义在基类中,并且不能改动. ... -
设计模式之代理模式和状态模式
2010-02-20 19:37 11091,代理(Proxy)模式基本思 ... -
设计模式之命令模式
2010-02-19 19:36 6841,命令(command)模式最大的特点:允许向一个函数或者对 ...
相关推荐
11_copy构造函数调用时机4_函数返回值是匿名对象的去和留的剖析_传智扫地僧 12_构造和析构的重点整理 13_构造和析构总结 14_中午课程复习 15_构造函数的调用规则研究 16_浅拷贝问题抛出 17_浅拷贝问题分析_传智扫地...
collections 作为 Python 的内建集合模块,实现了许多十分高效的特殊容器数据类型,即除了 Python 通用内置容器: dict、list、set 和 tuple 等的替代方案。在 IDLE 输入 help(collections) 可查看帮助文档,其中...
cstl是使用C语言编写的一个通用的数据结构和常用的算法库,它模仿SGI STL的接口和实现,支持vector, list, deque等等常用的数据结构,同时还支持排序,查找,划分等常用的算法,此外cstl也包含迭代器的类型,它作为...
4 通用工具 9 4.1 简介 9 4.1.1 类别 9 4.1.2 头文件 9 4.2 Pairs 9 4.2.1 简介 9 4.2.2 示例 9 4.3 auto_ptr 10 4.3.1 作用 10 4.3.2 引入原因 10 4.3.3 声明 10 4.3.4 auto_ptr拥有权的转移 10 4.3.5 示例 11 ...
2-4-4 C廿异常处理机制.............................................. 97 2-4-5 类的异常处理.................................................. 99 2-5 小结· · · · · · ·................................
队列类的实现采用了 C++ 标准模板库 (STL) 中的 deque 容器作为底层存储结构。deque 是一种双端队列,可以在两端高效地进行插入和删除操作,因此非常适合实现队列的入队和出队操作。队列类提供了以下主要方法: - `...
元组违约柜台双端队列collections模块实现了专用的容器数据类型,为Python的通用内置容器 , list , set和tuple提供了替代方法。 namedtuple() 用于使用命名字段创建元组子类的工厂函数deque 类似于列表的容器,两...
deque> //STL 双端队列容器 #include <exception> //异常处理类 #include <fstream> #include <functional> //STL 定义运算函数(代替运算符) #include <limits> #include <list&...
能用作标准库容器的元素类型,即 vector/list/deque 的 value_type。(用作 std::map 的 key_type 是更进一步的要求,本文从略)。 换言之,你的 String 能让以下代码编译运行通过,并且没有内存方面的错误。 代码...
libcstl是使用C语言编写的一个通用的数据结构和常用的算法库,它模仿SGI STL的接口和实现,支持vector,list,deque等等常用的数据结构,同时还支持排序,查找,划分等常用的算法,此外libcstl也包含迭 代器的类型,它...
#include <deque> //STL 双端队列容器 #include <exception> //异常处理类 #include #include <functional> //STL 定义运算函数(代替运算符) #include #include <list> //STL 线性列表容器 #include <map> ...
STL有7种主要容器:vector,list,deque,map,multimap,set,multiset. 17.你如何理解MVC。简单举例来说明其应用。 MVC模式是observer 模式的一个特例,典型的有MFC里面的文档视图架构。 18,多重继承如何消除向上继承...