C++ STL wrong keys types - c++

can't understand this: g++ compiler is angry on:
lengths.insert(pair<Deux,long>(d,one));
where
struct Deux {long big; long small};
map<Deux, long> lengths;
Deux d;
long one;
so, g++ said, that i miss operator<. after making overloaded operator< for struct Deux, i saw new interesting, but the same error:
map <long, Node*>ArrayOfNodes;
map <long, Node*>::iterator it;
for (it=ArrayOfNodes[Root]->nodes.begin();it<ArrayOfNodes[Root]->nodes.end();++it)
cout<<it->first<<endl;
also used structure Node:
struct Node {
long name;
long guest;
map <long,Node*>nodes;
/*bool operator<(const Node& node)const{
if ((*this).name<node.name) return true;
if ((*this).name>node.name) return false;
return (*this).guest<(*this).guest;
}*/
and error is:
no match for operator< in it < ((Path*)this)->Path::ArrayOfNodes.
std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = long int, _Tp = Node*,
_Compare = std::less<long int>, _Alloc = std::allocator<std::pair<const long int, Node*> >]
(((const long int&)((const long int*)(&((Path*)this)->Path::Root))))->Node::nodes.std::map<_Key, _Tp, _Compare, _Alloc>::end
[with _Key = long int, _Tp = Node*, _Compare = std::less<long int>, _Alloc = std::allocator<std::pair<const long int, Node*> >]()

The compiler complains that there is no operator < for Deux (I guess). The key must be a comparable class with operator < or you must pass a third template parameter to map - the comparator.
You see, the map keeps its keys in an ordered way. In order to order them, it needs a predicate. By default it tries to use operator <
try writing something like this:
bool operator < (Deux const & d1, Deux const & d2)
{
if(d1.big > d2.big)
return false;
if(d1.big < d2.big)
return true;
return d1.small < d2.small;
}

You did not mention the error message. Always post it!
So, I need to fork my post into two distinct sections.
Missing Declaration.
`error: 'Deux' was not declared in this scope`
That's because Deux is unknown at the point where you declare the map<>.
You need to declare Deux before map<Deux, long>, because map<Deux, long> requires the full definition of its parameters.
Missing Comparator.
`error: no match for 'operator<' in '__x < __y'`
That's because you haven't defined operator< for Deux.`
If you can define a logical operator<, i.e. one that is not arbitrarily chosen for sorting purposes, you could do it like this:
// must be in same namespace as Deux
bool operator< (Deux const &lhs, Deux const &rhs) {
return lhs.foo < rhs.foo;
}
If it needs access to non-public members, you can make it a member function:
bool operator< (Deux const &rhs) {
return this->foo < rhs.foo;
}
If such comparison would be arbitrary, do as Constantinius suggests.
Next time
You could have saved us time by posting your actual code or a minimal testcase, as well as by mentioning the error message.

My guess is that your struct is missing a compare function, to make an internal sorting of your Deux objects as keys. In a map, they have to be sorted.
This is the definition of std::map
template < class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > > class map;
So if you define a function like this:
bool compare_deux(Deux& a, Deux& b) {
return a.big < b.big;
}
and pass it as a template argument:
map <Deux, long, compare_deux> lengths;
you should be fine.

Use make_pair.
lengths.insert(make_pair<Deux,long>(d,one));

Just to add yet another reply, here's how I would implement Deux to be strictly ordered. We just use lexicographic ordering:
struct Deux
{
long big, small;
inline bool operator<(const Deux & o) const
{
return big < o.big || (!(o.big < big) && small < o.small);
}
};
std::map<Deux, T> m; // works now!
Alternatively, we could have spared ourselves the pain and said:
typedef std::pair<long, long> Deux;
std::map<Deux, T> m; // always works, lexicographic compare is provided by default
Always best not to reinvent the wheel!
Post a comment if you also want pair hashing using hash_combine to use unordered containers.

You need to declare the < operator or pass a comparison function on map creation.
struct Deux
{
long big;
long small
bool operator < (const Deux &n) const
{
if(big != n.big)
return big < n.big;
else
return small < n.small;
}
};

Related

find struct as value in std::map

I am trying to use std::map::count() to find a value, which is a custom struct, in the dictionary. The following code will not compile
typedef struct myStruct
{
int x;
int y;
}MyStruct;
MyStruct instace;
instace.x = 0;
instace.y = 1;
map<unsigned int, myStruct> myMap;
myMap[0] = instace;
if(myMap.count(instace) == 1)
{
//do something
}
and this is the error I get
no instance of overloaded function "std::map<_Kty, _Ty, _Pr, _Alloc>::count
[with _Kty=unsigned int, _Ty=myStruct, _Pr=std::less<unsigned int>,
_Alloc=std::allocator<std::pair<const unsigned int, myStruct>>]" matches the
argument list
I think the compiler does not know how to compare my struct. The third argument in the declaration of a map takes a compare function, I tried to create a compare function and pass it to the third argument in declaration like the following
struct comparer
{
bool operator()(MyStruct const& Left, MyStruct const& Right) const {
return (Left.x == Right.x && Left.y == Right.y);
}
};
map<unsigned int, myStruct, comparer> myMap;
but when I do myMap.count(), I get similar error.
I have searched google using every possible way I could describe the problem, but I did not find the answer. Can someone tell me how to solve this problem?
Thanks!
Looking at std::map::count, count takes the key and return the number of elements associated with that key. That means that your code should be:
if(myMap.count(0) == 1) // for key == 0

Failure on initialization of unordered map

and thanks for any input. I have a large dataset I am trying to manipulate. I am holding active elements in a list, and removing them when they become inactive. I want to hold all elements active and inactive in some data structure. Currently trying a map or an unordered_map, but am welcome to any suggestions.
I am compiling with
clang++ -std=c++11 -Wall -Wextra
When trying map:
#include <map>
std::map <class1, std::string> fullMap;
//and later...
for (std::list<class1>::iterator x = l.begin(); x != l.end(); x++)
{
fullMap[(*x)] = s
}
output reads:
error: invalid operands to binary expression ('const class1' and
'const class1') { return __x < __y; }
Even though I have overloaded the less than operator for class1.
This error originates at the overloaded bracket operators for map.
To circumvent I tried storing in an unordered_map.
#include <unordered_map>
std::unordered_map <class1, std::string> fullMap;
and the program fails at the initialization of fullMap with the even more confusing:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/hashtable_policy.h:830:23:
error: implicit instantiation of undefined template
'std::hash'
bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/hashtable_policy.h:1073:15: note: in instantiation of default argument for
'_Hashtable_ebo_helper<1, std::hash >' required here
private _Hashtable_ebo_helper<1, _H1>,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/hashtable_policy.h:1403:12: note: in instantiation of template class
'std::__detail::_Hash_code_base >, std::__detail::_Select1st, std::hash, std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash, true>' requested here : public
_Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/hashtable.h:175:14:
note: in instantiation of template class
'std::__detail::_Hashtable_base >, std::__detail::_Select1st, std::equal_to, std::hash,
std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash,
std::__detail::_Hashtable_traits >' requested here
: public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/unordered_map.h:100:18:
note: in instantiation of template class 'std::_Hashtable >,
std::allocator > >, std::__detail::_Select1st, std::equal_to, std::hash, std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy,
std::__detail::_Hashtable_traits >' requested here
_Hashtable _M_h;
^
main.cpp:34:44: note: in instantiation of template class
'std::unordered_map,
std::hash, std::equal_to,
std::allocator > > >' requested here std::unordered_map fullMap;
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/functional_hash.h:58:12:
note: template is declared here
struct hash;
I tried to cut the code down only to the relevant chunks, but let me know if more information is needed. Thanks for reading, any help is appreciated.
//
// class1.hpp
// class
//
// Created by Roach on 9/3/16.
// Copyright © 2016 Roach. All rights reserved.
//
#ifndef class1_hpp
#define class1_hpp
#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctime>
class class1
{
public:
class1 ();
class1 (const class1& t); // copy constructor
~class1 (); // destructor
class1& operator = (const class1& t); // assignment operator
bool operator == (const class1& t); // comparison operator
void setSetting2 (std::string t);
void setSetting1 (std::string p);
void setSetting3 (double d);
void setSetting4 (double d);
std::tm getTime () const;
std::string getSetting2 () const;
double getSetting3 () const;
double getSetting4 () const;
std::string getSetting1 () const;
void setSetting3End (double d);
void setSetting4End (double d);
double getSetting3End () const;
double getSetting4End () const;
double getSetting3flag () const;
double getSetting4flag () const;
double getSetting3final () const; // in pips
double getSetting4final () const; // in pips
void processList (class1::class1 t);
void setNew ();
//void dump (std::ostream& os) const;
private:
std::string setting1;
double setting4;
double setting3;
std::tm setting2;
double setting4End_;
double setting3End_;
bool setting4Flag_;
bool setting3Flag_;
double setting4final_; // in pips
double setting3final_; // in pips
};
// stream extraction operator
std::ostream& operator << (std::ostream& os, const class1& s);
std::istream& operator >> (std::istream& is, class1& t);
endif /* class1_hpp */
The following is my overloaded less than operator (I know it isn't the most succinct or efficient):
bool class1::operator< (const class1& t)
{
if (this->time_.tm_year < t.time_.tm_year) {return true;}
else if (this->time_.tm_year > t.time_.tm_year) {return false;}
else if (this->time_.tm_mon < t.time_.tm_mon) {return true;}
else if (this->time_.tm_mon > t.time_.tm_mon) {return false;}
else if (this->time_.tm_mday < t.time_.tm_mday) {return true;}
else if (this->time_.tm_mday > t.time_.tm_mday) {return false;}
else if (this->time_.tm_hour < t.time_.tm_hour) {return true;}
else if (this->time_.tm_hour > t.time_.tm_hour) {return false;}
else if (this->time_.tm_min < t.time_.tm_min) {return true;}
else if (this->time_.tm_min > t.time_.tm_min) {return false;}
else if (this->time_.tm_sec < t.time_.tm_sec) {return true;}
else {return false;}
}
The issue is that std::map<key_type, value_type> requires a properly defined operator< for key_type, in this case your operator< is not const specified so it is incompatible with std::map as this data structure requires that the comparator not alter the key object in any way. Thus the solution is to mark class1::operator< as const.
The second error notes that no hash function-object has been applied for use with std::unordered_map, this would require the following framework:
auto class1_hasher = [](const class1& c) -> std::size_t { return {some hash based on c}; }
std::unordered_map<class1, std::string, decltype(class1_hasher)> um;
I think the issue here is that you're breaking the preconditions required of the std::map and std::unordered_map interfaces.
In a std::map, the key type needs to be able to be compared using the less-than operator. This means that you either need to provide an overload of operator <, or provide a custom comparator when you're using the std::map type. Since you didn't provide a way of doing this with your type, the internal implementation of std::map wasn't able to make an expression of the form
somethingOfTypeClass1 < somethingElseOfTypeClass1
compile, hence your error message.
When you switched to std::unordered_map, you ran into trouble because, in order to store something as a key in an std::unordered_map, you need to specialize the std::hash template on your custom type because the internal works of an unordered_map require that the type is hashable. That's the second error you got.
To fix this issue, either
Define a custom operator < or comparator type for class1, then use std::map, or
Define a custom std::hash for class1, then use std::unordered_map.
Its difficult to suggest best data structure until we know the functionality requirement. But the Below code is working for me on GCC 4.9.3.
Please check your include files and syntax.
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <list>
using namespace std;
int main()
{
//LIST
std::list<int> myList;
myList.push_front(1);
myList.push_front(2);
myList.push_front(3);
myList.push_front(4);
//STRING
string s = "Test";
//MAP
std::map <int, std::string> fullMap;
for (std::list<int>::iterator x = myList.begin(); x != myList.end(); x++)
{
fullMap.insert(std::make_pair(*x,s));
}
//UNORDERED MAP
std::unordered_map <int, std::string> fullUnorderedMap;
for (std::list<int>::iterator y = myList.begin(); y != myList.end(); y++)
{
fullUnorderedMap.insert(std::make_pair(*y,s));
}
//PRINTING
for(auto it = fullMap.begin(); it != fullMap.end(); ++it)
{
cout<<it->first<<" "<<it->second<<endl;
}
for(auto it = fullUnorderedMap.begin(); it != fullUnorderedMap.end(); ++it)
{
cout<<it->first<<" "<<it->second<<endl;
}
}

Removing elements of 2D vector

I have a 2D vector and I need to remove blocks that do not contain sufficient values:
typedef vector<double> iniMatrix;
bool hasInsufficientEnergy() {
return true;
}
vector<double> Audio::filter(vector<iniMatrix>&blocks, double sumThres, double ZeroThres)
{
vector<double> totalEnergy;
vector<double> totalZeroCross;
double totalSum = sumThres * blocks.size();
double totalZero = ZeroThres * blocks.size();
vector<iniMatrix> blockked;
for(unsigned i=0; (i < blocks.size()); i++)
{
totalEnergy.push_back(abs(this->energy(blocks[i])));
totalZeroCross.push_back(zerocross(blocks[i]));
if(!totalEnergy[i] > totalSum || totalZeroCross[i] < ZeroThres)
{
hasInsufficientEnergy();
}else{
//hasInsufficientEnergy();
}
iniMatrix::iterator erase_after = remove_if(blocks[i].begin(), blocks[i].end(),
&hasInsufficientEnergy);
}
}
The problem is erase_after and comes up with an error message:
In function ‘_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator,
_OutputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<double*,
std::vector<double, std::allocator<double> > >, _OutputIterator =
__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >,
_Predicate = bool]’:
/usr/include/c++/4.2.1/bits/stl_algo.h:1302: instantiated from ‘_ForwardIterator
std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >,
_Predicate = bool]’
Audio.cpp:105: instantiated from here
/usr/include/c++/4.2.1/bits/stl_algo.h:1227: error: ‘__pred’ cannot be used as a function
Anyone have any ideas to where I am going wrong?
The third argument to std::remove_if is a predicate function (or any callable entity, for that matter) that takes a single element and returns true if it needs to be removed and false if not. The element of a vector of vectors (or "2D vector", as you call it) is a vector, vector<double> in your case. That's what the argument to your predicate should be:
bool HasInsufficientEnergy(const vector<double>& elem)
{
// herein is the code that tests elem and decides
// whether it should be removed or not
}
Then your filter() method shouldn't contain much more than this
void Audio::filter(vector<iniMatrix>&blocks)
{
auto it = std::remove_if(blocks.begin(), blocks.end(), &HasInsufficientEnergy);
blocks.erase(it, blocks.end());
}
If your predicate needs aditional arguments, then implement it as a class that takes them as constructor parameters. You need to overload operator(), too, so you have a callable functor.
If your compiler supports C++11, then learn how to use lambda functions as they are very useful for exactly this task.
true or false aren't function objects. If you want to use remove_if you must give it a pointer to function or function object, which takes a member of the collection as parameter and returns whether to remove it or not. Look at the example in std::remove_if
AFAICS, you could replace the remove_if/erase into the if and simplify it to:
for(auto i=blocks.begin(); i != blocks.end(); )
{
totalEnergy.push_back(abs(this->energy(*i)));
totalZeroCross.push_back(zerocross(*i));
if(!totalEnergy.rbegin() > totalSum || totalZeroCross.rbegin() < ZeroThres)
{
i = blocks.erase(i);
} else {
++i;
}
}
remove_if, and similar algorithms, expect a function, not a value, i.e. you should pass a function pointer or functor that an double const & and returns a bool
bool hasInsufficientEnergy(double const & element) {
// return true if it should be removed, false otherwise
}
and then
iniMatrix::iterator erase_after = remove_if(blocks[i].begin(), blocks[i].end(),
&hasInsufficientEnergy);
will work

Errors when using map() in C++

I wrote a piece of code and used map and vector but it shows me something I can't get. I'll be thankful if someone help me in this way and correct my code or give me some hints.
The code is:
// For each node in N, calculate the reachability, i.e., the
// number of nodes in N2 which are not yet covered by at
// least one node in the MPR set, and which are reachable
// through this 1-hop neighbor
std::map<int, std::vector<const NeighborTuple *> > reachability;
std::set<int> rs;
for (NeighborSet::iterator it = N.begin(); it != N.end(); it++)
{
NeighborTuple const &nb_tuple = *it;
int r = 0;
for (TwoHopNeighborSet::iterator it2 = N2.begin (); it2 != N2.end (); it2++)
{
TwoHopNeighborTuple const &nb2hop_tuple = *it2;
if (nb_tuple.neighborMainAddr == nb2hop_tuple.neighborMainAddr)
r++;
}
rs.insert (r);
reachability[r].push_back (&nb_tuple);
}
/*******************************************************************************/
//for keepping exposition of a node
std::map<Vector, std::vector<const NeighborTuple *> > position;
std::set<Vector> pos;
for (NeighborSet::iterator it = N.begin(); it != N.end(); it++)
{
NeighborTuple nb_tuple = *it;
Vector exposition;
pos.insert (exposition);
position[exposition].push_back (&nb_tuple);
}
and the errors are for this line: position[exposition].push_back (&nb_tuple);
and the errors are:
/usr/include/c++/4.1.2/bits/stl_function.h: In member function ‘bool std::less<_
Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = ns3::Vector3D]’:
/usr/include/c++/4.1.2/bits/stl_map.h:347: instantiated from ‘_Tp& std::map<_K
ey, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = ns3::Vector3D, _Tp = std::vector<const ns3::olsr::NeighborTuple*, std::allocator<const ns3::olsr::NeighborTuple*> >, _Compare = std::less<ns3::Vector3D>, _Alloc = std::allocator<std::pair<const ns3::Vector3D, std::vector<const ns3::olsr::NeighborTuple*, std::allocator<const ns3::olsr::NeighborTuple*> > > >]’
../src/routing/olsr/olsr-routing-protocol.cc:853: instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:227: error: no match for ‘operator<’ in ‘__x < __y’
debug/ns3/ipv6-address.h:432: note: candidates are: bool ns3::operator<(const ns3::Ipv6Address&, const ns3::Ipv6Address&)
debug/ns3/nstime.h:475: note: bool ns3::operator<(const ns3::Time&, const ns3::Time&)
debug/ns3/ipv4-address.h:305: note: bool ns3::operator<(const ns3::Ipv4Address&, const ns3::Ipv4Address&)
debug/ns3/address.h:231: note: bool ns3::operator<(const ns3::Address&, const ns3::Address&)
debug/ns3/type-id.h:376: note: bool ns3::operator<(ns3::TypeId, ns3::TypeId)
Thanks in advance.
Bahar
std::map is a sorted container of pairs. As such, keys in the map must have operator <() defined. Make sure Vector has the less-than operator defined.
For example:
class Vector {
int len, ang;
friend bool operator<(const Vector&, const Vector&);
};
bool operator<(const Vector& v1, const Vector& v2)
{
return true_if_v1_is_less_than_v2(); // you define what "less than" means
}
Of course, there other ways to do this. You may make operator< a member function. Or you may have the two member data public and the operator a non-member, non-friend function. Or you may define operator< in an anonymous namespace, to enhance information hiding. Or you may use a comparator other than operator<.
You declared the position object as followed : std::map<Vector, std::vector<const NeighborTuple *> > position;
And you are trying to push NeighborTuple * inside...
Try using const NeighborTuple *
I notice that you seem to have a pushback line that compiles and a line that does not.
The difference might be that you have a const in the first case
NeighborTuple const &nb_tuple = *it;

find() problems

I've got an error while using find() function. Here is the code:
#include <iostream>
#include <map>
#define N 100000
using namespace std;
int main (int argc, char * const argv[]) {
map<int,int> m;
for (int i=0; i<N; i++) m[i]=i;
find(m.begin(), m.end(), 5);
return 0;
}
I'm getting an compiller error:
error: no match for 'operator==' in '__first. __gnu_debug::_Safe_iterator<_Iterator, _Sequence>::operator* [with _Iterator = std::_Rb_tree_iterator<std::pair<const int, int> >, _Sequence = __gnu_debug_def::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >]() == __val'
Including 'algorithm' nothing changes. Compiling in VS2008 shows similar error.
I know about m.find(), but I realy need to use find() too.
Thanks a lot for your assistance!
P.S. Actualy, the task is to compare speed of m.find(5) and find(m.begin(), m.end(), 5), so I need to make both of them work properly.
begin() and end() on all STL containers provide access to elements of those collections. Type of those elements is known as value_type of the container. For std::map<Key, Value>, its value_type is std::pair<Key, Value>. Therefore, your find function is trying to find a pair<int, int> which is equal to 5. Since there's no operator== defined to compare pair<int, int> and int, you get the error.
The correct way to do this (so long as you want to avoid member find()) is to use std::find_if:
template <class First>
struct first_equal
{
const First value;
first_equal(const First& value)
: value(value)
{
}
template <class Second>
bool operator() (const std::pair<First, Second>& pair) const
{
return pair.first == value;
}
};
...
find_if(m.begin(), m.end(), first_equal<int>(5));
You could also overload operator== for pair and int to do what you want, but it's a very hackish way (because it will affect all your code, and because such a comparison has no meaning in general).
find() requires a parameter that can be compared to *iterator. For your map, this will be pair<int,int>. You'll need to create a dummy pair, plus a comparison functor to compare the pairs.
Just use m.find(5)