1 #include <string> 2 #include <cctype> 3 #include <algorithm> 4 #include <iostream> 5 #include <list> 6 using namespace std; 7 class Base{ 8 public: 9 int data; 10 Base(int y):data(y){} 11 }; 12 Base *pt(int x){ 13 Base *tmp = new Base(x); 14 return tmp; 15 } 16 int main() 17 { 18 19 string str("ceshi andx 123 benjing"); 20 int x = 0; 21 char first[6] = {0}; 22 char last[16] = {0}; 23 char mid[16] = {0}; 24 int ret = sscanf(str.c_str(), "%s%s%d%s", first, mid, &x, last); 25 cout << ret << " first:" << first << " mid:" << mid << " x:" << x << " last:" << last << endl; 26 27 //int指针类型的list 28 list<int *> intptr; 29 int *tmp = new int[5]; 30 for(int i=0; i<5; i++){ 31 tmp[i] = i; 32 intptr.push_back(&tmp[i]); 33 } 34 list<int *>::iterator liter; 35 for(liter = intptr.begin(); liter != intptr.end(); ++liter) 36 cout << **liter << endl; 37 for (std::list<int*>::iterator it = intptr.begin(); it != intptr.end();it++) 38 { 39 //delete ⁢ //error 40 } 41 delete[] tmp; 42 43 //以下list对象指针均建立在堆上,需要new/delete结合申请与释放 44 //list元素为基本数据类型的对象指针 45 list<int> *ptr = new list<int>[2]; 46 ptr->push_back(2); 47 ptr->push_back(3); 48 list<int>::iterator iter; 49 for(iter = ptr->begin(); iter != ptr->end(); ++iter) 50 cout << *iter << endl; 51 delete[] ptr; 52 //list元素为自定义类型的对象指针 53 list<Base> *pBase = new list<Base>[5]; 54 Base ba(4); 55 Base bb(5); 56 pBase->push_back(ba); 57 pBase->push_back(bb); 58 list<Base>::iterator iterb; 59 for(iterb = pBase->begin(); iterb != pBase->end(); ++iterb) 60 cout << (*iterb).data << endl; 61 delete[] pBase; 62 63 }