Connecting Tech Pros Worldwide Help | Site Map

Problems with iterators ans lists

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:37 PM
jhonyxxx
Guest
 
Posts: n/a
Default Problems with iterators ans lists

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!!


misruga@public.ibercaja.es


  #2  
Old July 22nd, 2005, 04:38 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Problems with iterators ans lists


"jhonyxxx" <jhonye@arrakis.es> wrote in message
news:1vcnf05becceukndhnhf57jppcri9tc34m@4ax.com...[color=blue]
> 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!!
>
>
> misruga@public.ibercaja.es
>[/color]

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


  #3  
Old July 22nd, 2005, 04:38 PM
tom_usenet
Guest
 
Posts: n/a
Default Re: Problems with iterators ans lists

On Mon, 19 Jul 2004 13:49:47 +0200, jhonyxxx <jhonye@arrakis.es>
wrote:
[color=blue]
>I have the next programa in C++:[/color]

[snipped]
[color=blue]
>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?[/color]

Did you use list<persona>::iterator?
[color=blue]
>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!![/color]

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::ostream& operator<<(std::ostream& 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::ostream_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::ostream_iterator<persona>(std::cout, "--")
);
//always a good idea to finish output with a \n
std::cout << '\n';
}

Tom
  #4  
Old July 22nd, 2005, 04:38 PM
jhonyxxx
Guest
 
Posts: n/a
Default Re: Problems with iterators ans lists

On Mon, 19 Jul 2004 13:02:34 +0100, "John Harrison"
<john_andronicus@hotmail.com> wrote:
[color=blue]
>
>"jhonyxxx" <jhonye@arrakis.es> wrote in message
>news:1vcnf05becceukndhnhf57jppcri9tc34m@4ax.com.. .[color=green]
>> 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!!
>>
>>
>> misruga@public.ibercaja.es
>>[/color]
>
>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
>[/color]

My compiler is Borland c++ v5
  #5  
Old July 22nd, 2005, 04:38 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Problems with iterators ans lists

> >[color=blue]
>
> My compiler is Borland c++ v5[/color]

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

john


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.