ITeye博客

2013 年文章归档

共 5 篇文章

c++ vector list map在遍历中删除元素

c++ STL 中的vector, list, map这些数据结构,经常需要在遍历时删除其中的元素,但是又不能直接删除,会出错。 在你调用erase方法删除元素时,erase方法会返回下一个元素的迭代器,利用这一点,可以写这样的代码: for(vector<int>::iterator it=d.begin();it!=d.end(); ) { if(*it==3) { it=d.erase(it); } else { it++; } } 这一点对于vector, list和map都适用。 对于map还可以写成这样 for(vector<int>::iterator it=d.begin()…

2013-05-07 14:47 · c

装饰器模式Decorator(c++)

相应的代码: #include <iostream> #include <vector> #include <string> using namespace std; // base class class Beverage { public: virtual string getDescription() = 0; virtual float cost() = 0; }; // base class of condiment, also a base decorator class class CondimentDecorator : public Beverage { protected…

2013-04-27 14:59 · 设计模式

c++ 构造函数和析构函数调用顺序

#include <iostream> #include <vector> using namespace std; class Organisms { public: Organisms() { cout<<"Organisms constructor"<<endl; } ~Organisms() { cout<<"Organisms distructor"<<endl; } }; class Animal: public Organisms { public: Animal() { cout<<"Animal constructor"<<endl; } ~Animal() { cout<…

2013-04-25 16:54 · c

epoll高效的原因

epoll高效的原因: 使用内核中断实现,中断后调用处理函数处理,是异步的。 http://blog.chinaunix.net/uid-17299695-id-3059110.html

2013-03-18 11:10 · c · linux系统

交换两个数字的值(不用临时变量)

c语言, 有两个int型的数字,怎么样不用临时变量交换两个数字的值? 用如下方法即可: int a = 10; int b = 20; a = a^b; b = a^b; a = a^b; printf("%d %d\n", a, b); result: 20 10 这是为什么呢? 首先得理解“异或操作”, 异或是一种位操作,相同的位异或结果是0, 不同的位异或结果为1. 在第一个 a = a^b后,a的值其实保存了a, b两个变量之间的比较结果,也可以理解为a, b两变量的差异。 这时根据b和保存了差异的变量,可以推导出最初的a变量的值, 就是 b = a^b. 然后再类似的可以推导出b的…

2013-03-10 19:52 · 面试相关