J
John
Hi all:
I write the following code about vector and list. Why the result is
not correct?
Thanks.
John
--------------------------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
typedef struct{
int ID;
double time;
}node;
void myvector(vector<node*> &v0){
node n1, n2;
n1.ID = 1;
n1.time = 1.1;
n2.ID = 2;
n2.time = 2.2;
v0.push_back(&n1);
v0.push_back(&n2);
cout<<"v0:"<<v0[0]<<endl;
cout<<"ID1:"<< (*v0.begin())->ID <<" time1:"<<v0[0]->time <<endl;
cout<<"ID2:"<< v0[1]->ID <<" time2:"<<v0[1]->time <<endl;
}
list< node* > mylist()
{
list< node* > l;
node n1, n2;
n1.ID = 5;
n1.time = 5.1;
n2.ID = 6;
n2.time = 6.2;
l.push_back(&n1);
l.push_back(&n2);
list<node*>::iterator pos;
pos = l.begin();
cout<<"pos ID1:"<<(*pos)->ID<<" time1:"<<(*pos)->time<<endl;
pos++;
cout<<"pos ID2:"<<(*pos)->ID<<" time2:"<<(*pos)->time<<endl;
return l;
}
int main()
{
vector<node*> v1;
myvector(v1);
if(!v1.empty()){
cout<<"v1:"<<v1[0]<<endl;
cout<<"ID1:"<< (v1.front())->ID <<" time1:"<<v1[0]->time <<endl;
}
if(!v1.empty()){
cout<<"v1_1:"<<endl;
cout<<"ID2:"<< v1[1]->ID <<" time2:"<<v1[1]->time <<endl;
}
list< node* > l1;
l1 = mylist();
cout<<"l1:"<<endl;
list<node*>::iterator pos;
pos = l1.begin();
cout<<"pos ID1:"<<(*pos)->ID<<" time1:"<<(*pos)->time<<endl;
pos++;
cout<<"pos ID2:"<<(*pos)->ID<<" time2:"<<(*pos)->time<<endl;
}
------------------------------
The output is:
v0:0xbffffc3c
ID1:1 time1:1.1
ID2:2 time2:2.2
v1:0xbffffc3c
ID1:1074040948 time1:4.87143e-270
v1_1:
ID2:134518728 time2:6.64672e-316
pos ID1:5 time1:5.1
pos ID2:6 time2:6.2
l1:
pos ID1:134531200 time1:4.87361e-270
pos ID2:32 time2:6.40993
The result is correct in function myvector() and mylist(). But after
the arguements are past back to main(), the result is wrong. What is
the problem?
Thanks a lot.
John
I write the following code about vector and list. Why the result is
not correct?
Thanks.
John
--------------------------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
typedef struct{
int ID;
double time;
}node;
void myvector(vector<node*> &v0){
node n1, n2;
n1.ID = 1;
n1.time = 1.1;
n2.ID = 2;
n2.time = 2.2;
v0.push_back(&n1);
v0.push_back(&n2);
cout<<"v0:"<<v0[0]<<endl;
cout<<"ID1:"<< (*v0.begin())->ID <<" time1:"<<v0[0]->time <<endl;
cout<<"ID2:"<< v0[1]->ID <<" time2:"<<v0[1]->time <<endl;
}
list< node* > mylist()
{
list< node* > l;
node n1, n2;
n1.ID = 5;
n1.time = 5.1;
n2.ID = 6;
n2.time = 6.2;
l.push_back(&n1);
l.push_back(&n2);
list<node*>::iterator pos;
pos = l.begin();
cout<<"pos ID1:"<<(*pos)->ID<<" time1:"<<(*pos)->time<<endl;
pos++;
cout<<"pos ID2:"<<(*pos)->ID<<" time2:"<<(*pos)->time<<endl;
return l;
}
int main()
{
vector<node*> v1;
myvector(v1);
if(!v1.empty()){
cout<<"v1:"<<v1[0]<<endl;
cout<<"ID1:"<< (v1.front())->ID <<" time1:"<<v1[0]->time <<endl;
}
if(!v1.empty()){
cout<<"v1_1:"<<endl;
cout<<"ID2:"<< v1[1]->ID <<" time2:"<<v1[1]->time <<endl;
}
list< node* > l1;
l1 = mylist();
cout<<"l1:"<<endl;
list<node*>::iterator pos;
pos = l1.begin();
cout<<"pos ID1:"<<(*pos)->ID<<" time1:"<<(*pos)->time<<endl;
pos++;
cout<<"pos ID2:"<<(*pos)->ID<<" time2:"<<(*pos)->time<<endl;
}
------------------------------
The output is:
v0:0xbffffc3c
ID1:1 time1:1.1
ID2:2 time2:2.2
v1:0xbffffc3c
ID1:1074040948 time1:4.87143e-270
v1_1:
ID2:134518728 time2:6.64672e-316
pos ID1:5 time1:5.1
pos ID2:6 time2:6.2
l1:
pos ID1:134531200 time1:4.87361e-270
pos ID2:32 time2:6.40993
The result is correct in function myvector() and mylist(). But after
the arguements are past back to main(), the result is wrong. What is
the problem?
Thanks a lot.
John