CHAPTER8-23.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
- //文件名:CHAPTER8-23.cpp
- #include <iostream>
- #include <list>
- #if _MSC_VER > 1020 // if VC++ version is > 4.2
- using namespace std; // std c++ libs implemented in std
- #endif
- typedef list<char > CHARLIST;
- void print_contents (CHARLIST list, char*);
- int main()
- {
- CHARLIST a(3,'A'); //定义了变量a,内容是3个'A'
- CHARLIST b(4,'B'); //定义了变量b,内容是4个'B'
- print_contents (a,"a");
- print_contents (b,"b");
- a.swap(b); //内容互换
- print_contents (a,"a");
- print_contents (b,"b");
- a.swap(b);//内容换回来
- print_contents (a,"a");
- print_contents (b,"b");
- a.assign(b.begin(),b.end());
- print_contents (a,"a");
- CHARLIST::iterator itor=b.begin();
- itor++;
- itor++;
- a.assign(b.begin(),itor);
- print_contents (a,"a");
- a.assign(3,'Z');
- print_contents (a,"a");
- return 0;
- }
- //下面是显示函数源代码
- void print_contents (CHARLIST list, char *name)
- {
- CHARLIST::iterator plist;
- cout <<"The contents of "<< name <<" : ";
- for(plist = list.begin(); plist != list.end(); plist++)
- { cout << *plist <<" " ; }
- cout<<endl;
- }