Thread: STL list and struct

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    STL list and struct

    My problem is this , Iwant make a list not of atomic elements, my list must be of agrgate data like a struct. With the container vector of the stl , the programa run okey with an iteratro eg. vitr i can access to dada memebers of struct like this

    typedef struct
    {
    string salario;
    int edad;
    } persona;
    ....
    .....
    vector<persona>::iterator vitr;
    for (vitr = v.begin(); vitr!=v.end(); vitr++)
    {
    cout << vitr->edad << "//";
    cout << vitr->salario << "**";
    }


    but in the case of list of structs thera ara many problems:

    example:

    #include <iostream> // C++ I/O routines
    #include <list> // The STL list class
    #include<stdio.h>
    #include <string>
    using namespace std;
    typedef struct MYSTRUCT
    {
    string valor2;
    int valor1;

    }dat;

    void inserta(list<MYSTRUCT>& );
    int main ()
    {
    list<dat> x;
    randomize();

    int t;

    for (t=1;t<=20;t++)
    {
    inserta(x);
    }
    // Show all of the items now in the list.
    list<dat>::iterator i;

    for (i = x.begin(); i != x.end(); i++)
    {
    cout <<i->valor1<<"--";// the compiler Borland c++ v5 mark error there
    cout << i->valor2 <<"--";// and there

    }
    cout << "\n\n\n";

    cout << x.size();
    cout << x.max_size();

    }
    void inserta(list<dat>& x)
    {
    dat pMySrtuct ;
    pMySrtuct.valor1 = random(100)+1;
    pMySrtuct.valor2 = "Buenas";
    x.push_front(pMySrtuct);
    }

    the error message is :
    Pointer to strcuture required on left side -> or ->*
    Please tell how I can make a list of struct data and manage the items with iterator like a vector or deque
    Thanks and sorry for my English.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Please edit your post and add code tags (as described in the stickied post: << !! Posting Code? Read this First !! >>).

    That part of your code looks correct at first glance, and it compiles for me when I remove the randomize() and random() function calls that aren't defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM