摘要: vector<int> myVec;myVec.reserve( 100 ); // 新元素还没有构造, // 此时不能用[]访问元素for (int i = 0; i < 100; i++ ){ myVec.push_back( i ); //新元素这时才构造}myVec.resize( 102 ); // 用元素的默认构造函数构造了两个新的元素myVec[100] = 1; //直接操作新元素myVec[101] = 2; #include <vector>#include <iostream>using namespace std;int mai 阅读全文
posted @ 2012-07-19 21:37 byfei 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 当执行大数据量的调用push_back()的时候,记住要调用vector::reserve()。研究了vector和deque在插入数据的情况。通过这些假设,我们可以看出deque分配的空间是预先分配好的,deque维持一个固定增长率,在vector实验中我们考虑到应该调用vecor::reserve().然后在下面这个例子验证了我们的假设,在使用vector的时候调用reserve()能够膀子我们预先分配空间,这将是vector一个默认选择的操作。当你分配很多内存单元的时候,记住使用deque回收内存要比vector消耗时间多。探讨了vector和deque在回收非邻接内存块上的不同,分别证 阅读全文
posted @ 2012-07-19 21:03 byfei 阅读(137) 评论(0) 推荐(0) 编辑
摘要: 了解二进制文件的读写方法 C++文件流: fstream // 文件流ifstream // 输入文件流 ofstream // 输出文件流 //创建一个文本文件并写入信息//同向屏幕上输出信息一样将信息输出至文件#include<iomanip.h>#include<fstream.h>void main() { ofstream f1("d:\\me.txt"); //打开文件用于写,若文件不存在就创建它 if(!f1)return; //打开文件失败则结束运行 f1<<setw(20)<<"姓名:"& 阅读全文
posted @ 2012-07-19 20:24 byfei 阅读(169) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;class Class{public:virtual void fun(){cout << "Class::fun" << endl;}};int main(){Class objClass;cout << "Size of Class = " << sizeof(objClass) << endl;return 0;}Size of Class = 4#include <iostream>us 阅读全文
posted @ 2012-07-19 20:19 byfei 阅读(450) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;template<typename T>class CPoint{public:T m_x;T m_y;};int main(){CPoint<int> objPoint;cout << "Size of object is = " << sizeof(objPoint) << endl;return 0;}Size of object Point is = 8#include <iostream>using na 阅读全文
posted @ 2012-07-19 19:58 byfei 阅读(124) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;class Base{public:Base(){cout << "In Base" << endl;cout << "This Pointer = " << (int*) this << endl;cout << endl;}virtual void f(){cout << "Base::f" << endl;}};class Drive: publ 阅读全文
posted @ 2012-07-19 19:05 byfei 阅读(107) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;class Base{public:virtual void fun(){cout << "Base::fun" << endl;}void show(){fun();}};class Drive: public Base{public:virtual void fun(){cout << "Drive::fun" << endl;}};int main(){Drive d;d.show();d.fun();retur 阅读全文
posted @ 2012-07-19 17:52 byfei 阅读(111) 评论(0) 推荐(0) 编辑
摘要: 1.这个程序清楚地示范了基类的函数是如何调用派生类的虚函数的。这一技术被用于不同的框架中,例如MFC和设计模式(比如Template Design Pattern)。现在你可以修改一下这个程序来看看它的行为,我将要在基类的构造函数中调用虚函数,而不是普通的成员... 阅读全文
posted @ 2012-07-19 17:52 byfei 阅读(41) 评论(0) 推荐(0) 编辑