C++ STL list 中对结构体的remove方法的使用
代码如下:程序编译报错,请高手解决!#include<iostream>#include<list>//使用list队列添加的头文件usingnamespacestd;/...
代码如下:程序编译报错,请高手解决!
#include <iostream>
#include <list> //使用list队列添加的头文件
using namespace std; //使用命名空间std
struct student{
char* name; //姓名
int age; //年龄
char* city; //城市
char* tel; //电话
};
int main()
{
student s[] = {
{"Xu",10,"Beijing","88888888"},
{"Zhang",12,"Shanghai","22222222"},
{"He",15,"Guangzhou","11111111"}
};
// 装入链表数据
list<student> l;
l.push_back(s[0]);
l.push_back(s[1]);
l.push_back(s[2]);
student li = {"Li",15,"Guangzhou","11111111"};
//元素头部插入
l.push_front(li); // 头部插入元素Li,15,Guangzhou,11111111
//元素尾部插入
student lend = {"Wang",18,"Wuhan","33333333"};
l.push_back(lend);
// remove的使用,这里不知道该如何填写,例如我要删除:"Wang",18,"Wuhan","33333333这条
//该如何写remvoe方法呢?
// 请高手在这里加上代码
//遍历打印链表
list<student>::iterator i,iend;
iend = l.end();
cout<<"姓名 年龄 城市 电话"<<endl;
cout<<"--------------------------------"<<endl;
for(i=l.begin();i!=iend;i++){
cout<<(*i).name<<" ";
cout<<(*i).age<<" ";
cout<<(*i).city<<" ";
cout<<(*i).tel<<" "<<endl;
}
cout<<"--------------------------------"<<endl;
return 0;
} 展开
#include <iostream>
#include <list> //使用list队列添加的头文件
using namespace std; //使用命名空间std
struct student{
char* name; //姓名
int age; //年龄
char* city; //城市
char* tel; //电话
};
int main()
{
student s[] = {
{"Xu",10,"Beijing","88888888"},
{"Zhang",12,"Shanghai","22222222"},
{"He",15,"Guangzhou","11111111"}
};
// 装入链表数据
list<student> l;
l.push_back(s[0]);
l.push_back(s[1]);
l.push_back(s[2]);
student li = {"Li",15,"Guangzhou","11111111"};
//元素头部插入
l.push_front(li); // 头部插入元素Li,15,Guangzhou,11111111
//元素尾部插入
student lend = {"Wang",18,"Wuhan","33333333"};
l.push_back(lend);
// remove的使用,这里不知道该如何填写,例如我要删除:"Wang",18,"Wuhan","33333333这条
//该如何写remvoe方法呢?
// 请高手在这里加上代码
//遍历打印链表
list<student>::iterator i,iend;
iend = l.end();
cout<<"姓名 年龄 城市 电话"<<endl;
cout<<"--------------------------------"<<endl;
for(i=l.begin();i!=iend;i++){
cout<<(*i).name<<" ";
cout<<(*i).age<<" ";
cout<<(*i).city<<" ";
cout<<(*i).tel<<" "<<endl;
}
cout<<"--------------------------------"<<endl;
return 0;
} 展开
3个回答
展开全部
给:
#include <iostream>
#include <list> //使用list队列添加的头文件
using namespace std; //使用命名空间std
struct student{
char* name; //姓名
int age; //年龄
char* city; //城市
char* tel; //电话
};
int main()
{
student s[] = {
{"Xu",10,"Beijing","88888888"},
{"Zhang",12,"Shanghai","22222222"},
{"He",15,"Guangzhou","11111111"}
};
// 装入链表数据
list<student> l;
l.push_back(s[0]);
l.push_back(s[1]);
l.push_back(s[2]);
student li = {"Li",15,"Guangzhou","11111111"};
//元素头部插入
l.push_front(li); // 头部插入元素Li,15,Guangzhou,11111111
//元素尾部插入
student lend = {"Wang",18,"Wuhan","33333333"};
l.push_back(lend);
//加的代码******************************************
list<student>::iterator iter;
for( iter=l.begin();iter!=l.end();++iter)
{
if( !strcmp(iter->name,"Wang"))
iter = l.erase(iter);
}
//遍历打印链表
list<student>::iterator i,iend;
iend = l.end();
cout<<"姓名 年龄 城市 电话"<<endl;
cout<<"--------------------------------"<<endl;
for(i=l.begin();i!=iend;i++){
cout<<(*i).name<<" ";
cout<<(*i).age<<" ";
cout<<(*i).city<<" ";
cout<<(*i).tel<<" "<<endl;
}
cout<<"--------------------------------"<<endl;
return 0;
}
展开全部
C/C++ code
// remove_if example使用list队列添加的头文件
#include <iostream>
#include <algorithm>
using namespace std;
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^
pend = remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ?
// ^ ^
cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
cout << " " << *p;
cout << endl;
return 0;
}
// remove_if example使用list队列添加的头文件
#include <iostream>
#include <algorithm>
using namespace std;
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^
pend = remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ?
// ^ ^
cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
cout << " " << *p;
cout << endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C/C++ code
// remove_if example
#include <iostream>
#include <algorithm>
using namespace std;
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^
pend = remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ?
// ^ ^
cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
cout << " " << *p;
cout << endl;
return 0;
}
// remove_if example
#include <iostream>
#include <algorithm>
using namespace std;
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^
pend = remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ?
// ^ ^
cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
cout << " " << *p;
cout << endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询