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
Printable View
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
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.
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
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?
--any order...
---The pair is unique
---insertion any order
---deletion of specified pair(whereever it may be)
It sounds like a std::set<std::pair<int, int> > would be suitable for your requirements.
can i have a simple example (i could not find a simple one in the net ;( ...) with all insertions deletions
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:
Would insert two different things, because the pairs are different.Code:std::set<std::pair<int,int> > list;
list.insert(std::make_pair(1,3));
list.insert(std::make_pair(1,4));
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)
It sounds like you answered your own question: you can use the find member function.Quote:
Originally Posted by dpp
*How do i print a pair value in cout
*How to remove the pair valuedint work...Code:list.remove(std::pair(1,4));
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
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.