#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <list> //使用list队列添加的头文件
using namespace std;
struct person{
int age;
char *name;
person(int _age,char *_name):age(_age),name(_name){}
bool operator==(const person&); //用remove要重载==,除非是内置类型
};
bool person::operator==(const person& person){
if(!strcmp(name,person.name))//字符串相等判断,不过不如直接用string成员更简单
if(age==person.age)
return true;
return false;
}
void printlist(person p)
{
cout<<p.age<<" "<<p.name<<endl;
}
void initperson(){
person * p1,*p2,*p3;
p1 = (person*)malloc(sizeof(person));
p2 = (person*)malloc(sizeof(person));
p3 = (person*)malloc(sizeof(person));
p1->age = 100;
p1->name = (char*)malloc(10);
memset(p1->name,0,10);
memcpy(p1->name,"zhang",5);
p2->age = 200;
p2->name = (char*)malloc(10);
memset(p2->name,0,10);
memcpy(p2->name,"zhang",5);
p3->age = 300;
p3->name = (char*)malloc(10);
memset(p3->name,0,10);
memcpy(p3->name,"zhang",5);
list<person> listTWO;
listTWO.push_back(*p1);
listTWO.push_back(*p2);
listTWO.push_back(*p3);
for_each(listTWO.begin(),listTWO.end(),printlist);
for (list<person>::iterator it = listTWO.begin();it != listTWO.end();it++)
{
if (200 == it->age)
{
listTWO.remove(*it);
break;
}
}
listTWO.remove(person(100,"zhang"));
cout<<"after remove"<<endl;
for_each(listTWO.begin(),listTWO.end(),printlist);
}
void fun_01()
{
person p[] ={
person(10,"zhang"),
person(20,"liu"),
person(30,"han")
};
list<person> listONE(p,p+3);
// person * temp = (person*)malloc(sizeof(person));
for_each(listONE.begin(),listONE.end(),printlist);
for (list<person>::iterator it = listONE.begin();it != listONE.end();it++)
{
if (10 == it->age)
{
// *temp= *it;
listONE.remove(*it);
break;
}
}
// listONE.remove(person(10,"zhang"));
printf("\nafter remove\n");
for_each(listONE.begin(),listONE.end(),printlist);
}
struct student11{
char* name; //姓名
int age; //年龄
char* city; //城市
char* tel; //电话
student11(char * name,int age,char * city,char* tel): //<a href="https://www.baidu.com/s?wd=%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YvuW63n1D1mHNWryRkmhPW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRYrH0LP1R1rjf1PjTznHcd" target="_blank" class="baidu-highlight">构造函数</a>
name(name),age(age),city(city),tel(tel){}
bool operator==(const student11&); //用remove要重载==,除非是内置类型
};
bool student11::operator==(const student11& stu){
if(!strcmp(name,stu.name))//字符串相等判断,不过不如直接用string成员更简单
if(age==stu.age)
if(!strcmp(city,stu.city))
if(!strcmp(tel,stu.tel))
return true;
return false;
}
void print(student11 stu){
cout<<left<<setw(8)<<stu.name;//格式化输出打印的好看点...
cout<<left<<setw(5)<<stu.age;
cout<<left<<setw(12)<<stu.city;
cout<<left<<setw(10)<<stu.tel<<endl;
}
void fun_sut()
{
student11 s[] = {
student11("Xu",100,"shenzhen","12344"),
student11("Zhang",200,"chengdu","4555"),
student11("He",300,"beijing","123232")
};
list<student11> stuList(s,s+3);//用数组初始化
cout<<"姓名 年龄 城市 电话"<<endl;
cout<<"---------------------------------"<<endl;
for_each(stuList.begin(),stuList.end(),print);//打印
cout<<"---------------------------------"<<endl;
stuList.remove(student11("Zhang",200,"chengdu","4555"));
cout<<"姓名 年龄 城市 电话"<<endl;
cout<<"---------------------------------"<<endl;
for_each(stuList.begin(),stuList.end(),print);//打印
cout<<"---------------------------------"<<endl;
}
void main()
{
// fun_01();
// fun_sut();
cout<<"\n"<<endl;
initperson();
}
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <list> //使用list队列添加的头文件
using namespace std;
struct person{
int age;
char *name;
person(int _age,char *_name):age(_age),name(_name){}
bool operator==(const person&); //用remove要重载==,除非是内置类型
};
bool person::operator==(const person& person){
if(!strcmp(name,person.name))//字符串相等判断,不过不如直接用string成员更简单
if(age==person.age)
return true;
return false;
}
void printlist(person p)
{
cout<<p.age<<" "<<p.name<<endl;
}
void initperson(){
person * p1,*p2,*p3;
p1 = (person*)malloc(sizeof(person));
p2 = (person*)malloc(sizeof(person));
p3 = (person*)malloc(sizeof(person));
p1->age = 100;
p1->name = (char*)malloc(10);
memset(p1->name,0,10);
memcpy(p1->name,"zhang",5);
p2->age = 200;
p2->name = (char*)malloc(10);
memset(p2->name,0,10);
memcpy(p2->name,"zhang",5);
p3->age = 300;
p3->name = (char*)malloc(10);
memset(p3->name,0,10);
memcpy(p3->name,"zhang",5);
list<person> listTWO;
listTWO.push_back(*p1);
listTWO.push_back(*p2);
listTWO.push_back(*p3);
for_each(listTWO.begin(),listTWO.end(),printlist);
for (list<person>::iterator it = listTWO.begin();it != listTWO.end();it++)
{
if (200 == it->age)
{
listTWO.remove(*it);
break;
}
}
listTWO.remove(person(100,"zhang"));
cout<<"after remove"<<endl;
for_each(listTWO.begin(),listTWO.end(),printlist);
}
void fun_01()
{
person p[] ={
person(10,"zhang"),
person(20,"liu"),
person(30,"han")
};
list<person> listONE(p,p+3);
// person * temp = (person*)malloc(sizeof(person));
for_each(listONE.begin(),listONE.end(),printlist);
for (list<person>::iterator it = listONE.begin();it != listONE.end();it++)
{
if (10 == it->age)
{
// *temp= *it;
listONE.remove(*it);
break;
}
}
// listONE.remove(person(10,"zhang"));
printf("\nafter remove\n");
for_each(listONE.begin(),listONE.end(),printlist);
}
struct student11{
char* name; //姓名
int age; //年龄
char* city; //城市
char* tel; //电话
student11(char * name,int age,char * city,char* tel): //<a href="https://www.baidu.com/s?wd=%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YvuW63n1D1mHNWryRkmhPW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRYrH0LP1R1rjf1PjTznHcd" target="_blank" class="baidu-highlight">构造函数</a>
name(name),age(age),city(city),tel(tel){}
bool operator==(const student11&); //用remove要重载==,除非是内置类型
};
bool student11::operator==(const student11& stu){
if(!strcmp(name,stu.name))//字符串相等判断,不过不如直接用string成员更简单
if(age==stu.age)
if(!strcmp(city,stu.city))
if(!strcmp(tel,stu.tel))
return true;
return false;
}
void print(student11 stu){
cout<<left<<setw(8)<<stu.name;//格式化输出打印的好看点...
cout<<left<<setw(5)<<stu.age;
cout<<left<<setw(12)<<stu.city;
cout<<left<<setw(10)<<stu.tel<<endl;
}
void fun_sut()
{
student11 s[] = {
student11("Xu",100,"shenzhen","12344"),
student11("Zhang",200,"chengdu","4555"),
student11("He",300,"beijing","123232")
};
list<student11> stuList(s,s+3);//用数组初始化
cout<<"姓名 年龄 城市 电话"<<endl;
cout<<"---------------------------------"<<endl;
for_each(stuList.begin(),stuList.end(),print);//打印
cout<<"---------------------------------"<<endl;
stuList.remove(student11("Zhang",200,"chengdu","4555"));
cout<<"姓名 年龄 城市 电话"<<endl;
cout<<"---------------------------------"<<endl;
for_each(stuList.begin(),stuList.end(),print);//打印
cout<<"---------------------------------"<<endl;
}
void main()
{
// fun_01();
// fun_sut();
cout<<"\n"<<endl;
initperson();
}