logo
down
shadow

Unordered_map with custom class as key


Unordered_map with custom class as key

By : user2186730
Date : November 25 2020, 04:01 AM
will be helpful for those in need I am working on a compiler right now and I am trying to represent the scope class by inheriting from unordered_map, because it is essentially a hash table of symbols to declarations. I added a custom hash function to the symbol, but I am getting an error complaining there is no default constructor for initializing a std::pair. Here is the relevant code:
code :
emplace(d->get_name()->get_str(), d);
/Library/Developer/CommandLineTools/usr/include/c++/v1/utility:422:5: note: candidate constructor not viable: no known conversion from
      'std::__1::basic_string<char>' to 'const const Symbol' for 1st argument
    pair(_T1 const& __t1, _T2 const& __t2)


Share : facebook icon twitter icon
insert custom class into unordered_map c++

insert custom class into unordered_map c++


By : Max Rahleev
Date : March 29 2020, 07:55 AM
wish of those help None of the two-parameter insert methods matches insert(key_type, mapped_type), which is what you are attempting.
The map holds std::pair, so you need to insert a pair, either explicitly:
code :
my_items.insert(std::make_pair(Node(33), 894));
my_items.insert({Node(33), 894});
my_items.emplace(Node(33), 894);
GNU C++: why custom class can not be value type for unordered_map?

GNU C++: why custom class can not be value type for unordered_map?


By : vesaiosdev
Date : March 29 2020, 07:55 AM
may help you . This is most probably a stdlibc++ bug, in the version you're using.
I'm on ubuntu using gcc 4.8.1, and clang 3.5 trunk. With clang using libc++ there's no problem. With both clang and gcc using libstdc++ the issue is triggered.
code :
int main() {
    using namespace std;
    cout << is_convertible<const A &, A>::value << endl;
}
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:117:14: error: base class has incomplete type
    : public conditional<_B1::value, _B2, _B1>::type
      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_pair.h:122:19: note: in instantiation of template class
      'std::__and_<std::is_convertible<const int &, const int>, std::is_convertible<const A &, A> >' requested here
               enable_if<__and_<is_convertible<const _U1&, _T1>,
                         ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_pair.h:124:12: note: in instantiation of default argument for
      'pair<const int, A>' required here
        constexpr pair(const pair<_U1, _U2>& __p)
                  ^~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_pair.h:124:12: note: while substituting deduced template arguments into
      function template 'pair' [with _U1 = const int, _U2 = A, $2 = <no value>]
        constexpr pair(const pair<_U1, _U2>& __p)
                  ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:803:24: note: in instantiation of default argument for
      '__test<std::pair<const int, A>, const std::pair<const int, A> &>' required here
      static true_type __test(int);
                       ^~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:803:24: note: while substituting deduced template arguments into
      function template '__test' [with _Tp = std::pair<const int, A>, _Arg = const std::pair<const int, A> &, $2 = <no value>]
      static true_type __test(int);
                       ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:117:14: note: (skipping 10 contexts in backtrace; use
      -ftemplate-backtrace-limit=0 to see all)
    : public conditional<_B1::value, _B2, _B1>::type
             ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/unordered_map.h:97:27: note: in instantiation of template type alias
      '__check_copy_constructible' requested here
    class unordered_map : __check_copy_constructible<_Alloc>
                          ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1305:42: note: in instantiation of template class
      'std::unordered_map<int, A, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, A> > >' requested here
        static decltype(__test_aux<_To1>(std::declval<_From1>()), __one())
                                         ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1306:2: note: while substituting explicitly-specified template
      arguments into function template '__test'
        __test(int);
        ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1319:11: note: in instantiation of template class
      'std::__is_convertible_helper<const A &, A, false>' requested here
                               __is_convertible_helper<_From, _To>::value>
                               ^
c++1y-sample.cpp:33:13: note: in instantiation of template class 'std::is_convertible<const A &, A>' requested here
    cout << is_convertible<const A &, A>::value << endl;
            ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1317:12: note: definition of 'std::is_convertible<const A &, A>' is
      not complete until the closing '}'
    struct is_convertible
unordered_map of different custom types

unordered_map of different custom types


By : awaymoko
Date : March 29 2020, 07:55 AM
it should still fix some issue Suppose if I have these two enums: , I believe you're looking for nested maps:
code :
std::unordered_map<Type, std::unordered_map<Location, unsigned int>>
unordered_map for custom class does not cause error when inserting the same key

unordered_map for custom class does not cause error when inserting the same key


By : nithin veerakumar
Date : March 29 2020, 07:55 AM
Hope this helps Your hash and operator == must satisfy a consistency requirement that they currently violate. When two objects are equal according to ==, their hash codes must be equal according to hash. In other words, while non-equal objects may have the same hash code, equal objects must have the same hash code:
code :
size_t operator()(const Line& k) const  {
    return hash<float>()(k.getM());
}   
C++ unordered_map using a custom class type as the key

C++ unordered_map using a custom class type as the key


By : BoN
Date : March 29 2020, 07:55 AM
may help you . To be able to use std::unordered_map (or one of the other unordered associative containers) with a user-defined key-type, you need to define two things:
Related Posts Related Posts :
  • C++ on Vistual Studio with CMake error: manifest 'build.ninja' still dirty after 100 tries
  • Is there a way to automatically resolve an overloaded method via a template parameter?
  • Fastest way to find pair in a vector, remove it while iterating
  • error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR'
  • Unable to call boost::clear_vertex while using listS for the vertex and edge lists
  • Decoding binary data from serial port
  • Pattern to Implement an OO interface to a C program written in an imperative style
  • CEPH + Librados++ is not returning consisten results
  • `LoadLibraryExW` triggers exception `0xC0000023` from `NtMapViewOfSection`
  • static_cast to a struct type to access all of its member variable
  • Forward declaration for boost::intrusive_ptr class to reduce compile time
  • How to use priority_queue with a non-static compare method of class instance?
  • Template parameters inside and outside class/struct
  • Determining prime number
  • How to resolve ambiguity between constructors taking std::string and std::vector
  • My program crashes when I try to change private values from an object
  • Strict aliasing rules broken with templates and inheritance
  • C++ Derived Class Override Return Type
  • singly linked list c++ constructor, destructor and printing out
  • How to clone class with vector of unique_ptr to base class
  • error: no match for operator
  • std::vector doesnt accept my struct as template
  • selection of people's contours
  • how to fix the (Error using mexOpenCV) on matlab?
  • Is or was there a proposal for c++ to use the context for short enum values?
  • Fair assumptions about std::hash implementations
  • undefined reference to libusb using cyusb
  • Function returns null pointer instead of address
  • C++17 copy elision and object destruction
  • Input multiple strings via operator>> c++
  • Avoiding overflow boost container
  • How to Write a Lambda Wrapping a Function with Optional Return Value
  • Partial specialization with more template parameters
  • How to convert fixed size array to pointer on pointer array
  • Memory leak in const member constructor with tag dispatching
  • C++ function with a generic and optional class
  • Custom QGraphicsItem That Contains Child QGraphicsItems
  • Are There Restrictions on What can be Passed to auto Template Parameters?
  • Rotating line inside rectangle bounds
  • Why do I need dynamic memory allocation if I can just create an array?
  • How can I convert a text file into a form that MPI_Bcast can send?
  • How to get array of all `this` of an instance
  • Using pointers as parameters
  • Automatic type deduction with const_cast is not working
  • Why does std::is_rvalue_reference not do what it is advertised to do?
  • Function Template Specialization with Forward Declared Type
  • template deduction failed in vector
  • Is there a signed `sizeof` alternative in Qt
  • clarification on overloading the -> operator
  • What is __m128d?
  • QtConcurrent: why releaseThread and reserveThread cause deadlock?
  • Function receiving different value than passed
  • Can C++ close a '''fstream''' variable after '''.close()'''?
  • Is it necessary to overload operator in this specific case
  • Comparing an element of a string array with a string
  • how to dereference a pointer of a map of pointers to objects in c++
  • How recursive function control flow change for static variable?
  • SDL 2 blitting BMP File
  • Why does an extremely large value cause this code to repeat infinitely?
  • Function returning different value than what is in function body before return statement
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org