MyWiki:Reference desk/Archives/Computing/2016 February 3

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

This template must be substituted. Replace {{Archive header with {{subst:Archive header.

{| width = "100%"

|- ! colspan="3" align="center" | Computing desk |- ! width="20%" align="left" | < February 2 ! width="25%" align="center"|<< Jan | February | Mar >> ! width="20%" align="right" |Current desk > |}

Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


February 3

[edit source]

more STL questions

[edit source]

First of all, thank you to everyone who helped me yesterday; I really appreciate it. Now, why doesn't the following code compile?

#include <unordered_map>
#include <vector>
#include <utility>
#include <iterator>

typedef std::vector<signed int> myvec;
typedef std::unordered_map<myvec, double, std::equal_to<std::vector<signed int> > >  uom;  

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<std::__1::vector<int, std::__1::allocator<int> > >'
       {return static_cast<const _Hash&>(*this)(__x);}
               ^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::vector<int, std::__1::allocator<int> >, std::__1::__hash_value_type<std::__1::vector<int,
     std::__1::allocator<int> >, double>, std::__1::equal_to<std::__1::vector<int, std::__1::allocator<int> > >, true>::operator()' requested here
   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)