473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

trying to get the second 2nd value of same key in multimap

14
I am unable to get the "is" I keep getting the below:

the find : this
the find : this


what I want is:

the find : this
the find : is


from the below code:
Expand|Select|Wrap|Line Numbers
  1. typedef multimap<int,string> IntStringMMap;
  2.  
  3. IntStringMMap coll; // container for int/string values
  4.  
  5. coll.insert(make_pair(2,"a"));
  6. coll.insert(make_pair(1,"this"));
  7. coll.insert(make_pair(4,"of"));
  8. coll.insert(make_pair(1,"is"));
  9. coll.insert(make_pair(3,"multimap"));
  10.  
  11.  
  12. IntStringMMap::iterator post = coll.find(1);
  13. cout << "the find : " << post->second << endl;
  14. post = coll.find(1);
  15. cout << "the find : " << post->second << endl;
Nov 1 '06 #1
2 2172
Banfa
9,065 Expert Mod 8TB
Try

Expand|Select|Wrap|Line Numbers
  1. IntStringMMap::iterator post = coll.find(1);
  2. cout << "the find : " << post->second << endl;
  3. post++;
  4. cout << "the find : " << post->second << endl;
  5.  
Nov 1 '06 #2
srikpen
14
[quote=Banfa]

Thanks very much Banfa.
Just one query. Is this "post++" (i.e. next same-key value is at next data-type + 1 address location) a compiler/c++ standard or it varies on
windows or other unix platforms?

On the other hand your solution made me to dump another workaround.
It works great.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9.     // type of the collection
  10.     typedef multimap<int,string> IntStringMMap;
  11.  
  12.     IntStringMMap coll;        // container for int/string values
  13.  
  14.     // insert some elements in arbitrary order
  15.     // - a value with key 1 gets inserted twice
  16.     coll.insert(make_pair(5,"tagged"));
  17.     coll.insert(make_pair(2,"a"));
  18.     coll.insert(make_pair(1,"this"));
  19.     coll.insert(make_pair(4,"of"));
  20.     coll.insert(make_pair(6,"strings"));
  21.     coll.insert(make_pair(1,"is"));
  22.     coll.insert(make_pair(3,"multimap"));
  23.  
  24.    // Find on srchkey and print all values on the srchkey
  25.    int srchkey = 1;
  26.    IntStringMMap::iterator post = coll.find(srchkey);
  27.    while( post != coll.end() && post->first == srchkey )
  28.    {
  29.       cout << "key value " << srchkey << " has : "  << post->second << endl;
  30.       post++;
  31.    }
  32. }
  33.  
Nov 6 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Tanguy Fautré | last post by:
Hello, does std::multimap make any guarantee about the insertion order? for example: int main() { std::multimap<int, int> Map;
7
by: Kevin W. | last post by:
How do I sort a map by the value, rather than the key? (either automatically or with the sort function.) -- Kevin W :-) Opera/CSS/webdev blog: http://www.exclipy.com/ Using Opera:...
3
by: He Shiming | last post by:
Hi Folks, Happy holidays! I have a question regarding STL multimap. Basically, the current multimap<int,int> look like this: key=>value 1=>10, 1=>20, 1=>30,
3
by: Przemek | last post by:
All, what is the efficient way to find all distinct keys in a multimap. I use the code template<class T1, class T2> set<T1> GetDistinctKeys(multimap<T1, T2> nMap) { T1 temp; multimap<T1,...
1
by: placid | last post by:
Hi all, i was just wondering if i have a class class A {}; then i want to use a multimap like, multimap<string,A*> as; //can i have a pointer to a A object ? A* aa = new A();
4
by: Nick Keighley | last post by:
Hi, I've checked out various documentation for multimap but can't find anywhere it explicitly stated that insert() invalidates multimap iterators. consider this pseudo code:- int...
9
by: Jae | last post by:
Hi I wonder how can I implement the STL map sorting by value. For example, I have a map m map<int, intm; m = 10; m = 5; m = 6;
7
by: Matthias Pfeifer | last post by:
Hi there, a std::map is build from std::pair elements where for a pair p p.first is called the key and p.second the value. Is there a way to keep the map sorted by the values? Ie is there a...
20
by: puzzlecracker | last post by:
I am using while loop for that but I am sure you can do it quicker and more syntactically clear with copy function. Here is what I do and would like to if someone has a cleaner solution: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.