Wikipedia:Reference desk/Archives/Computing/2016 February 3#more STL questions
{{#ifeq:{{PAGENAME}}|Special:Undelete| |{{#if:|
more STL questions
First of all, thank you to everyone who helped me yesterday; I really appreciate it. Now, why doesn't the following code compile?
- include
- include
- include
- include
typedef std::vector
typedef std::unordered_map
int f(){
myvec v;
uom S;
v.clear();
v.push_back(3);
v.push_back(-3);
S[v] = 4.4;
return (0);
}
The compiler gives a whole bunch of error messages starting with
Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:386:17: error: no matching function for
call to object of type 'const std::__1::equal_to
{return static_cast
^19:59, 3 February 2016 (UTC)19:59, 3 February 2016 (UTC)19:59, 3 February 2016 (UTC)19:59, 3 February 2016 (UTC)19:59, 3 February 2016 (UTC)19:59, 3 February 2016 (UTC)~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:2014:21: note: in instantiation of member
function 'std::__1::__unordered_map_hasher std::__1::allocator size_t __hash = hash_function()(__k); I don't understand these messages, can anyone advise? Thanks, Robinh (talk) 19:59, 3 February 2016 (UTC) :Don't use Nimur's code; it is wrong (and causing compilation errors) for reasons unrelated to your problem. To use unordered_map, just replace map with unordered_map in your code and provide a hash function for vector<int>. In the previous thread I linked to a couple of possible hash functions and an explanation of how to use custom hash functions with unordered_map. -- BenRG (talk) 03:06, 4 February 2016 (UTC)