I'm trying to use the STL unordered multimap. I read a file in and insert it into a vector, then strip the puncuation, then insert the words into the map. I then want to have the program spit out where in the map a certain key is. (it can be in multiple places)
My problem is that the compiler throws this error:
error: no match for ‘operator<’ (operand types are ‘std::pair<const std::__cxx11::basic_string<char>, int>’ and ‘const std::__cxx11::basic_string<char>’)
{ return *__it < __val; }
~~~~~~^~~~~~~
Does anyone have any ideas why the compiler is throwing this error? I declared the pair correctly according the cppreference, so I don't know why the operator is throwing an error.
partitioned with respect to element < value or comp(element, value) (that is, all elements for which the expression is true precedes all elements for which the expression is false)
I don't think you need to be using std::equal_range. I think you can get away with just using a foreach loop.
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <utility>
usingnamespace std;
typedefunsignedint uint;
typedef unordered_multimap<string, int> hashtable;
int main(){
hashtable map;
std::string word = "hello";
int line_num = 42;
map.insert(make_pair(word, line_num));
for (const std::pair<std::string, int>& element : map) // (or just do const auto&)
{
std::cout << element.first << " " << element.second << '\n';
}
}
Other notes:
You do not need to use the int values of characters, you can use the character symbol itself.
- 95 is an underscore, so you can do *p != '_'
- 32 is a space, so you can do *p = ' '
In general, avoid using eof() or similar conditions for looping.
You want to check if the input itself was successfully extracted, so you want to check to see if line 79 was successful.
1 2 3 4 5 6 7 8
if (cin >> word)
{
// good
}
else
{
// unable to extract
}