| | |
Multimap
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi..
Suppose I have a multimap like below
I want to print a output flags for first element, middle element and last element for multi map. Like e.g,
Also suppose I want to get the maximum length in a a field. Like here I should get maximum length = 2 which is for c.
Then if suppose I want to format it and set the fix length for this field as 2.
Also lets suppose I want the value to have more than one value like
That means for my key a I have got more than 2 values. How can I make it work since pair is not working in this case ?
Thanks
Suppose I have a multimap like below
C++ Syntax (Toggle Plain Text)
multimap<string, int> m; m.insert(pair<string, int>("a", 1)); m.insert(pair<string, int>("c", 20)); m.insert(pair<string, int>("b", 3)); m.insert(pair<string, int>("b", 4)); m.insert(pair<string, int>("a", 5)); m.insert(pair<string, int>("b", 6));
C++ Syntax (Toggle Plain Text)
a 1 start a 5 end b 3 start b 4 middle b 6 end c 20 single
Also suppose I want to get the maximum length in a a field. Like here I should get maximum length = 2 which is for c.
Then if suppose I want to format it and set the fix length for this field as 2.
Also lets suppose I want the value to have more than one value like
C++ Syntax (Toggle Plain Text)
m.insert(pair<string, int>("a", (int1,int2,string1,string2)));
Thanks
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Hi,
your question is bit unclear... what are you storing in the multimap logically. What I've understood is that you want to get the multiple values against the same key
Hope this helps
your question is bit unclear... what are you storing in the multimap logically. What I've understood is that you want to get the multiple values against the same key
cpp Syntax (Toggle Plain Text)
multimap<int, string> mm; mm.insert(std::make_pair(1, "a")); mm.insert(std::make_pair(1, "c")); mm.insert(std::make_pair(2, "d")); mm.insert(std::make_pair(2, "e")); typedef pair<multimap<int, string>::iterator, multimap<int, string>::iterator> Pair_Range; Pair_Range pRange = mm.equal_range(1); for (multimap<int, string>::iterator it = pRange.first; it != pRange.second; ++it) { cout << it->second<<endl; }
Hope this helps
I think I was clear in my question.
Well I solved the last part where I was showing something like this since I have no experience in using map:-
I simply added the required string arrays like this :-
strcombo = str1 +"\t"+ str2 +"\t"+ str3
and then created map like this ;-
multimap<string , string> mmmap;
and then :-
mmmap.insert(pair<string, string>(strcombo, str1));
So it solved my problem.
Now the first problem is still there:-
1) I want to print the flags for the first / middle and the last value for each key ?
Hope I can get some advice on that.
Thanks
Well I solved the last part where I was showing something like this since I have no experience in using map:-
C++ Syntax (Toggle Plain Text)
m.insert(pair<string, int>("a", (int1,int2,string1,string2)));
I simply added the required string arrays like this :-
strcombo = str1 +"\t"+ str2 +"\t"+ str3
and then created map like this ;-
multimap<string , string> mmmap;
and then :-
mmmap.insert(pair<string, string>(strcombo, str1));
So it solved my problem.
Now the first problem is still there:-
1) I want to print the flags for the first / middle and the last value for each key ?
Hope I can get some advice on that.
Thanks
Last edited by Web_Sailor; Oct 1st, 2009 at 5:29 am.
Hmm, I don't know why you're fixated on using a map.
Why not create your own object/class?
And then store this in a vector?
Why not create your own object/class?
C++ Syntax (Toggle Plain Text)
class Something { public: char letter; //a int length; //3 string pos; //start }
And then store this in a vector?
Last edited by iamthwee; Oct 1st, 2009 at 5:50 am.
*Voted best profile in the world*
Yep, and thinking out aloud...
I assume you have to add the third parameter pos which will either be single, start-end or start-middle-end and no other case.
Therefore if you sort your file by letter so that
you have:
Your algo would to count the number of entries per letter.
And the following snippet might be useful when you come to custom sort your class...
http://www.daniweb.com/code/snippet217098.html
I assume you have to add the third parameter pos which will either be single, start-end or start-middle-end and no other case.
Therefore if you sort your file by letter so that
you have:
a 1 start
a 5 end
b 3 start
b 4 middle
b 6 end
c 20 singleYour algo would to count the number of entries per letter.
if count = 1 then
single
else if count = 2 then
start end
else if count = 3 then
start middle end
else
discard (bad data)And the following snippet might be useful when you come to custom sort your class...
http://www.daniweb.com/code/snippet217098.html
Last edited by iamthwee; Oct 2nd, 2009 at 5:34 am.
*Voted best profile in the world*
For example....
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; class Foo { public: string name; double length; }; //This functions sort by name THEN length bool SortFooByNameThenLength ( const Foo& left, const Foo& right ) { if ( left.name > right.name ) { return false; } else if ( left.name < right.name ) { return true; } /*if letters are both the same now go to length and sort by that instead */ else { /*note the difference, left.LENGTH instead of left.NAME */ if (left.length > right.length ) { return false; } else { return true; } } } int main() { Foo test; vector <Foo> myFoo; test.name = "a"; test.length = 1; myFoo.push_back(test); test.name = "b"; test.length = 5; myFoo.push_back(test); test.name = "a"; test.length = 5; myFoo.push_back(test); test.name = "b"; test.length = 6; myFoo.push_back(test); test.name = "b"; test.length = 3; myFoo.push_back(test); test.name = "c"; test.length = 20; myFoo.push_back(test); // call the sort like this std::sort(myFoo.begin(), myFoo.end(), SortFooByNameThenLength); for ( unsigned int i = 0; i < myFoo.size(); i++ ) { cout <<myFoo[i].name << " " << myFoo[i].length << endl; } cin.get(); return 0; }
*Voted best profile in the world*
![]() |
Similar Threads
- trying to get the second 2nd value of same key in multimap (C++)
- Multimap confusion; how does it work? (C)
- problems with multimap... (C++)
- Multimap or other ? (C++)
Other Threads in the C++ Forum
- Previous Thread: may pay money for vectorized MD5
- Next Thread: Hidding the dos window
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






