分享
 
 
 

STL的心得(3)---运用(控制台)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

很多网上介绍的都是关于STL在控制台的运用,我认为,这总适合与学习理论知识以及其实践,当真正用到项目中去的

恐怕这就不适合了。我也老套一下。分为两种情况。

1)控制台的运用

2)用MFC写一个简单的画图程序(恐怕这个应该是大家更加注意的)

OK,先说控制台:

下面源代码,可以直接运行(VC6 Console)(关于其整个工程文件可以到www.confach.533.net去Download),如果你找不到,可以直接按上面的联系方式与我Mail,我会尽快给你发过去,大家一起学习)

#ifdef _MSC_VER

#endif

#include "stdafx.h"

#pragma warning(disable:4786)

#pragma warning(disable:4514)

#include

#include

#include

#include

#include

#include

#include #include #include #include using namespace std; class PrintInt{public: void operator()(int elem) const { std::cout< }};//define a classclass StudentID{public: StudentID(int id=0) { value=id; std::cout<<"Assigning student id:"< } ~StudentID() { std::cout<<"Destructing ID:"< }private: int value;};class Student{public: Student(char *pName="noName",int ssID=0):id(ssID) { std::cout<<"Constructing student:"< strncpy(name,pName,sizeof(name)); name[sizeof(name)-1]=0; StudentID id(ssID); }private: char name[20]; StudentID id;};void TestMMap(){ typedef multimap IntStringMap; IntStringMap coll;// coll.insert(std::make_pair(5,"tagged"));// coll.insert(make_pair(2,"a"));// coll.insert(make_pair(1,"this"));// coll.insert(make_pair(4,"of"));// coll.insert(make_pair(6,"strings"));// coll.insert(make_pair(1,"is"));// coll.insert(make_pair(3,"multimap"));// /* print all element values *- iterate over all elements *- element member second is the value */ std::cout<<"========================================"<// IntStringMap::iterator pos;// for (pos = coll.begin(); pos != coll.end(); ++pos)// {// std::cout << pos->second <<" ";// } std::cout << endl; }void TestSet(){ typedef std::set >IntSet; IntSet set1; set1.insert(1); set1.insert(3); set1.insert(5); set1.insert(1); set1.insert(4); set1.insert(6); std::cout<<"========================================"< IntSet::const_iterator pos; for(pos=set1.begin();pos!=set1.end();++pos) { std::cout<<*pos<<" "<< " set"< }}void TestAlgorithm(){ vector mVector; vector::iterator pos; mVector.push_back(1); mVector.push_back(2); mVector.push_back(5); mVector.push_back(76); mVector.push_back(4); std::cout<<"====================================="< for(pos=mVector.begin();pos!=mVector.end();++pos) { std::cout<<*pos< } pos=min_element(mVector.begin(),mVector.end()); std::cout<<"min:"<<*pos< pos=max_element(mVector.begin(),mVector.end()); std::cout<<"Max:"<<*pos< std::cout<<"===================================="< std::cout<<" Sort "< std::cout<<"===================================="< std::sort(mVector.begin(),mVector.end()); for(pos=mVector.begin();pos!=mVector.end();++pos) { std::cout<<*pos< } std::cout<<"===================================="< std::cout<<" Reverse "< std::cout<<"===================================="< std::reverse(mVector.begin(),mVector.end()); for(pos=mVector.begin();pos!=mVector.end();++pos) { std::cout<<*pos< } }void TestIteratorAdapter(){ list coll1; //insert elements from 1 to 9 into the first collection for (int i=1; i<=9; ++i) { coll1.push_back(i); } // copy the elements of coll1 into coll2 by appending vector coll2; copy (coll1.begin(), coll1.end(), //source back_inserter(coll2)); //destination //copy the elements of coll1 into coll3 by inserting them front // - reverses the order of the elements //Remarks:if use front_inserter,the container must provide push_front method deque coll3; copy (coll1.begin(), coll1.end(), //source front_inserter(coll3)); //destination //copy elements of coll1 into coll4 // - only inserter that works for associative collections set coll4; copy (coll1.begin(), coll1.end(), //source inserter(coll4,coll4.begin())); //destination std::cout<<"=====================List=========="< list::iterator pos; for(pos=coll1.begin();pos!=coll1.end();++pos) { std::cout<<*pos< } std::cout<<"=====================Vector(List use back_insert)=========="< vector::iterator pos1; for(pos1=coll2.begin();pos1!=coll2.end();++pos1) { std::cout<<*pos1< } std::cout<<"=====================Deque(List use front_insert)=========="< deque::iterator pos2; for(pos2=coll3.begin();pos2!=coll3.end();++pos2) { std::cout<<*pos2< } std::cout<<"=====================Set(List use inserter)=========="< set::iterator pos3; for(pos3=coll4.begin();pos3!=coll4.end();++pos3) { std::cout<<*pos3< } }void TestStreamIterator(){ vector coll; /*read all words from the standard input * - source: all strings until end-of-file (or error) * - destination: coll (inserting) */ copy (istream_iterator(cin), //start of source istream_iterator(), //end of source back_inserter(coll)); //destination //sort elements sort (coll.begin(), coll.end()); /*print all elements without duplicates * - source: coll * - destination: standard output (with newline between elements) */ unique_copy (coll.begin(), coll.end(), //source ostream_iterator (cout, "\n")); //destination} template inline void PRINT_ELEMENTS (const T& coll, const char* optcstr=""){typename T::const_iterator pos;std::cout << optcstr<for (pos=coll.begin(); pos!=coll.end(); ++pos) {std::cout << *pos << ' '<}std::cout << std::endl;} //void main()//{// const int size=100;// int array[size];// array[0]=0;// array[1]=1;// array[1000]=1000;//// cout<// //}typedef struct tagIntegerNode { int iValue; tagIntegerNode *pNextIntegerNode;}INTEGERNODE; class A { public: // Made public to simplify tracing // Would normally be protected: static INTEGERNODE *pFirstIntegerNode;}; // Static data members must be initialized at file scope, even// if private.INTEGERNODE *A::pFirstIntegerNode; class B:public A { public: B( void );}; class C:public A { public: C( void );}; B::B( void ){ pFirstIntegerNode = new INTEGERNODE; pFirstIntegerNode->iValue = 1; pFirstIntegerNode->pNextIntegerNode = NULL;}; C::C( void ){ pFirstIntegerNode->pNextIntegerNode = new INTEGERNODE; pFirstIntegerNode->pNextIntegerNode->iValue = 2; pFirstIntegerNode->pNextIntegerNode->pNextIntegerNode = NULL;}; //----------------------------------------------------//-----------------------------------------------------typedef list ListElement;typedef list OwningList;void OutputList(const ListElement& EntireListElement,int listNumber){ ostream_iterator out(cout," "); cout<<"List "< copy(EntireListElement.begin(),EntireListElement.end(),out); cout< }void TestOwningList(){ OwningList ListOfListElements; for(int i=0;i<3;++i) { ListElement EntireListElement; for(int j=0;j<5;++j) { EntireListElement.push_back (i*4+j); } OutputList(EntireListElement,i+1); ListOfListElements.push_back(EntireListElement); } cout< OwningList::iterator it=ListOfListElements.begin(); for(int j=1;it!=ListOfListElements.end();++it,++j) { const ListElement& EntireListElement1=*it; OutputList(EntireListElement1,j); } }typedef vector INTVECTOR; void TestVectorAttr1(){ // Dynamically allocated vector begins with 0 elements. INTVECTOR theVector; // Add one element to the end of the vector, an int with the value 42. theVector.push_back(42) ; // Show statistics about vector. cout << "theVector's size is: " << theVector.size() << endl; cout << "theVector's maximum size is: " << theVector.max_size() << endl; cout << "theVector's capacity is: " << theVector.capacity() << endl; // Ensure there's room for at least 1000 elements. theVector.reserve(1000); cout << endl << "After reserving storage for 1000 elements:" << endl; cout << "theVector's size is: " << theVector.size() << endl; cout << "theVector's maximum size is: " << theVector.max_size() << endl; cout << "theVector's capacity is: " << theVector.capacity() << endl; // Ensure there's room for at least 2000 elements. theVector.resize(2000); cout << endl << "After resizing storage to 2000 elements:" << endl; cout << "theVector's size is: " << theVector.size() << endl; cout << "theVector's maximum size is: " << theVector.max_size() << endl; cout << "theVector's capacity is: " << theVector.capacity() << endl; }void TestVectorCreate(){ cout<<"============================================="< cout<<"====================Test Vector============="< cout<<"============================================="< INTVECTOR c1; c1.push_back(1); c1.push_back(2); c1.push_back(3); c1.push_back(4); INTVECTOR::iterator it; cout<<"---Create a empty vector without any element"< for(it=c1.begin();it!=c1.end();++it) { cout<<*it<<","; } cout< cout<<"--Create a copy vector"< INTVECTOR c2(c1); for(it=c2.begin();it!=c2.end();++it) { cout<<*it<<","; } cout< cout<<"---Create a vector with n elements"< INTVECTOR c3(8); c3.assign(8,1); c3.push_back(10); c3.push_back(12); c3.push_back(14); c3.push_back(16); c3.push_back(18); for(it=c3.begin();it!=c3.end();++it) { cout<<*it< } cout<<"---Create a vector with n copies of elements"< INTVECTOR c4(5,10);for(it=c4.begin();it!=c4.end();++it) { cout<<*it< } int nArr[]={1,2,3,4,5};INTVECTOR c5(nArr,nArr+sizeof(nArr)/sizeof(int));cout<<"---Create a vector with a array"<for(it=c5.begin();it!=c5.end();++it) { cout<<*it< }cout<<"--Test End----------"<} void TestVectorIterator(){ vector ages(15) ; ages.push_back(15); vector::const_iterator p=ages.begin(); for(int i=0;i<10;i++) { ages.push_back(i); } cout<<"===========Test Iterator=============="< //p=ages.begin(); cout<<"The first age is:"<<*p< cout<<"===============End===================="< }class Shape{public: virtual void Draw() { cout<<"Draw it in Shape class"< } virtual void GetSize()=0; };class Rectangle:public Shape{public : virtual void Draw() { cout<<"Draw it in Rectangle class"< } void GetSize() { cout<<"Get size in Rectangle class"< } };void TestPointer(){ int i=100; char c='a'; float f=i; double d=f; bool b=true; int *pI=&i; int **ppI=&pI; cout<<"Address of i :0x"<<&i< printf("Address of c :0x%p\n",&c); cout<<"Address of f :0x"<<&f< cout<<"Address of d :0x"<<&d< cout<<"Address of b :0x"<<&b< cout<<"Address of pI :0x"<<&pI< cout<<"Value of pI :0x"< cout<<"Address of ppI :0x"<<&ppI< cout<<"Value of ppI :0x"<} void main(){ TestPointer(); return; Shape *pShape=NULL; Rectangle a; pShape=&a; pShape->Draw(); pShape->GetSize(); return; TestVectorCreate(); TestVectorAttr1(); TestVectorIterator(); return; TestOwningList(); B instanceB; C instanceC; cout<iValue < cout<pNextIntegerNode->iValue< short int iArray[1000]; for(int i1=0;i1<1000;i1++) { iArray[i1]=i1; } Student s("confach zhang",99471136); vectorcol1; dequedeq1; listlist1; std::cout<<"=================================================="< std::cout<<" index value Type"< std::cout<<"=================================================="< for(int i=0;i<100;i++) { if(i%2==0) col1.push_back(i); } for( i=0;i { std::cout< } std::cout<<"===============Object Function=================="< for_each(col1.begin(),col1.end(),PrintInt()); std::cout<<"=================================================="< std::cout<<" index value Type"< std::cout<<"=================================================="< for(i=0;i<100;i++) { if(i%2==0) { deq1.push_front(i); } else { deq1.push_back(i); } } for(i=0;i { std::cout< } std::cout<<"=================================================="< std::cout<<" index value Type"< std::cout<<"=================================================="< for(char c='a';c<='z';c++) { list1.push_back(c); } while(!list1.empty()) { std::cout< list1.pop_front(); } std::cout<<"=================================================="< std::cout<<" index value Type"< std::cout<<"=================================================="< for( c='a';c<='z';c++) { list1.push_front(c); } std::cout<<"==============User Define function=========================="< PRINT_ELEMENTS(list1,"confach"); list::iterator pos; //list::const_iterator//for read_only //list::iterator//for read/write for(pos=list1.begin();pos!=list1.end();++pos) //Caution:// Note that the preincrement operator (prefix ++) is used here. This is because it might have better//performance than the postincrement operator. The latter involves a temporary object because it//must return the old position of the iterator. For this reason, it generally is best to prefer ++pos//over pos++. Thus, you should avoid the following version://for (pos = coll.begin(); pos != coll.end(); pos++) {// ^^^^^ // OK, but slower//...//}//For this reason, I recommend using the preincrement and pre-decrement operators in general. { std::cout< } TestSet(); TestMMap(); TestAlgorithm(); std::cout<}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有