Results 1 to 3 of 3

Thread: Access an object stored in the list.

  1. #1
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Access an object stored in the list.

    Hi all,

    I am storing some objects in the list and the i am trying to access them one by one. Each object consists of two strings and one integer.

    The problem is that i cannot access these objects through the iterator.
    1.How can i access these objects?

    Qt Code:
    1. #include <stdlib.h>
    2. #include <iostream>
    3. #include <map>
    4. #include <list>
    5. using namespace std;
    6.  
    7.  
    8. class A{
    9. public:
    10. string x;
    11. string y;
    12. int z;
    13. };
    14.  
    15. list<A> G_QUERY_LIST;
    16. list<A>::iterator it;
    17.  
    18.  
    19. void create_new_query()
    20. {
    21. string name;
    22. string query;
    23. string results;
    24. cout << "Please enter a name for this query: ";
    25. cin >> name;
    26. cout << "Please enter the query you want to execute: ";
    27. cin >> query;
    28. A ena;
    29. ena.x = name;
    30. ena.y = query;
    31. G_QUERY_LIST.push_back(ena);
    32. }
    33.  
    34.  
    35. int main() {
    36. create_new_query();
    37. create_new_query();
    38. cout << "mylist contains:";
    39. for (it=G_QUERY_LIST.begin(); it!=G_QUERY_LIST.end(); it++)
    40. cout<< *it.ena.x;
    41. cout << endl;
    42.  
    43.  
    44.  
    45. return 0;
    46.  
    47. }
    To copy to clipboard, switch view to plain text mode 

    Many thanks in advance.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access an object stored in the list.

    Qt Code:
    1. for (it=G_QUERY_LIST.begin(); it!=G_QUERY_LIST.end(); ++it)
    2. cout<< (*it).ena.x;
    To copy to clipboard, switch view to plain text mode 

    Priority of * is lower than that of "."

    FYI: Note that, generally, ++it is faster for iterators.

    HTH

    PS: If you do not mind using 3rd party libs: using Boost.Foreach your code could be written

    Qt Code:
    1. #include <boost/foreach.hpp>
    2. BOOST_FOREACH(const A &item, G_QUERY_LIST)
    3. cout<< item.ena.x;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to caduel for this useful post:

    cbarmpar (21st September 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access an object stored in the list.

    Excellent answer thats what i wanted!

    A small correction:
    its (*it).x and not (*it).ena.x

Similar Threads

  1. Access an object from another file.
    By cbarmpar in forum General Programming
    Replies: 1
    Last Post: 6th September 2008, 22:17
  2. Replies: 4
    Last Post: 26th June 2007, 19:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.