C ++ test the original list (push_back //// remove), code via

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u012748494/article/details/50511809
#include <the iostream>
#include <The iomanip>
#include <algorithm>
#include <CString>
#include <list> // Use the queue header list to add
the using namespace STD;
 struct {Person
int age;
char *name;
person(int _age,char *_name):age(_age),name(_name){}
bool operator == (const person &) ; // used to override == remove, unless the built-in type
};
 BOOL Person :: operator == (const & Person Person) {
    IF (! strcmp (name, PERSON.NAME)) // string equality test, but not as good as with a string member directly simpler
        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; // Name
    int age; // Age
    char * city; // city
    char * tel; // phone
    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 "> constructor </a>
        name (name), Age (Age), City (City), Tel (Tel)} {
    BOOL operator == (const & student11); // used to override == remove, unless the built-in type
};
  
student11 :: operator == BOOL (const student11 & STU) {
    IF (! strcmp (name, stu.name)) // string equality test, but not as good as with a string member directly simpler
        IF (Age == stu.age)
            IF (! strcmp (City, stu.city))
                IF (strcmp (Tel, stu.tel)!)
                    return to true;
    return to false;
}
  
void Print (student11 STU) {
    COUT left << << setw (. 8) STU << .name; // nice point formatted printed output ...
    COUT left << << setw (. 5) << stu.age;
    COUT left << << setw (12 is) << stu.city;
    COUT << << setw left (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 << "name, age, city phone" << endl;
    cout << "--------------------------------- "<< endl;
    for_each (stuList.begin (), stuList.end (), Print); // print
    cout <<" --------------------- ------------ "<< endl;


    stuList.remove (student11 (" Zhang ", 200," chengdu "," 4555 "));
     
    cout <<" name, age, city phone "<< endl;
    cout << "---------------------------------" << endl;
    for_each (stuList.begin ( ), stuList.end (), print) ; // print
    cout << "-------------------------------- - "<< endl;
}
void main ()
{
// fun_01 ();
// fun_sut ();
cout<<"\n"<<endl;
initperson();
}



Guess you like

Origin blog.csdn.net/u012748494/article/details/50511809