Блог пользователя MaxWelll

Автор MaxWelll, история, 7 лет назад, По-английски

Can anybody tell me about C++ multimaps. As far as I know you can assign many values to a key but... how do I access to any of theese values?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
map<int,set<int, int> > m;
m[key].insert(values);
m[key].find(x)
  • »
    »
    7 лет назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится
    Sorry but I dont get it yet.
    
    I mean for example:
    
    I have 1->2, 2->4, 1->3 and 3->5. That would be as:
    
    key |  values
       1|  {2,3}
       2|  {4}
       3|  {5}
    

    How can I know how many values are assigned to every key and how can I make reference to each of the values a key has?

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Thanks two of you for help

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Unfortunately they didn't tell you how to use multimap. Here's an example.

    • »
      »
      »
      7 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      That's true, their comments were helpful but what i wanted was an example of using multimap. Could you make a comment with the example, my internet provider does not allow me to go that link

      • »
        »
        »
        »
        7 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится
        #include <map>
        #include<iostream>
        using namespace std;
        int main() {
        	multimap<int,string> m;
        	m.insert({1,"hello"});
        	m.insert({1,"world"});
        	m.insert({2,"goodbye"});
        	m.insert({3,"spacecows"});
        	
        	cout<<"Number of elements in key 1: "<<m.count(1)<<endl;
        	
        	auto range = m.equal_range(1);
        	for(auto iter = range.first; iter != range.second; iter++) cout<<iter->second<<" ";
        	cout<<endl;
        	return 0;
        }
        
        • »
          »
          »
          »
          »
          7 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          I see, very ilustrative example. I've never used auto I gess it make the variable get the type that is asigned to it, am I right?

          • »
            »
            »
            »
            »
            »
            7 лет назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Yes, the compiler will deduce the type for you. auto is quite a bit shorter to write than pair<multimap<int,string>::iterator,multimap<int,string>::iterator>.

            I recommend reading these two posts (C++11 for programming contests... and C++ Tricks) for some additional info on using C++11 in programming contests.