关于STL List
如下程序,在VC6.0下可以编译通过,但是执行的时候就会出错,主为什么呢?#include<iostream>#include<list>typedefstruct{in...
如下程序,在VC6.0下可以编译通过,但是执行的时候就会出错,
主为什么呢?
#include<iostream>
#include<list>
typedef struct
{
int a;
char *b;
public:
bool operator>(const &node b)const
{
return a>b.a;
}
}node;
int main()
{
std::list<node> nodelist(3);
int x=0;
node n1;
n1.a=1;
strcpy(n1.b="China");
nodelist.push_back(n1);
node n2;
n2.a=2;
strcpy(n2.b="Tmenden");
nodelist.push_back(n2);
node n3;
n3.a=3;
strcpy(n3.b,"huaef");
nodelist.push_back(n3);
std::list<node>::iterator iter;
for(iter=nodelist.begin();iter!=nodelist.end();iter++)
{
std::cout<<iter->a<<":"<<iter->b<<std::endl;
}
return 0;
}
打错了,应该是这样。
#include<iostream>
#include <string>
#include<list>
typedef struct
{
int a;
char *b;
}node;
int main()
{
std::list<node> nodelist(3);
int x=0;
node n1;
n1.a=1;
strcpy(n1.b,"chen");
nodelist.push_back(n1);
node n2;
n2.a=2;
strcpy(n2.b,"yu");
nodelist.push_back(n2);
node n3;
n3.a=3;
strcpy(n3.b,"hua");
nodelist.push_back(n3);
std::list<node>::iterator iter;
for(iter=nodelist.begin();iter!=nodelist.end();iter++)
{
std::cout<<iter->a<<":"<<iter->b<<std::endl;
}
return 0;
} 展开
主为什么呢?
#include<iostream>
#include<list>
typedef struct
{
int a;
char *b;
public:
bool operator>(const &node b)const
{
return a>b.a;
}
}node;
int main()
{
std::list<node> nodelist(3);
int x=0;
node n1;
n1.a=1;
strcpy(n1.b="China");
nodelist.push_back(n1);
node n2;
n2.a=2;
strcpy(n2.b="Tmenden");
nodelist.push_back(n2);
node n3;
n3.a=3;
strcpy(n3.b,"huaef");
nodelist.push_back(n3);
std::list<node>::iterator iter;
for(iter=nodelist.begin();iter!=nodelist.end();iter++)
{
std::cout<<iter->a<<":"<<iter->b<<std::endl;
}
return 0;
}
打错了,应该是这样。
#include<iostream>
#include <string>
#include<list>
typedef struct
{
int a;
char *b;
}node;
int main()
{
std::list<node> nodelist(3);
int x=0;
node n1;
n1.a=1;
strcpy(n1.b,"chen");
nodelist.push_back(n1);
node n2;
n2.a=2;
strcpy(n2.b,"yu");
nodelist.push_back(n2);
node n3;
n3.a=3;
strcpy(n3.b,"hua");
nodelist.push_back(n3);
std::list<node>::iterator iter;
for(iter=nodelist.begin();iter!=nodelist.end();iter++)
{
std::cout<<iter->a<<":"<<iter->b<<std::endl;
}
return 0;
} 展开
1个回答
展开全部
编译就不行啊。
//之于楼主的错误,首先你基于C++编程,就要用c++提供的东西嘛,简单。
容器类很好用不是吗?
另外你都include srting了,还那么麻烦掉strcpy干嘛?直接=就可以了。
#include<iostream>
#include <string>
#include<list>
using namespace std;
typedef struct
{
int a;
string b;
}node;
int main()
{
std::list<node> nodelist(3);
int x=0;
node n1;
n1.a=1;
n1.b="chen";
nodelist.push_back(n1);
node n2;
n2.a=2;
n2.b="yu";
nodelist.push_back(n2);
node n3;
n3.a=3;
n3.b="hua";
nodelist.push_back(n3);
std::list<node>::iterator iter;
for(iter=nodelist.begin();iter!=nodelist.end();iter++)
{
cout<<iter->a<<":"<<iter->b<<endl;
}
return 0;
}
至于你出现的错误,不知道你有意还是无意,如果想去掉前面那三行东西可以这么改
list<node> nodelist;
建议看下容器类里面到底有哪些方法
//之于楼主的错误,首先你基于C++编程,就要用c++提供的东西嘛,简单。
容器类很好用不是吗?
另外你都include srting了,还那么麻烦掉strcpy干嘛?直接=就可以了。
#include<iostream>
#include <string>
#include<list>
using namespace std;
typedef struct
{
int a;
string b;
}node;
int main()
{
std::list<node> nodelist(3);
int x=0;
node n1;
n1.a=1;
n1.b="chen";
nodelist.push_back(n1);
node n2;
n2.a=2;
n2.b="yu";
nodelist.push_back(n2);
node n3;
n3.a=3;
n3.b="hua";
nodelist.push_back(n3);
std::list<node>::iterator iter;
for(iter=nodelist.begin();iter!=nodelist.end();iter++)
{
cout<<iter->a<<":"<<iter->b<<endl;
}
return 0;
}
至于你出现的错误,不知道你有意还是无意,如果想去掉前面那三行东西可以这么改
list<node> nodelist;
建议看下容器类里面到底有哪些方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询