STL的心得(3)---运用(控制台)
很多网上介绍的都是关于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 <vector>
#include <iostream>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <iosfwd>
#include <iterator>
class PrintInt
{
public:
void operator()(int elem) const
{
std::cout<
};
//define a class
class 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:"<
name[sizeof(name)-1]=0;
StudentID id(ssID);
}
private:
char name[20];
StudentID id;
};
void TestMMap()
{
typedef multimap
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<<"========================================"<
// for (pos = coll.begin(); pos != coll.end(); ++pos)
// {
// std::cout << pos->second <<" ";
// }
std::cout << endl;
}
void TestSet()
{
typedef std::set
IntSet set1;
set1.insert(1);
set1.insert(3);
set1.insert(5);
set1.insert(1);
set1.insert(4);
set1.insert(6);
std::cout<<"========================================"<
for(pos=set1.begin();pos!=set1.end();++pos)
{
std::cout<<*pos<<" "<< " set"<
}
void TestAlgorithm()
{
vector
vector
mVector.push_back(1);
mVector.push_back(2);
mVector.push_back(5);
mVector.push_back(76);
mVector.push_back(4);
std::cout<<"====================================="<
{
std::cout<<*pos<
pos=min_element(mVector.begin(),mVector.end());
std::cout<<"min:"<<*pos<
std::cout<<"Max:"<<*pos<
for(pos=mVector.begin();pos!=mVector.end();++pos)
{
std::cout<<*pos<
std::cout<<"===================================="<
for(pos=mVector.begin();pos!=mVector.end();++pos)
{
std::cout<<*pos<
}
void TestIteratorAdapter()
{
list
//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
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
copy (coll1.begin(), coll1.end(), //source
front_inserter(coll3)); //destination
//copy elements of coll1 into coll4
// - only inserter that works for associative collections
set
copy (coll1.begin(), coll1.end(), //source
inserter(coll4,coll4.begin())); //destination
std::cout<<"=====================List=========="<
for(pos=coll1.begin();pos!=coll1.end();++pos)
{
std::cout<<*pos<
std::cout<<"=====================Vector(List use back_insert)=========="<
for(pos1=coll2.begin();pos1!=coll2.end();++pos1)
{
std::cout<<*pos1<
std::cout<<"=====================Deque(List use front_insert)=========="<
for(pos2=coll3.begin();pos2!=coll3.end();++pos2)
{
std::cout<<*pos2<
std::cout<<"=====================Set(List use inserter)=========="<
for(pos3=coll4.begin();pos3!=coll4.end();++pos3)
{
std::cout<<*pos3<
}
void TestStreamIterator()
{
vector
/*read all words from the standard input
* - source: all strings until end-of-file (or error)
* - destination: coll (inserting)
*/
copy (istream_iterator
istream_iterator
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
//destination
}
template
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr<
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
typedef list
void OutputList(const ListElement& EntireListElement,int listNumber)
{
ostream_iterator
cout<<"List "<
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<
for(int j=1;it!=ListOfListElements.end();++it,++j)
{
const ListElement& EntireListElement1=*it;
OutputList(EntireListElement1,j);
}
}
typedef vector
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<<"============================================="<
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"<
{
cout<<*it<<",";
}
cout<
for(it=c2.begin();it!=c2.end();++it)
{
cout<<*it<<",";
}
cout<
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"<
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"<
{
cout<<*it<
cout<<"--Test End----------"<
void TestVectorIterator()
{
vector
ages.push_back(15);
vector
for(int i=0;i<10;i++)
{
ages.push_back(i);
}
cout<<"===========Test Iterator=============="<
cout<<"The first age is:"<<*p<
}
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<
cout<<"Address of f :0x"<<&f<
cout<<"Address of pI :0x"<<&pI<
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<
short int iArray[1000];
for(int i1=0;i1<1000;i1++)
{
iArray[i1]=i1;
}
Student s("confach zhang",99471136);
vector
deque
list
std::cout<<"=================================================="<
{ if(i%2==0)
col1.push_back(i);
}
for( i=0;i
std::cout< }
std::cout<<"===============Object Function=================="<
std::cout<<"=================================================="<
{
if(i%2==0)
{
deq1.push_front(i);
}
else
{
deq1.push_back(i);
}
}
for(i=0;i
std::cout< }
std::cout<<"=================================================="<
{
list1.push_back(c);
}
while(!list1.empty())
{
std::cout<
}
std::cout<<"=================================================="<
{
list1.push_front(c);
}
std::cout<<"==============User Define function=========================="<
list
//list
//list
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<