STD Lists doubt

This is a discussion on STD Lists doubt within the C++ Programming forums, part of the General Programming Boards category; I need to store multiple classes in a list in order to access the data by a number, but the ...

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    STD Lists doubt

    I need to store multiple classes in a list in order to access the data by a number, but the list is in continuous modification, so I can't access the data using the position of the data in the list. I think the better way is to scan for the data number, but in that case I need to return an iterator, how can I return an iterator from a function? Do I have an alternative to this?
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It's easy to return an iterator, just return it like you would any other type:
    Code:
    std::list<MyClass>::iterator foo();
    Are you sure that's the best way, though? If you're accessing the data by a number, perhaps a map would be more appropriate? Instead of scanning the list for that number, which is O(n), you would be looking it up in a map which is O(log n). You could also use a hashed map (like unordered_map) which if setup correctly would give you O(1) lookup.

    If the order you keep the item in the container is important, then that might not be the best solution, though. In that case you'd have two keys, one being the ordering in the container and the other being the number you referred to.

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I've read about maps
    Classname tanto;

    map<int,Classname> first;

    first.insert(1, tanto);
    Now, to access the class data, is it first[1].function(); or first->function(); ?
    Last edited by lautarox; 03-08-2010 at 01:48 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    map is a standard container just like list or vector. It maps a key to a value. So if your class had some type of unique ID, you could map that ID to the class object. Then, when you want to get the object from the container, you just lookup the key instead of scanning each element trying to find it.
    Code:
    #include <iostream>
    #include <map>
    #include <string>
    
    class my_data
    {
    public:
      std::string text;
      my_data(const char* t = "") : text(t) { }
    };
    
    int main()
    {
        std::map<int, my_data> data_map;
    
        // insert new elements
        data_map.insert(std::make_pair(1, my_data("one")));
        data_map.insert(std::make_pair(1200, my_data("twelve hundred")));
        data_map.insert(std::make_pair(5, my_data("five")));
    
        // insert or change existing elements
        data_map[10] = my_data("ten");  // insert
        data_map[1200] = my_data("one thousand two hundred");  // change
        data_map[77] = my_data("seventy seven");  // insert
    
        // Look up a single element:
        int lookup = 10;
        std::map<int, my_data>::const_iterator one_item = data_map.find(lookup);
        if (one_item != data_map.end())
            std::cout << one_item->second.text << " (" << lookup << ")\n";
        else
            std::cout << lookup <<  " not found.\n";
    
        lookup = 100;
        one_item = data_map.find(lookup);
        if (one_item != data_map.end())
            std::cout << one_item->second.text << " (" << lookup << ")\n";
        else
            std::cout << lookup <<  " not found.\n";
    
        // Lookup a single element (but automatically create a new one if it isn't there):
        lookup = 5;
        std::cout << data_map[lookup].text << " (" << lookup << ")\n";
        lookup = 50;
        std::cout << data_map[lookup].text << " (" << lookup << ")\n";
    
        // Show everything in the map:
        std::map<int, my_data>::const_iterator map_end = data_map.end();
        for (one_item = data_map.begin(); one_item != map_end; ++one_item)
        {
            std::cout << "Key: " << one_item->first;
            std::cout << " Value: \"" << one_item->second.text << "\"\n";
        }
    
        // done
        std::cin.get();
    }
    (Note: I just typed this up, I didn't test it.) Notice how you can insert in any order, but the loop at the end outputs them in sorted order. Also notice how you can use operator[] or iterators and member functions to access the map contents. The lookups in the map are faster algorithmically than list lookups.
    Last edited by Daved; 03-08-2010 at 02:01 PM.

  5. #5
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I can't thank you enough! I really needed this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  2. Cant use std namespace, help required
    By esaptonor in forum C++ Programming
    Replies: 16
    Last Post: 08-15-2006, 12:15 PM
  3. which to prefix with std:: ??
    By wakish in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2005, 03:58 PM
  4. help w/linked lists
    By Shy_girl_311 in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2001, 05:35 PM
  5. I need some help on my linked lists app
    By valar_king in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21