data in map/class

This is a discussion on data in map/class within the C++ Programming forums, part of the General Programming Boards category; Well.. By each string (name) I only need one bool and vector <string> .. so I should use pair for ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Well.. By each string (name) I only need one bool and vector <string> .. so I should use pair for this? Names (string) should also be unique, thats why I wanted to go for map (would be easier)..

    Edit: Why did my post go to the top?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I would make a class. Whether you include the name in the class or not depends on whether the name is a logical part of whatever data you are trying to represent. If you include it in the class, then you can make a set of your class objects, and create an operator< for the class that compares only the name. Or if the name is not part of the class or if you might be changing names as you go, then use a map of string to class objects.

    As you said, if the list is big, it might be better to use a smart pointer. You can encapsulate that in the class itself so that when the class is copied the list is not.

    >> Why did my post go to the top?
    They fixed the time on the forum which apparently through off some post times.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    data in map/class

    Hello..

    I need to store data in a map/class that way (and I wonder what would be better to use):

    I need a list of 3 things:
    string name;
    bool is_static;
    vector <string> list_;

    So I could use:

    Code:
    map <string, map <bool, vector <string> > >
    or

    Code:
    class some_class {
    public:
      string name;
      bool is_static;
      vector <string> list_;
    }
    The list 'may' be huge, and thats why I think it would be smarter to pass it to the function as a smart pointer..

    What do you think would be best choice/approach to do?

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    If you are going to pass a large object to a function, it is usually far better to pass it by reference. It makes it easier on the stack heap, and takes less memory to complete the job.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Is name unique?
    Despite being unique, do you want the list to be keyed on it?


    Edit:
    On a side note, map <bool, vector <string> is a no-no, unless really that is what you want. To have a map that can only take 2 entries. One for false and one for true. The alternative here would be:

    map <string, pair <bool, vector <string> > >
    , or a multimap.
    Last edited by Mario F.; 11-04-2006 at 08:04 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM

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