list对象指针与指针类型list

时间:2021-03-25 16:56:25
 #include <string>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
class Base{
public:
int data;
Base(int y):data(y){}
};
Base *pt(int x){
Base *tmp = new Base(x);
return tmp;
}
int main()
{ string str("ceshi andx 123 benjing");
int x = 0;
char first[6] = {0};
char last[16] = {0};
char mid[16] = {0};
int ret = sscanf(str.c_str(), "%s%s%d%s", first, mid, &x, last);
cout << ret << " first:" << first << " mid:" << mid << " x:" << x << " last:" << last << endl; //int指针类型的list
list<int *> intptr;
int *tmp = new int[];
for(int i=; i<; i++){
tmp[i] = i;
intptr.push_back(&tmp[i]);
}
list<int *>::iterator liter;
for(liter = intptr.begin(); liter != intptr.end(); ++liter)
cout << **liter << endl;
for (std::list<int*>::iterator it = intptr.begin(); it != intptr.end();it++)
{
//delete &it; //error
}
delete[] tmp; //以下list对象指针均建立在堆上,需要new/delete结合申请与释放
//list元素为基本数据类型的对象指针
list<int> *ptr = new list<int>[2];
ptr->push_back(2);
ptr->push_back(3);
list<int>::iterator iter;
for(iter = ptr->begin(); iter != ptr->end(); ++iter)
cout << *iter << endl;
delete[] ptr;
//list元素为自定义类型的对象指针
list<Base> *pBase = new list<Base>[5];
Base ba(4);
Base bb(5);
pBase->push_back(ba);
pBase->push_back(bb);
list<Base>::iterator iterb;
for(iterb = pBase->begin(); iterb != pBase->end(); ++iterb)
cout << (*iterb).data << endl;
delete[] pBase; }