很多网上介绍的都是关于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:"<
<
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<
< p>
}
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<
< p>
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);
vector
col1;
deque
deq1;
list
list1;
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<
<<"? Vector?<
}
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<
<<"?? Deque?<
}
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<
<<"? Iterator?<
}
TestSet();
TestMMap();
TestAlgorithm();
std::cout< <<"===============TEST Iterator Adapter='========"< }