0
#include<bits/stdc++.h>
using namespace std;
void getValue(multimap<int,string> &m){
    for(auto &mValue : m){
        cout<<mValue.first<<" "<<mValue.second<<endl;
    }
}
int main(){
    int n;
    cin>>n;
    multimap<int,string> m;
    
    for(int i=0;i<n;i++){
        int num;
        string str;
        cin>>num;
        getline(cin,str);
        m.insert(make_pair(num,str));
    }
    

    getValue(m);
    return 0;
}

Error : invalid initialization of reference of type ‘std::map >&’ from expression of type ‘std::multimap >’ getValue(m);

  • Do not use `std::make_pair` with `emplace()` it does not make any sense. – Slava May 11 '21 at 15:08
  • If you're just trying to print unique keys in your multimap, does this answer your question? [is there an iterator across unique keys in a std::multimap?](https://stackoverflow.com/questions/9371236/is-there-an-iterator-across-unique-keys-in-a-stdmultimap) – scohe001 May 11 '21 at 15:10
  • You'll probably have this problem once it compiles: http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – chris May 11 '21 at 15:10
  • A `std::multimap` is not a kind of `std::map`; they are unrelated types. `getValue` will have to accept a `std::multimap` reference, or you could make it a function template. – Fred Larson May 11 '21 at 15:11
  • `template void getValue(const M& m) { /*same code*/ }` – m88 May 11 '21 at 15:11
  • 1
    Side note: The time you save by only having to type `#include` is likely quickly eaten up by your builds taking ten times longer than using the required headers directly. Add to that the identifier minefield that results from including the entire C++ Standard Library and pulling it into the global namespace with `using namespace std` and you're writing some seriously brittle code that will require more compiles. – user4581301 May 11 '21 at 15:12
  • 4
    Rename `getValue` as `printValues`? – Jarod42 May 11 '21 at 15:12
  • 2
    @Jarod42 I just wanted to say the same, and change references to `const` – Slava May 11 '21 at 15:13

1 Answers1

2

std::map<int, std::string> is a different type to std::multimap<int, std::string>, although they bear similarities.

The simplest way would to write a similar function:

void getValue(const std::multimap<int, std::string> &m){
    for(auto &mValue : m){
        std::cout<<mValue.first<<" "<<mValue.second<<std::endl;
    }
}

However there a a number of map-like containers in std and beyond, so you might want to change getValue to be a template

template <typename Map>
void getValue(const Map &m){
    for(auto &mValue : m){
        std::cout<<mValue.first<<" "<<mValue.second<<std::endl;
    }
}

You might want to constrain that to only accept map-like types, e.g. (with C++17's library addition of std::void_t)

template <typename Map>
std::void_t<typename Map::key_type, typename Map::mapped_type> getValue(const Map &m){
    for(auto &mValue : m){
        std::cout<<mValue.first<<" "<<mValue.second<<std::endl;
    }
}
Caleth
  • 52,200
  • 2
  • 44
  • 75