Problems with iterators ans lists

J

jhonyxxx

I have the next programa in C++:

#include <iostream.h> // C++ I/O routines
#include <list.h> // The STL list class
#include<stdio.h>
#include <string.h>
typedef struct
{
char nombre[30];
int edad;
} persona;
using namespace std;
int main () {
randomize();
// Create a new, empty linked list of int.
list<int> x;
int t;

//for (t=1;t<=10;t++)
// x.push_front(t);
for (t=1;t<=100;t++)
{
if (t%2==0)
x.push_back(random(5000)+1);
else
x.push_front(random(5000)+1);

//x.sort();
}
// Show all of the items now in the list.
list<int>::iterator i;

for (i = x.begin(); i != x.end(); i++) {
cout << *i<<"--";

}
printf("\n\n\n");

printf("%d\n",x.size());
printf("%d\n",x.max_size());

x.sort();
printf("\n\n\n");
for (i = x.begin(); i != x.end(); i++) {
cout <<*i<<"--";
}

}



If I use with a single list of int ist´s ok but if the list containing
list<person> and I try generate the items with
persona ficha;
strcpy(ficha.nombre,"hola");
ficha.edad=40;
and then I try to see with



for (i = x.begin(); i != x.end(); i++) {
cout << i->nombre<<"--";
cout <<i->edad<<"--";
}

the programa break down and tell to me that exists errors for
compiling in the cout sentences, the exact message is :

pointer to structure requeired on left side -> or ->*

I have change al type of opertors over the iterators and I don´t
solve the problem, what is wrong?
Seems that I use list with atomic elements int, float char etc. all is
ok but if the list is wit struct the iterator seems turn fool.
help, me please thank you!!


(e-mail address removed)
 
J

John Harrison

jhonyxxx said:
I have the next programa in C++:

#include <iostream.h> // C++ I/O routines
#include <list.h> // The STL list class
#include<stdio.h>
#include <string.h>
typedef struct
{
char nombre[30];
int edad;
} persona;
using namespace std;
int main () {
randomize();
// Create a new, empty linked list of int.
list<int> x;
int t;

//for (t=1;t<=10;t++)
// x.push_front(t);
for (t=1;t<=100;t++)
{
if (t%2==0)
x.push_back(random(5000)+1);
else
x.push_front(random(5000)+1);

//x.sort();
}
// Show all of the items now in the list.
list<int>::iterator i;

for (i = x.begin(); i != x.end(); i++) {
cout << *i<<"--";

}
printf("\n\n\n");

printf("%d\n",x.size());
printf("%d\n",x.max_size());

x.sort();
printf("\n\n\n");
for (i = x.begin(); i != x.end(); i++) {
cout <<*i<<"--";
}

}



If I use with a single list of int ist´s ok but if the list containing
list<person> and I try generate the items with
persona ficha;
strcpy(ficha.nombre,"hola");
ficha.edad=40;
and then I try to see with



for (i = x.begin(); i != x.end(); i++) {
cout << i->nombre<<"--";
cout <<i->edad<<"--";
}

the programa break down and tell to me that exists errors for
compiling in the cout sentences, the exact message is :

pointer to structure requeired on left side -> or ->*

I have change al type of opertors over the iterators and I don´t
solve the problem, what is wrong?
Seems that I use list with atomic elements int, float char etc. all is
ok but if the list is wit struct the iterator seems turn fool.
help, me please thank you!!


(e-mail address removed)

Are you sure that you changed this

list<int>::iterator i;

to this?

list<person>::iterator i;


If you did then maybe you are using a very old version of the STL. What you
have written should work.

If it doesn't work then try this

for (i = x.begin(); i != x.end(); ++i) {
cout << (*i).nombre<<"--";
cout << (*i).edad<<"--";
}

But a better solution would be to get a more modern version of the STL. For
instance have a look at www.stlport.org for a free version of the STL that
works with many different compilers. Which compiler are you using at the
moment?

john
 
T

tom_usenet

I have the next programa in C++:
[snipped]

If I use with a single list of int ist´s ok but if the list containing
list<person> and I try generate the items with
persona ficha;
strcpy(ficha.nombre,"hola");
ficha.edad=40;
and then I try to see with

for (i = x.begin(); i != x.end(); i++) {
cout << i->nombre<<"--";
cout <<i->edad<<"--";
}

the programa break down and tell to me that exists errors for
compiling in the cout sentences, the exact message is :

pointer to structure requeired on left side -> or ->*

I have change al type of opertors over the iterators and I don´t
solve the problem, what is wrong?

Did you use list said:
Seems that I use list with atomic elements int, float char etc. all is
ok but if the list is wit struct the iterator seems turn fool.
help, me please thank you!!

There are a lot of problems and errors in your code, here's a
corrected version:

#include <iostream> //no .h
#include <list> //no .h
#include <cstring> //c++ version
//cstdio not required
#include <ctime> //for time
#include <cstdlib> //for random functions

//"random" isn't a standard function. Here's a definition:
inline int random(int max)
{
return static_cast<int>(
static_cast<double>(std::rand()) * max / (RAND_MAX+1)
);
}

//typedef struct is not necessary in C++, it is a C "hack".
struct persona
{
char nombre[30];
int edad;
};

//to sort the list, need to define a < function.
bool operator<(persona const& lhs, persona const& rhs)
{
return strcmp(lhs.nombre, rhs.nombre) < 0;
}

int main ()
{
std::srand(static_cast<unsigned>(std::time(0)));
// Create a new, empty linked list of int.
std::list<persona> x;
// some random names to fill the list with.
char const* names[5] = {"hola", "hello", "foo", "bar", "baz"};
for (int t=1;t<=100;t++)
{
persona ficha;
std::strcpy(ficha.nombre,names[random(5)]);
ficha.edad=40;
x.push_back(ficha);
}
// Show all of the items now in the list.
std::list<persona>::iterator i;


for (i = x.begin(); i != x.end(); i++)
{
std::cout << i->nombre<<"--";
std::cout <<i->edad<<"--";
}
//no need to use printf
//mixing printf and cout can be dangerous (see
//std::ios_base::sync_with_stdio).
std::cout << "\n\n\n";

std::cout << x.size() << '\n';
std::cout << x.max_size() << '\n';
std::cout << "\n\n\n";

x.sort();
for (i = x.begin(); i != x.end(); i++)
{
std::cout << i->nombre<<"--";
std::cout <<i->edad<<"--";
}
//always a good idea to finish output with a \n
std::cout << '\n';
}

And here's a version using some improved C++ facilities:

#include <iostream> //no .h
#include <list> //no .h
#include <string> //c++ version
#include <algorithm>
#include <iterator>
//cstdio not required
#include <ctime> //for time
#include <cstdlib> //for random functions

//"random" isn't a standard function. Here's a definition:
inline int random(int max)
{
return static_cast<int>(
static_cast<double>(std::rand()) * max / (RAND_MAX +
1)
);
}

//typedef struct is not necessary in C++, it is a C "hack".
struct persona
{
//added a constructor to initialize the persona
persona(std::string nombre, int edad)
:nombre(nombre), edad(edad)
{
}
//crucially nombre can now be any length.
std::string nombre;
int edad;
};

//to sort the list, need to define a < function.
bool operator<(persona const& lhs, persona const& rhs)
{
return lhs.nombre < rhs.nombre;
}

//add output streaming support to persona.
std::eek:stream& operator<<(std::eek:stream& os, persona const& p)
{
os << '(' << p.nombre << ", ";
os << p.edad << ')';
return os;
}

//define a random person generator.
struct persona_gen
{
static char const* names[5];
persona operator()() const
{
return persona(names[random(5)], random(40));
}
};

char const* persona_gen::names[5] = {"hola", "hello", "foo", "bar",
"baz"};

int main ()
{
//seed the random number generator with the current time.
std::srand(static_cast<unsigned>(std::time(0)));

// Create a new, empty linked list of persona.
std::list<persona> x;
// add 100 random entries.
std::generate_n(std::back_inserter(x), 100, persona_gen());
// Show all of the items now in the list.
std::copy(
x.begin(),
x.end(),
std::eek:stream_iterator<persona>(std::cout, "--")
);

std::cout << "\n\n\n";

std::cout << x.size() << '\n';
std::cout << x.max_size() << '\n';
std::cout << "\n\n\n";

x.sort();
std::copy(
x.begin(),
x.end(),
std::eek:stream_iterator<persona>(std::cout, "--")
);
//always a good idea to finish output with a \n
std::cout << '\n';
}

Tom
 
J

jhonyxxx

jhonyxxx said:
I have the next programa in C++:

#include <iostream.h> // C++ I/O routines
#include <list.h> // The STL list class
#include<stdio.h>
#include <string.h>
typedef struct
{
char nombre[30];
int edad;
} persona;
using namespace std;
int main () {
randomize();
// Create a new, empty linked list of int.
list<int> x;
int t;

//for (t=1;t<=10;t++)
// x.push_front(t);
for (t=1;t<=100;t++)
{
if (t%2==0)
x.push_back(random(5000)+1);
else
x.push_front(random(5000)+1);

//x.sort();
}
// Show all of the items now in the list.
list<int>::iterator i;

for (i = x.begin(); i != x.end(); i++) {
cout << *i<<"--";

}
printf("\n\n\n");

printf("%d\n",x.size());
printf("%d\n",x.max_size());

x.sort();
printf("\n\n\n");
for (i = x.begin(); i != x.end(); i++) {
cout <<*i<<"--";
}

}



If I use with a single list of int ist´s ok but if the list containing
list<person> and I try generate the items with
persona ficha;
strcpy(ficha.nombre,"hola");
ficha.edad=40;
and then I try to see with



for (i = x.begin(); i != x.end(); i++) {
cout << i->nombre<<"--";
cout <<i->edad<<"--";
}

the programa break down and tell to me that exists errors for
compiling in the cout sentences, the exact message is :

pointer to structure requeired on left side -> or ->*

I have change al type of opertors over the iterators and I don´t
solve the problem, what is wrong?
Seems that I use list with atomic elements int, float char etc. all is
ok but if the list is wit struct the iterator seems turn fool.
help, me please thank you!!


(e-mail address removed)

Are you sure that you changed this

list<int>::iterator i;

to this?

list<person>::iterator i;


If you did then maybe you are using a very old version of the STL. What you
have written should work.

If it doesn't work then try this

for (i = x.begin(); i != x.end(); ++i) {
cout << (*i).nombre<<"--";
cout << (*i).edad<<"--";
}

But a better solution would be to get a more modern version of the STL. For
instance have a look at www.stlport.org for a free version of the STL that
works with many different compilers. Which compiler are you using at the
moment?

john

My compiler is Borland c++ v5
 
J

John Harrison

My compiler is Borland c++ v5

Sorry I don't know anything about that compiler, perhaps you could ask on a
Borland newsgroup, details on their website.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top