Thread: container

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    container

    Hello,
    I have been developing in c++ for about 6 months. So, by no means am I an expert. I have come across a problem where I need some sort of data structure to hold information in pairs - I think. I'm using Microsoft Visual C++. One of my problems is I have an object that is directly related to 1 other of the same object, but I would only like to show one object of the pair in a GUI. If that object is selected then somehow it triggers the object that is related to it and I remove both from my data strucuture.

    Lets say I have 10 distinct objects in my data structure.
    Then I would have 5 pairs or 5 objects that are related to another object in the data strucutre.
    They are related by date or price.

    Can anyone help me with a solution? I'm not sure if I'm making sense, lol.

    -Alex.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    STL has a "pair" that holds two items that belong together in some fashion.

    Not sure if that's much help or not.

    http://www.sgi.com/tech/stl/pair.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    Yeah, I can see how that would work. Can I have a std::set<type pair> set1
    container?
    Thanks!
    -Alex.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are going to use a std::set<std::pair<T1, T2> >, a std::map<T1, T2> may be appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you have a key-value situation, where one type is always the type you use to uniquely identify a pair, then use a map. If a pair is uniquely identified by both types, then you can use a set<pair<T1, T2>, comparator> where comparator is a custom function or object for comparing pair<T1, T2> objects.

    So which you choose depends on how you would uniquely identify an object in your container.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting container
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 01:12 PM
  2. stl container to store more than 1 type
    By sujeet1 in forum C++ Programming
    Replies: 7
    Last Post: 05-09-2007, 04:10 AM
  3. inherited classes and stl container
    By rahulsk1947 in forum C++ Programming
    Replies: 9
    Last Post: 05-05-2007, 02:27 PM
  4. Replies: 1
    Last Post: 01-23-2006, 07:12 PM
  5. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM