正文

C++初学者代码2007-04-27 12:59:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/25273.html

分享到:

// 以下是自己当初学 C++ 时的代码,有点象草稿纸,希望对初学者有所帮助:// 所有函数均在VC下编译运行: // 1,特殊函数的调用顺序问题#include <iostream>using namespace std; void test();class mycls{public: mycls(){  cout<<"construct mycls"<<endl; } ~mycls(){  cout<<"destroy mycls"<<endl; }}; class myexp{public: myexp(){  cout<<"construct myexp"<<endl; } ~myexp(){  cout<<"destroy myexp"<<endl; } myexp(myexp& t){  cout<<"copy construct myexp"<<endl; } /*  注释掉拷贝构造函数结果是:  construct mycls  in try  in fun test()  construct myexp  destroy myexp  destroy mycls  in catch  destroy myexp  Press any key to continue . . .  不注释掉结果是:  construct mycls  in try  in fun test()  construct myexp  destroy mycls  in catch  destroy myexp  Press any key to continue . . .  */ }; int main(){  try{  mycls t;  cout<<"in try"<<endl;  test();  cout<<"this will not show in screen !"<<endl; }catch(myexp){  cout<<"in catch"<<endl; } return 0;}void test(){ cout<<"in fun test()"<<endl; throw myexp();} // 2,叠代器#include <iostream>#include <vector> using namespace std; int main(){ vector<int> myvtin; for(int i = 2; i < 7; i ++)  myvtin.push_back(i); cout<<"input iterator:"; for(vector<int>::iterator it = myvtin.begin(); it != myvtin.end(); it ++)  cout<<*it<<" "; cout<<endl;  vector<int> myvtout(5); vector<int>::iterator itout = myvtout.begin(); *itout++ = 20; *itout++ = 23; *itout++ = 25; *itout++ = 14; *itout = 27; cout<<"output iterator:"; for(vector<int>::iterator it = myvtout.begin(); it != myvtout.end(); it ++)  cout<<*it<<" "; cout<<endl;   vector<int> myvtfw(5); vector<int>::iterator itfw = myvtfw.begin(); vector<int>::iterator itfwsave = itfw; for(int i = 7; i < 12; itfw ++,i ++)   *itfw = i; cout<<"forward iterator:"; for(; itfwsave != myvtfw.end(); itfwsave ++)  cout<<*itfwsave<<" "; cout<<endl; /* vector<int> myvtdd(5);   vector<int>::iterator itdd = myvtdd.begin(); for(int i = 9; i < 14; itdd ++,i ++)   *itdd = i; cout<<"double dest iterator:"; itdd = myvtdd.end()-1; do {  cout<<*itdd<<" ";  // 输出有问题   -- itdd;  if(itdd == myvtfw.begin())   break; }while(true); cout<<endl;*/ vector<int> myvtrd(5); vector<int>::iterator itrd = myvtrd.begin(); *itrd++ = 23; *itrd++ = 2; *itrd++ = 63; *itrd++ = 73; *itrd = 93; cout<<"random iterator:"; cout<<*(itrd - 4)<<" "<<*(itrd-2)<<" "; itrd -= 3; cout<<*(itrd++)<<" "<<*(itrd+2)<<endl; return 0;} // 3,多重继承#include <iostream>using namespace std; class base{public: base(){  cout<<"base"<<endl; }    virtual ~base(){  cout<<"~base"<<endl; } virtual void test(){}};class baseTwo{public: baseTwo(){  cout<<"baseTwo"<<endl; }    virtual ~baseTwo(){  cout<<"~baseTwo"<<endl; }};class derived:public base,public baseTwo{public: derived(){  cout<<"derived"<<endl; } ~derived(){  cout<<"~derived"<<endl; }};int main(){ base *p = new derived;  //cout<<sizeof(base)<<" "<<sizeof(derived)<<endl; delete p; return 0;} // 4,二义性问题// 二义性是指,派生类继承多个基类时,基类里有同名的数据成员或者成员函数// 而派生类里没有该成员,派生类的实例不知道应该用哪个基类的成员,这就是// 二义性,下面的程序因为存在二义性编译无法通过#include <iostream>using namespace std; class base{public: base(){  cout<<"base"<<endl; }    virtual ~base(){  cout<<"~base"<<endl; }    void test(){  cout<<"base test()"<<endl; }};class baseTwo{public: baseTwo(){  cout<<"baseTwo"<<endl; }    virtual ~baseTwo(){  cout<<"~baseTwo"<<endl; } void test(){  cout<<"baseTwo test()"<<endl; }};class derived:public base,public baseTwo{public: derived(){  cout<<"derived"<<endl; } ~derived(){  cout<<"~derived"<<endl; }};int main(){    derived p;  //cout<<sizeof(base)<<" "<<sizeof(derived)<<endl; p.test(); return 0;} // 5,命名空间#include <iostream>using namespace std; namespace mysp{ int j ; int i;} namespace mysp2{ int j; int i;}using namespace mysp;using namespace mysp2; int main(){// i = 32;  //: error C2872: 'i' : ambiguous symbol printf("%d",mysp::i);  return 0;} // 6,文件操作#include <iostream>#include <fstream>#include <string>using namespace std; int main(){// ofstream tfile("file.txt");// cout<<"create file "<<endl;// tfile<<"first test"<<str<<endl; // ofstream afile("file.txt",std::ios::app);// str += " app file";// afile << str<<endl; /* ifstream ifile("file.txt",ios::in | ios::out | ios::binary); if(ifile.fail())  cout<<"can't open file file.txt"<<endl; ostream ofile(ifile.rdbuf()); char buf[100]; int  i; for(i = 0;!ifile.eof() && i < sizeof(buf);){  char c;  ifile.get(buf[i++]);//(c); } ofile.seekp(0); for(int j = 0; j < i-1; j ++)  ofile.put(static_cast<char>(toupper(buf[j])));*/ fstream myfile("file.txt",ios::in | ios::out); char c; while(!myfile.eof()){  myfile.get(c);  cout<<c; } myfile.seekp(-4,ios::cur); myfile<<"deng lanzhong hello !"<<endl; char p[] = {'a','b'}; cout<<sizeof p<<endl;  /*  ofstream ofile("file.txt"); if(ofile.fail())  cout<<"can't open file file.txt"<<endl; else {  char c;  ofile.seekp(10); // ofile.getline(c,80);//(c); // cout<<c<<endl; // ostream ofile(ofile.rdbuf());  while(!ofile.eof()){   ofile<<"deng test read and write "<<endl;   }*/  return 0;} // 7,显示构造函数#include <iostream>using namespace std; class cMytype{ char a; char b;public: cMytype(char c='a',char d='b'):a(c),b(d){} operator int(){return a+b;} // 需要强制类型转换 explicit cMytype(int c){ a = 67; b = c - 67; } // 隐式构造函数 cMytype(int c){ a = 67; b = c - 67; } void show(){  cout<<a<<' '<<b<<endl; }}; int main(){ cMytype c('c'); cMytype t=(cMytype)140; t.show(); return 0;} // 8,虚析够函数#include <iostream>using namespace std; class base{public: base(){  cout<<"base"<<endl; } virtual ~base(){  cout<<"~base"<<endl; }};class derived:public base{public: derived(){  cout<<"derived"<<endl; } ~derived(){  cout<<"~derived"<<endl; }}; int main(){ base *p = new derived;  // 如果 base 的析构函数不是虚的则下面删除    // p时不会调用 derived 的析构函数 delete p; return 0;} // 9,异常#include <stdexcept>#include <iostream>#include <typeinfo>using namespace std; class test:public runtime_error{public: test():runtime_error("test error"){}}; int main(){ try{  test(); }catch(exception& at){  cout<<at.what(); } return 0;} // 10,有界模版数组#include <iostream>#include <cassert>using namespace std; template<class T,int max>class Tmp{ T t[max];public: T& operator[](int i){  assert(i >= 0 && i < max);  return t[i]; }}; int main(){    Tmp<int,10> a;  for(int i = 0; i<=10; i ++) // 导致assert失败  a[i] = i;  //cout<<sizeof(base)<<" "<<sizeof(derived)<<endl; return 0;} // 11,重载#include <iostream>using namespace std; class test{ static int count; int i; int j;public: test(int a=0,int b=0):i(a),j(b){  count ++;  cout<<"construct test:"<<count<<endl; } test(test& t){  count ++;  cout<<"copy construct test:"<<count<<endl;  i = t.i;  j = t.j; } ~test(){  cout<<"destroy test:"<<count<<endl;  count --; } void put(); test& operator=(test& t); test operator+(test& t)const; test& operator+=(test& t); friend int operator+(int a,test& t); bool operator==(test& t); bool operator<(test& t); bool operator>(test& t); test& operator++(); test operator++(int); test& operator-(); test& operator+(); friend ostream& operator<<(ostream& out,test&t); friend istream& operator>>(istream& in,test&t); int operator[](int i); }; class ptest{ test *pt; public: ptest(test* p=0):pt(p){} test* operator->(){  static test t(0,0);  if(!pt)   return &t;  else   return pt; }}; int test::count = 0; ostream& operator<<(ostream& out,test&t){ out<<t.i<<" "<<t.j; return out;} istream& operator>>(istream& in,test&t){ in>>t.i>>t.j; return in;} int test::operator[](int i){ if(!i)  return this->i; else if(i)  return this->j; return -1;}test& test::operator-(){ this->i = - this->i; this->j = - this->j; return *this;}test& test::operator+(){ return *this;}test test::operator++(int){ test temp; temp = *this; this->i ++; this->j ++; return temp;}test& test::operator++(){ this->i ++; this->j ++; return *this;}void test::put(){  cout<<"i="<<i<<" "<<"j="<<j<<endl;}test& test::operator=(test& t){  this->i = t.i;  this->j = t.j;  return *this;}test test::operator+(test& t)const{ test nt; nt.i = this->i + t.i; nt.j = this->j + t.j; return nt;}bool test::operator==(test&t){ return ( this->i == t.i &&       this->j == t.j );}bool test::operator<(test&t){ return (this->i < t.i ||       (this->i == t.i && this->j < t.j));}bool test::operator>(test&t){ return t < *this;}int operator+(int a,test& t){ return a+t.i+t.j;}test& test::operator +=(test &t){ this->i += t.i; this->j += t.j; return *this;} int main(){ test a(4,4),b(3,29); ptest p;  a.put(); b.put(); cout<<"test <<:"<<endl; cout<<a; cout<<"test >>: enter n m:"<<endl; cin>>a; cout<<"after enter a:"<<a<<endl;  // cout<<"test a++"<<endl;// a ++;// cout<<"test b = a++"<<endl;/// b = a ++;// b.put(); // cout<<"test +:"<<endl;// +b;// b.put();     cout<<"test p = &:"<<endl; p->put(); p = &a;    p->put(); // cout<<"test []:"<<endl;// cout<<"a[0]:"<<a[0]<<endl; /* cout<<"test a>b:"; if(a>b){  a.put();  cout<<"is bigger than ";  b.put(); } else {  b.put();  cout<<"is bigger than ";  a.put(); }  cout<<"test += :"<<endl; cout<<"a:"; a.put(); cout<<"b:"; b.put();    a += b; cout<<"after a+=b,a:"; a.put();*/  return 0;} // 12,重载 new#include <iostream>#include <new>#include <cstddef>#include <cstring>using namespace std; const int  nameLength = 10;const int  max = 5;class student{ char        name[nameLength]; static char pool[]; static short int bInUse[max];public: student(char*str); student::student(){strcpy(name,"");} void* operator new(size_t)throw(bad_alloc); void  operator delete(void*)throw(); void* operator new[](size_t size)throw(bad_alloc); void  operator delete[](void*)throw(); void  put();};  char student::pool[max*sizeof(student)]; short int student::bInUse[max]; student::student(char *str){ strncpy(name,str,sizeof(name));} void* student::operator new(size_t)throw(bad_alloc){ for(int j = 0; j < max; j ++)  if(!bInUse[j]){   bInUse[j] = true;   return pool+j*sizeof(student);  } throw bad_alloc();} void  student::operator delete(void*p)throw(){ if(p)  bInUse[((char*)p-pool)/(sizeof(student))] = false;}void* student::operator new[](size_t size)throw(bad_alloc){ int elements = size/sizeof(student); for(int j = 0; j < max; j ++)  if(!bInUse[j]){   int i;   for(i = 0; i < elements && j+i < max; i ++)    if(bInUse[i])     break;   if(i == elements && j+i <= max){    for(i = 0; i < elements; i ++)     bInUse[j+i] = elements;    return pool+j*sizeof(student);   }  } throw bad_alloc();}void  student::operator delete[](void*p)throw(){ if(p){  int j = ((char*)p-pool)/sizeof(student);  int elements = bInUse[j];  for(int i = 0; i < elements; i ++)   bInUse[((char*)p-pool)/(sizeof(student))] = false; }}void  student::put(){ cout<<name<<endl;} int main(){ student *ps[max]; int i; for(i = 0; i < max; i ++){  cout<<i<<" enter Name:";  char name[nameLength];  cin>>name;  ps[i] = new student(name); } cout<<"you enter followed names:"<<endl; for(i = 0; i < max; i ++){  ps[i]->put();  delete ps[i]; } student* psa=new student[max]; for(i = 0; i < max; i ++){  cout<<i<<" enter Name:";  char name[nameLength];  cin>>name;  *(psa + i) = name; } cout<<"you enter followed names:"<<endl; for(i = 0; i < max; i ++)  ps[i]->put();    delete []psa; return 0;} // 13,资源释放#include <iostream>using namespace std; class mycls{public: mycls(){  cout<<"construct mycls"<<endl; } ~mycls(){  cout<<"destroy mycls"<<endl; }};class myexp{ int j;public: myexp(int i):j(i){  cout<<"construct myexp"<<endl; } myexp(){  cout<<"construct myexp"<<endl; } ~myexp(){  cout<<"destroy myexp"<<endl; } void put(){  cout<<"j:"<<j<<endl; } myexp(myexp& t){  j = t.j;  cout<<"copy construct myexp"<<endl; }};class myques{public: myques(){  cout<<"construct myques"<<endl; } ~myques(){  cout<<"destroy myques"<<endl; }}; void test()throw(myexp); // 异常情况指定 void myterminate(){// 处理未经捕获的异常函数 cout<<"in myterminate():"<<endl;} void myunexpected(){// 处理函数抛出的未经指定的异常 ???? cout<<"in myunexpected():"<<endl;} myques *myp;  //测试资源的释放 int main()throw(myexp){  set_terminate(myterminate); set_unexpected(myunexpected);  try{  cout<<"in try"<<endl;  mycls t;  test();  cout<<"this will not show in screen !"<<endl; }catch(myexp x){  x.put();  cout<<"in catch"<<endl;  }//catch(...){ // cout<<"catch all"<<endl;  // 这个异常不被捕获有 terminate() 处理,最后调用 abort()函数 //} return 0;}void test()throw(myexp){ myp = new myques; throw myexp(); delete myp; }/*in tryconstruct myclsconstruct myquesconstruct myexpcopy construct myexpdestroy myclsj:-858993460in catchdestroy myexpdestroy myexpPress any key to continue*/ // 14,STLbitset#include <iostream>#include <iomanip>#include <bitset>using namespace std; void printBit(bitset<20>& bit){ cout<<"Bitset="; for(int i = 0; i < 20; i ++)  cout<<bit[i]; cout<<endl;} void testBit(bitset<20>& bit){ cout<<"test bitset<20> as followed:"<<endl; for(int i = 0; i < 20; i ++)  if(bit.test(i))   cout<<"bit:"<<setw(2)    <<setiosflags(ios::right)<<i<<" set"<<endl;  else   cout<<"bit:"<<setw(2)    <<setiosflags(ios::right)<<i<<" unset"<<endl;} int main(){   bitset<20> mybit;  cout<<"sizeof(mybit):"<<sizeof mybit<<endl; cout<<"before set:"<<endl;  printBit(mybit);  int j ; for(int i = 0; i <  4; i ++){  j = rand()%20;  cout<<"set:"<<j<<endl;  mybit.set(j); }  cout<<"after set:"<<endl; printBit(mybit);  mybit.reset(j); cout<<"after reset:"<<j<<endl; printBit(mybit);  testBit(mybit);  return 0;} // 15,STLdeque#include <iostream>#include <deque>using namespace std; int main(){/* deque<int> myArray(10,22); deque<int>::iterator iter; int i = 0;  for(iter = myArray.begin(); iter != myArray.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;   deque<char> myArray; deque<char>::iterator iter; int i = 0; for(i = 0; i < 6; i ++)  myArray.push_front('a'+ i);  for(i = 0,iter = myArray.begin(); iter != myArray.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;  deque<char> my; my.insert(my.begin(),20,'a');  deque<char>::iterator iter; int i = 0; for(iter = my.begin(); iter != my.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl; my[0] = 'K'; cout<<my[0]<<endl;  deque<int> myArray(2,10); int i = 0; for(i = 10; i < 16; i ++)  myArray.push_back(i); for(i = myArray.size(); i>= 0; i -= 2){  deque<int>::iterator iterT;  for(i = myArray.size(),iterT = myArray.begin(); iterT != myArray.end(); iterT ++)   cout<<*iterT<<" ";  myArray.pop_back();  myArray.pop_front();  cout<<endl; }*/  deque<char> myArray1;  int i = 0; cout<<myArray1.max_size()<<endl; for(i = 10; i < 16; i ++)  myArray1.push_back(i+'a'); deque<char>::iterator iter; myArray1.resize(20,'a'); for(iter =  myArray1.begin(); iter != myArray1.end(); iter ++)   cout<<*iter<<" "; cout<<endl; deque<char> myArray2; myArray2.assign(myArray1.begin()+2,myArray1.end()-2); for(iter = myArray2.begin(); iter != myArray2.end(); iter ++)  cout<<*iter<<" ";  return 0;} // 16,STLlist#include <iostream>#include <list>using namespace std; int main(){/*   list<int> mylist(10,22); list<int>::iterator iter; int i = 0;  for(iter = mylist.begin(); iter != mylist.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;    list<char> mylist; list<char>::iterator iter; int i = 0; for(i = 0; i < 6; i ++)  mylist.push_front('a'+ i); mylist.insert(++mylist.begin(),10,'x'); for(i = 0,iter = mylist.begin(); iter != mylist.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;*/  list<char> myt;  int i;  for(i = 0; i < 6; i ++)  myt.push_front('a'+ i);  list<char>::iterator iter; myt.reverse(); for(iter = myt.begin(); iter != myt.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;   return 0;} // 17,STLmap#include <iostream>#include <map>using namespace std; int main(){   map<int,int> mymap;  mymap.insert(map<int,int>::value_type(1,523)); mymap.insert(map<int,int>::value_type(2,23)); mymap.insert(map<int,int>::value_type(3,263)); mymap.insert(map<int,int>::value_type(4,293)); mymap.insert(map<int,int>::value_type(5,1223)); mymap.insert(map<int,int>::value_type(8,23));  mymap[0] = 20; map<int,int>::iterator iter; for(iter = mymap.begin(); iter != mymap.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl;  cout<<endl; cout<<"after erase():"<<endl; mymap.erase(++mymap.begin()); for(iter = mymap.begin(); iter != mymap.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl;  cout<<endl; cout<<"find key 7:"<<endl; if((iter = mymap.find(7))!=mymap.end())  cout<<"I find it ,value is:"<<iter->second<<endl; else  cout<<"Can't find it"<<endl;  map<int,int> mymap2(mymap.begin(),mymap.end()); mymap2[2] = 100; cout<<"mymay2 is :"<<endl; for(iter = mymap2.begin(); iter != mymap2.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl; if(mymap > mymap2)  // 比较这里,比较相同键值的元素  cout<<"mymap > mymap2"<<endl; else if(mymap == mymap2)  cout<<"mymap = mymap2"<<endl; else  cout<<"mymap < mymap2"<<endl;  return 0;} // 18 ,STLmultimap#include <iostream>#include <map>using namespace std; typedef multimap<int,char> MYMAP; int main(){   MYMAP mymap;  mymap.insert(MYMAP::value_type(4,'y')); mymap.insert(MYMAP::value_type(5,'k')); // 不能使用这种形式:mymap[3] = 'a'; for(int i = 0; i < 8; i ++)  mymap.insert(MYMAP::value_type(i,'h'+i));  MYMAP::iterator iter; cout<<"mymap as followed:"<<endl; for(iter = mymap.begin(); iter != mymap.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl;  cout<<endl; cout<<"after erase():"<<endl; mymap.erase(mymap.begin()); for(iter = mymap.begin(); iter != mymap.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl;  cout<<endl; cout<<"find key 4:"<<endl; if((iter = mymap.find(4))!=mymap.end())  cout<<"I find it ,value is:"<<iter->second<<endl; else  cout<<"Can't find it"<<endl;  MYMAP mymap2(mymap.begin(),mymap.end());  cout<<"mymay2 is :"<<endl; for(iter = mymap2.begin(); iter != mymap2.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl; if(mymap > mymap2)    // 比较这里,比较相同键值的元素  cout<<"mymap > mymap2"<<endl; else if(mymap == mymap2)  cout<<"mymap = mymap2"<<endl; else  cout<<"mymap < mymap2"<<endl;  return 0;} // 19,STLmultiset#include <iostream>#include <set>using namespace std; int main(){   multiset<int> mymultiset;  mymultiset.insert(20); mymultiset.insert(24); mymultiset.insert(24); mymultiset.insert(24); mymultiset.insert(24); mymultiset.insert(24); mymultiset.insert(2); mymultiset.insert(111); mymultiset.insert(88); mymultiset.insert(26); mymultiset.insert(mymultiset.begin(),100); cout<<"The multiset's size is:"<<mymultiset.size()<<endl; multiset<int>::iterator iter; for(iter = mymultiset.begin(); iter != mymultiset.end(); iter ++)  cout<<*iter<<" "; cout<<endl;  mymultiset.erase(++mymultiset.begin()); cout<<"after erase():"<<endl; for(iter = mymultiset.begin(); iter != mymultiset.end(); iter ++)  cout<<*iter<<" "; cout<<endl;  cout<<"find(88):"<<endl; if((iter = mymultiset.find(88)) != mymultiset.end())  cout<<"I find it "<<"next is:"<<*(++iter)<<endl; else  cout<<"I find not !"<<endl; cout<<"upper_bound(88):"<<*mymultiset.upper_bound(88)  <<" lower_bound(88):"<<*mymultiset.lower_bound(88) //这两个老是不对劲???  <<endl; cout<<"count(24):"<<mymultiset.count(24)<<endl; return 0;} // 20 , STLpriority_queue#include <iostream>#include <list>#include <queue>using namespace std; int main(){   priority_queue<int,vector<int>,greater<int>> mypq;  mypq.push(20); mypq.push(24); mypq.push(2); mypq.push(111); mypq.push(88); mypq.push(26); cout<<"The priority queue's size is:"<<mypq.size()<<endl; while(!mypq.empty()){  cout<<mypq.top()<<" ";  mypq.pop(); } cout<<endl; return 0;} // 21,STL queue#include <iostream>#include <list>#include <queue>using namespace std; int main(){   queue<int,list<int>> myqueue;  for(int i = 0; i < 20; i ++)  myqueue.push(i); cout<<myqueue.back()<<endl; while(!myqueue.empty()){  cout<<myqueue.front()<<" ";  myqueue.pop(); } cout<<endl; return 0;} // 22,STL set#include <iostream>#include <set>using namespace std; int main(){   set<int> myset;  myset.insert(20); myset.insert(24); myset.insert(2); myset.insert(111); myset.insert(88); myset.insert(26); cout<<"The set's size is:"<<myset.size()<<endl; set<int>::iterator iter; for(iter = myset.begin(); iter != myset.end(); iter ++)  cout<<*iter<<" "; cout<<endl;  cout<<"after erase():"<<endl; myset.erase(++myset.begin()); for(iter = myset.begin(); iter != myset.end(); iter ++)  cout<<*iter<<" "; cout<<endl;  cout<<"find(88):"<<endl; if((iter = myset.find(88)) != myset.end())  cout<<"I find it !"<<endl; else  cout<<"I find not !"<<endl; cout<<"upper_bound(88):"<<*myset.upper_bound(88)     <<" lower_bound(88):"<<*myset.lower_bound(88)<<endl;   //不对哦这里: 111 88 应该是 111 26 吧 ???    return 0;} // 23,STL stack#include <iostream>#include <list>#include <stack>using namespace std; int main(){   stack<int,list<int>> mystack;  for(int i = 0; i < 20; i ++)  mystack.push(i); while(!mystack.empty()){  cout<<mystack.top()<<" ";  mystack.pop(); }  return 0;} // 24 ,STL vector#include <iostream>#include <vector>using namespace std; int main(){/* vector<int> myArray(10,22); vector<int>::iterator iter; int i = 0;  for(iter = myArray.begin(); iter != myArray.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;  vector<char> myArray; vector<char>::iterator iter; int i = 0; for(i = 0; i < 6; i ++)  myArray.push_back('a'+ i);  for(i = 0,iter = myArray.begin(); iter != myArray.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl; vector<char> my; for(i = 0; i < 6; i ++)  my.push_back('A'+ i);  for(i = 0,iter = my.begin(); iter != my.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;   vector<int> myArray; vector<int>::iterator iter; int i = 0; for(i = 0; i < 6; i ++)  myArray.push_back(i); iter = myArray.begin(); iter ++; myArray.insert(iter++,10,100); for(i = 0,iter = myArray.begin(); iter != myArray.end(); iter ++)  cout<<"element#"<<i++<<":"<<*iter<<endl;  vector<int> myArray(2,10); vector<int>::iterator iter; int i = 0; for(i = 10; i < 16; i ++)  myArray.push_back(i);// ??? for(iter = myArray.begin(); iter != myArray.end(); iter ++) for(i = myArray.size(); i>= 0; i --){  vector<int>::iterator iterT;  for(i = myArray.size(),iterT = myArray.begin(); iterT != myArray.end(); iterT ++)   cout<<*iterT<<" ";  cout<<endl; // myArray.pop_back();  myArray.erase(myArray.begin()); }*/ vector<char> myArray1; vector<char> myArray2; vector<char>::iterator iter; int i = 0; for(i = 10; i < 16; i ++)  myArray1.push_back(i+'a'); for(i = myArray1.size(),iter = myArray1.begin(); iter != myArray1.end(); iter ++)   cout<<*iter<<" "; cout<<endl; for(i = 10; i < 16; i ++)  myArray2.push_back(i+'a'); for(i = myArray2.size(),iter = myArray2.begin(); iter != myArray2.end(); iter ++)   cout<<*iter<<" "; cout<<endl; if(myArray1 > myArray2)  cout<<"up bigger down"<<endl; else if(myArray1 < myArray2)  cout<<"up smaller down"<<endl; else  cout<<"up equal down"<<endl;    return 0;} // 25,特殊叠代器#include <iostream>#include <vector>#include <numeric>#include <algorithm>using namespace std; void show_val(int a){ cout<<a<<" ";} int main(){ vector<int> myvt;  cout<<"enter five integers:"; for(int i = 0;i < 5; i ++)  //输入流跌代器  myvt.push_back(*istream_iterator<int>(cin)); cout<<"use istream_iterator you enter is :"; for_each(myvt.begin(),myvt.end(),show_val); cout<<endl;  cout<<"use ostream_iterator partial_sum is :";  //输出流跌代器 partial_sum(myvt.begin(),myvt.end(),ostream_iterator<int>(cout)); cout<<endl;  //跌代器适配器 cout<<"use rbegin() and rend():"; for_each(myvt.rbegin(),myvt.rend(),show_val); cout<<endl;  //插入跌代器 对应容器中的 push_back,push_front 等 back_insert_iterator<vector<int>> binit(myvt); *binit++ = 1000; *binit++ = 10000; *binit = 100000; cout<<"use back_insert_iterator():"<<endl; for_each(myvt.begin(),myvt.end(),show_val); cout<<endl;  return 0;} // 自定义谓词#include <iostream>#include <map>using namespace std; class mycompare{public: bool operator()(const int a,const int b)const{  cout<<"compare:"<<a<<" "<<b<<endl;  return a>b; }}; int main(){   map<int,int,mycompare> mymap;  mymap.insert(map<int,int>::value_type(1,523)); mymap.insert(map<int,int>::value_type(2,23)); mymap.insert(map<int,int>::value_type(3,263));  mymap.insert(map<int,int>::value_type(8,23));  mymap[0] = 20; map<int,int,mycompare>::iterator iter; for(iter = mymap.begin(); iter != mymap.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl;  cout<<endl; cout<<"after erase():"<<endl; mymap.erase(++mymap.begin()); for(iter = mymap.begin(); iter != mymap.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl;  cout<<endl; cout<<"find key 7:"<<endl; if((iter = mymap.find(7))!=mymap.end())  cout<<"I find it ,value is:"<<iter->second<<endl; else  cout<<"Can't find it"<<endl;  map<int,int,mycompare> mymap2(mymap.begin(),mymap.end()); mymap2[2] = 100; cout<<"mymay2 is :"<<endl; for(iter = mymap2.begin(); iter != mymap2.end(); iter++)  cout<<iter->first   <<"->"<<iter->second<<endl; if(mymap > mymap2)  // 比较这里,比较相同键值的元素  cout<<"mymap > mymap2"<<endl; else if(mymap == mymap2)  cout<<"mymap = mymap2"<<endl; else  cout<<"mymap < mymap2"<<endl;  return 0;}  ///////////////////////////////// #include <vector>#include <algorithm>#include <iostream>using namespace std; #define OUTPUT(p) { cout<<#p<<":"<<p<<endl; } class Test{ public:    // 默认构造函数    Test():a(0){ cout<<"function:Test():a(0)"<<endl; }    // 带参数的普通构造函数    Test(int i):a(i){ cout<<"function:Test(int i):a(i)"<<endl; }     // 拷贝构造函数    Test(const Test& other):a(other.a){         cout<<"function:Test(const Test& other):a(other.a)"<<endl;     }    // 重载输出运算符函数    friend ostream& operator<<(ostream& out,const Test& t){        out<<t.a<<endl;        out<<"friend ostream& operator<<(ostream& out,const Test& t)"<<endl;        return out;    }    // 重载前缀自增运算符函数    Test& operator++(){ // prefix        cout<<"function:Test& operator++()"<<endl;        a++;        return *this;    }    // 重载后缀自增运算符函数    Test operator++(int){ //postfix        cout<<"function:Test operator++(int)"<<endl;        Test tp(*this);        a++;        return tp;    }    // 重载赋值运算符函数    Test& operator=(Test& other){        cout<<"function:Test& operator=(Test& other)"<<endl;        a = other.a;        return *this;    }    // 强制类型转换    operator int(void){ return a; }; private:    int a;}; int main(){    cout<<"Test first(23)"<<endl;    Test first(23);    OUTPUT(first);     cout<<"Test temp(first)"<<endl;    Test temp(first);    OUTPUT(temp);     cout<<"temp=++first"<<endl;    temp=++first;    OUTPUT(temp);    OUTPUT(first);     cout<<"Test equal"<<endl;    Test equal;    OUTPUT(equal);     cout<<"equal=first++"<<endl;    equal=first++;    OUTPUT(first);    OUTPUT(equal);     int t=-1;    t = (int)equal;    cout<<"int t=-1"<<endl        <<"t = (int)equal"<<endl        <<"t="<<t<<endl;     return 0;}       

阅读(7116) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册