⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cc

📁 C++作业
💻 CC
字号:
# include "IntVec.hh"# include "ObjPtrVec.hh"# include "Obj.hh"using namespace std;# include <iostream># include <vector>// These are service methods used during testingvoid showInt(vector<int> &v) {    vector<int>::iterator it;    cerr << "Elements:";    for (it=v.begin();it!=v.end();it++) {	cerr << " " << *it;    }    cerr << endl;}void loadInt(vector<int> &v) {    v.clear();    for (int i=1;i<=10;i++) {	v.push_back(i);    }}void showObj(vector<Obj*> &v) {    vector<Obj*>::iterator it;    cerr << "Elements:";    for (it=v.begin();it!=v.end();it++) {	cerr << " " << (*it)->getVal();    }    cerr << endl;}void loadObj(vector<Obj*> &v) {    v.clear();    for (int i=1;i<=10;i++) {	v.push_back(new Obj(i));    }}// This method tests the behavior of a vector<int>.// Your IntVec class should perform identical to a// vector<int> in as many ways as possible.  You should// create a IntVec::test() method that performs// testings similar to those shown here (except// applied to your IntVec class, not to vector<int>).void intTest() {        vector<int> V,V2;    vector<int>::iterator is, ie;    cerr << endl;    cerr << "**********************" << endl;    cerr << "* BEGIN TESTS (int) *" << endl;    cerr << "*********************" << endl;    cerr << endl << "*** int::Testing initialization ***  " << endl;    cerr << "Expect  : " << endl;    showInt(V);    cerr << endl << "*** int::Testing assign() ***" << endl;    V.assign(10,7);    cerr << "Expect  : 7 7 7 7 7 7 7 7 7 7" << endl;    showInt(V);        loadInt(V);    is = V.begin();    is++;    is++;    is++;    ie = is;    ie++;    ie++;    V.assign(is,ie);    cerr << "Expect  : 4 5" << endl;    showInt(V);    cerr << endl << "*** int::Testing void push_back() ***" << endl;    loadInt(V);    V.push_back(1);    V.push_back(2);    V.push_back(3);    cerr << "Expect  : 1 2 3 4 5 6 7 8 9 10 1 2 3" << endl;    showInt(V);    cerr << endl << "*** int::Testing void pop_back() ***" << endl;    loadInt(V);    V.pop_back();    cerr << "Expect  : 1 2 3 4 5 6 7 8 9" << endl;    showInt(V);    cerr << endl << "*** int::Testing begin() ***" << endl;    loadInt(V);    cerr << "Expect   : 1" << endl;    cerr << "V.begin(): " << *(V.begin()) << endl;        cerr << endl << "*** int::Testing end() ***" << endl;    loadInt(V);    ie = V.end();    ie--;    cerr << "Expect   : 10" << endl;    cerr << "V.end()-1: " << *ie << endl;    cerr << endl << "*** int::Testing insert() ***" << endl;    loadInt(V);    V.insert(V.begin(),99);    cerr << "Expect  : 99 1 2 3 4 5 6 7 8 9 10" << endl;    showInt(V);    loadInt(V);    is = V.begin();    is++;    is++;    cerr << "Expect  : 1 2 88 88 88 3 4 5 6 7 8 9 10" << endl;    V.insert(is,3,88);    showInt(V);    loadInt(V);    is = V.begin();    is++;    is++;    ie = is;    ie++;    ie++;    V.insert(V.end(),is,ie);    cerr << "Expect  : 1 2 3 4 5 6 7 8 9 10 3 4" << endl;    showInt(V);    cerr << endl << "*** int::Testing erase() ***" << endl;    loadInt(V);    is = V.begin();    is++;    is++;    V.erase(is);    cerr << "Expect  : 1 2 4 5 6 7 8 9 10" << endl;    showInt(V);    loadInt(V);    is = V.begin();    is++;    is++;    ie = is;    ie++;    ie++;    V.erase(is,ie);    cerr << "Expect  : 1 2 5 6 7 8 9 10" << endl;    showInt(V);    cerr << endl << "*** int::Testing clear() ***" << endl;    loadInt(V);    V.clear();    cerr << "Expect  : " << endl;    showInt(V);        cerr << endl << "*** int::Testing empty() ***" << endl;    cerr << "-- V is empty --" << endl;    cerr << "Expect : true" << endl;    cerr << "empty(): ";    if(V.empty()) {	cerr << "true" << endl;    } else {	cerr << "false" << endl;    }    loadInt(V);    cerr << "-- V is NOT empty --" << endl;      cerr << "Expect : false" << endl;    cerr << "empty(): ";    if(V.empty()) {	cerr << "true" << endl;    } else {	cerr << "false" << endl;    }        cerr << endl << "*** int::Testing references ***" << endl;    loadInt(V);    cerr << "Expect   : 10" << endl;    cerr << "V.back() : " << V.back() << endl;    cerr << "Expect   : 1" << endl;    cerr << "V.front(): " << V.front() << endl;    cerr << "Expect   : 7" << endl;    cerr << "V.at(6)  : " << V.at(6) << endl;    cerr << "Expect   : 5" << endl;    cerr << "V[4]     : " << V[4] << endl;    showInt(V);    cerr << endl << "*** int::Testing resize() ***" << endl;    loadInt(V);    V.resize(6);    cerr << "Expect  : 1 2 3 4 5 6" << endl;    showInt(V);    V.resize(9);    cerr << "Expect  : 1 2 3 4 5 6 0 0 0" << endl;    showInt(V);    V.resize(12, 99);    cerr << "Expect  : 1 2 3 4 5 6 0 0 0 99 99 99" << endl;    showInt(V);    cerr << endl << "*** int::Testing size() ***" << endl;    loadInt(V);    cerr << "Expect  : 10" << endl;    cerr << "V.size(): " << V.size() << endl;    showInt(V);        cerr << endl << "*** int::Testing capacity() ***" << endl;    loadInt(V);    cerr << "Capacity: " << V.capacity() << endl;    V.resize(V.size()+V.capacity());    cerr << "Resized capacity: " << V.capacity() << endl;    showInt(V);    cerr << endl << "*** int::Testing reserve() ***" << endl;    loadInt(V);    V.reserve(40);    cerr << "Expect  : 40 (or greater)" << endl;    cerr << "Capacity: " << V.capacity() << endl;    cerr << endl << "*** int::Testing swap() ***" << endl;    V.clear();    V2.clear();    V.push_back(1);    V.push_back(2);    V2.push_back(7);    V2.push_back(8);    V.swap(V2);    cerr << "Expect (V) : 7 8" << endl;    showInt(V);    cerr << "Expect (V2): 1 2" << endl;    showInt(V2);       cerr << endl;    cerr << "*******************" << endl;    cerr << "* END TESTS (int) *" << endl;    cerr << "*******************" << endl;}// This method tests the behavior of a vector<Obj*>.// Your ObjPtrVec class should perform identical to a// vector<Obj*> in as many ways as possible.  You should// create a ObjPtrVec::test() method that performs// testings identifical to those shown here (except// applied to your ObjPtrVec class, not to vector<Obj*>).void objTest() {    vector<Obj*> V,V2;    vector<Obj*>::iterator is, ie;    cerr << endl;    cerr << "*********************" << endl;    cerr << "* BEGIN TESTS (Obj) *" << endl;    cerr << "*********************" << endl;    cerr << endl << "*** Obj::Testing initialization ***  " << endl;    cerr << "Expect  : " << endl;    showObj(V);    cerr << endl << "*** Obj::Testing assign() ***" << endl;    V.assign(10,new Obj(7));    cerr << "Expect  : 7 7 7 7 7 7 7 7 7 7" << endl;    showObj(V);        loadObj(V);    is = V.begin();    is++;    is++;    is++;    ie = is;    ie++;    ie++;    V.assign(is,ie);    cerr << "Expect  : 4 5" << endl;    showObj(V);    cerr << endl << "*** Obj::Testing void push_back() ***" << endl;    loadObj(V);    V.push_back(new Obj(1));    V.push_back(new Obj(2));    V.push_back(new Obj(3));    cerr << "Expect  : 1 2 3 4 5 6 7 8 9 10 1 2 3" << endl;    showObj(V);    cerr << endl << "*** Obj::Testing void pop_back() ***" << endl;    loadObj(V);    V.pop_back();    cerr << "Expect  : 1 2 3 4 5 6 7 8 9" << endl;    showObj(V);    cerr << endl << "*** Obj::Testing begin() ***" << endl;    loadObj(V);    cerr << "Expect   : 1" << endl;    cerr << "V.begin(): " << *(V.begin()) << endl;        cerr << endl << "*** Obj::Testing end() ***" << endl;    loadObj(V);    ie = V.end();    ie--;    cerr << "Expect   : 10" << endl;    cerr << "V.end()-1: " << *ie << endl;    cerr << endl << "*** Obj::Testing insert() ***" << endl;    loadObj(V);    V.insert(V.begin(),new Obj(99));    cerr << "Expect  : 99 1 2 3 4 5 6 7 8 9 10" << endl;    showObj(V);    loadObj(V);    is = V.begin();    is++;    is++;    cerr << "Expect  : 1 2 88 88 88 3 4 5 6 7 8 9 10" << endl;    V.insert(is,3,new Obj(88));    showObj(V);    loadObj(V);    is = V.begin();    is++;    is++;    ie = is;    ie++;    ie++;    V.insert(V.end(),is,ie);    cerr << "Expect  : 1 2 3 4 5 6 7 8 9 10 3 4" << endl;    showObj(V);   cerr << endl << "*** Obj::Testing erase() ***" << endl;    loadObj(V);    is = V.begin();    is++;    is++;    V.erase(is);    cerr << "Expect  : 1 2 4 5 6 7 8 9 10" << endl;    showObj(V);    loadObj(V);    is = V.begin();    is++;    is++;    ie = is;    ie++;    ie++;    V.erase(is,ie);    cerr << "Expect  : 1 2 5 6 7 8 9 10" << endl;    showObj(V);    cerr << endl << "*** Obj::Testing clear() ***" << endl;    loadObj(V);    V.clear();    cerr << "Expect  : " << endl;    showObj(V);        cerr << endl << "*** Obj::Testing empty() ***" << endl;    cerr << "-- V is empty --" << endl;    cerr << "Expect : true" << endl;    cerr << "empty(): ";    if(V.empty()) {	cerr << "true" << endl;    } else {	cerr << "false" << endl;    }    loadObj(V);    cerr << "-- V is NOT empty --" << endl;      cerr << "Expect : false" << endl;    cerr << "empty(): ";    if(V.empty()) {	cerr << "true" << endl;    } else {	cerr << "false" << endl;    }     cerr << endl << "*** Obj::Testing references ***" << endl;    loadObj(V);    cerr << "Expect   : 10" << endl;    cerr << "V.back() : " << V.back()->getVal() << endl;    cerr << "Expect   : 1" << endl;    cerr << "V.front(): " << V.front()->getVal() << endl;    cerr << "Expect   : 7" << endl;    cerr << "V.at(6)  : " << V.at(6)->getVal() << endl;    cerr << "Expect   : 5" << endl;    cerr << "V[4]     : " << V[4]->getVal() << endl;    showObj(V);    cerr << endl << "*** Obj::Testing resize() ***" << endl;    loadObj(V);    V.resize(6);    cerr << "Expect  : 1 2 3 4 5 6" << endl;    showObj(V);    V.resize(9,new Obj());    cerr << "Expect  : 1 2 3 4 5 6 0 0 0" << endl;    showObj(V);    V.resize(12, new Obj(99));    cerr << "Expect  : 1 2 3 4 5 6 0 0 0 99 99 99" << endl;    showObj(V);    cerr << endl << "*** Obj::Testing size() ***" << endl;    loadObj(V);    cerr << "Expect  : 10" << endl;    cerr << "V.size(): " << V.size() << endl;    showObj(V);        cerr << endl << "*** Obj::Testing capacity() ***" << endl;    loadObj(V);    cerr << "Capacity: " << V.capacity() << endl;    V.resize(V.size()+V.capacity(),new Obj());    cerr << "Resized capacity: " << V.capacity() << endl;    showObj(V);    cerr << endl << "*** Obj::Testing reserve() ***" << endl;    loadObj(V);    V.reserve(40);    cerr << "Expect  : 40 (or greater)" << endl;    cerr << "Capacity: " << V.capacity() << endl;       cerr << endl << "*** Obj::Testing swap() ***" << endl;    V.clear();    V2.clear();    V.push_back(new Obj(1));    V.push_back(new Obj(2));    V2.push_back(new Obj(7));    V2.push_back(new Obj(8));    V.swap(V2);    cerr << "Expect (V) : 7 8" << endl;    showObj(V);    cerr << "Expect (V2): 1 2" << endl;    showObj(V2);    cerr << endl;    cerr << "*******************" << endl;    cerr << "* END TESTS (Obj) *" << endl;    cerr << "*******************" << endl;}int main() {    // You should comment these out when submitting your program    intTest();    objTest();    // You should uncomment these before submitting your program    // (and naturally, implement the 'test' method in each of    // your classes)    IntVec::test();    ObjPtrVec::test();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -