stl for pairs

This is a discussion on stl for pairs within the C++ Programming forums, part of the General Programming Boards category; Is there an stl in which values can be INSERTED and REMOVED in pairs... An example implementation with a sample ...

  1. #1
    dpp
    dpp is offline
    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
    22,274
    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 11:26 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    dpp
    dpp is offline
    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
    22,274
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    dpp
    dpp is offline
    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
    22,274
    It sounds like a std::set<std::pair<int, int> > would be suitable for your requirements.
    Last edited by laserlight; 05-14-2009 at 12:05 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    dpp
    dpp is offline
    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
    dpp
    dpp is offline
    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
    22,274
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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,893
    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
    dpp
    dpp is offline
    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,893
    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-24-2004, 12:22 AM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 03:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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