Thread: stl for pairs

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    stl for pairs

    Is there an stl in which values can be INSERTED and REMOVED in pairs...
    An example implementation with a sample code would be very helpfull

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by an "stl"? A container from the standard library? If so, you could use a container with std::pair or a struct of your own making, or perhaps you are looking to use std::map.
    Last edited by laserlight; 05-14-2009 at 10:26 AM.
    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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    yup... I was thinking about using map (i havn't used it though)

    I am not sure about maps(as far as i know ,it has key valus and its assoaciated values)...but will that solve my requirement

    for eg:
    1 3
    3 4
    4 5

    --are the pairs .

    I should have the facility to insert pairs :6 7....3 2 etc.... as well as remove some or all of these && find the existence of any of the pairs

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are your requirements? For example, are you trying to associate a unique key with a non-unique value, or must the pairs themselves be unique? Does order matter? What kind of efficiency considerations do you have for say, insertion, deletion, and access?
    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

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    --any order...
    ---The pair is unique
    ---insertion any order
    ---deletion of specified pair(whereever it may be)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like a std::set<std::pair<int, int> > would be suitable for your requirements.
    Last edited by laserlight; 05-14-2009 at 11:05 AM.
    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

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    can i have a simple example (i could not find a simple one in the net ;( ...) with all insertions deletions

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by laserlight View Post
    It sounds like a std::set<std::pair<int, int> > would be suitable for your requirements.
    This may be what you want, but just be sure to note that 1,2 and 1,3 are different pairs. If you want a map where 1,3 would override 1,2 it would take more work ex:
    Code:
    std::set<std::pair<int,int> > list;
    list.insert(std::make_pair(1,3));
    list.insert(std::make_pair(1,4));
    Would insert two different things, because the pairs are different.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    ya thanks....i guess the remove would be the same...
    is there a function to check whether the pair is in the list ..
    like wat we have in set...set.find(value)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    is there a function to check whether the pair is in the list ..
    like wat we have in set...set.find(value)
    It sounds like you answered your own question: you can use the find member function.
    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

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    It sounds like you answered your own question: you can use the find member function.
    Code:
    if( mySet.find( std::make_pair( x, y ) ) != mySet.end() )
    {
        // Found the pair (x, y) in the set
    }
    else
    {
        // No such pair (x, y) in the set
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by CornedBee View Post
    awesome website, I'll be sure to use that in the future
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    *How do i print a pair value in cout
    *How to remove the pair value
    Code:
    list.remove(std::pair(1,4));
    dint work...

    well the reason i am asking is cos I could not find a proper site where i could get answers for my questions...bear with me

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Pairs are not printable by default. Learn how to write your own stream insertion operations, or write a simple function that prints the pair.

    A list doesn't support removing by value. And it's called erase anyway. So look up erase. You'll find that it takes an iterator to the element that should be erased. Now, how do you get an iterator to an element with a specific value? Hint: it's a free function in the <algorithm> header.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM