<operator missing when iterating through c++ map - c++

The following code does not want to compile. See the included error message.
Code:
#include <map>
#include <vector>
#include <iostream>
class MapHolder {
public:
std::map<std::vector<std::string>,MapHolder> m_map;
void walk_through_map() {
std::map<std::vector<std::string>,MapHolder>::iterator it;
for(it = m_map.begin(); it < m_map.end(); ++it) {
it->second.print();
}
}
void print() { std::cout << "hey" << std::endl; }
};
int
main(int argc, char *argv[])
{
MapHolder m;
m.walk_through_map();
}
Error:
$ g++ test.cc -O test
test.cc: In member function 'void MapHolder::walk_through_map()':
test.cc:12: error: no match for 'operator<' in 'it < ((MapHolder*)this)->MapHolder::m_map.std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Tp = MapHolder, _Compare = std::less<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _Alloc = std::allocator<std::pair<const std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, MapHolder> >]()'
I used this type of map and iterating process multiple times before. What's the problem here? How can it be solved.
(The code looks meaningless, but this is a reduced sample which is still supposed to work)

Use != instead of < in iterator comparison.

operator< is only available for random access iterators. Since std::maps are usually implemented using some sort of balanced tree, there is usually no fast way to find out whether one iterator points to an element before another one (though end is an exception).
I guess the reasoning behind this is that these mysterious compiler errors force you to think about your code again and implement operator< yourself if you find that this is the best way to solve your problem.

Related

insert in std::map is throwing error. The key is a std::pair and the value is a short

#include <iostream>
#include <map>
#include <utility>
int main()
{
std::pair<std::string, std::string> p;
std::map< std::pair<std::string, std::string>, short> m;
// p = std::make_pair("A", "a1");
m.insert(std::make_pair("A", "a1"), 10);
return 0;
}
This code is throwing the following error
maptest.cpp: In function ‘int main()’:
maptest.cpp:9: error: no matching function for call to
‘std::map<std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, short int,
std::less<std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >, std::allocator<std::pair<const
std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, short int> > >::insert(std::pair<const char*,
const char*>, int)’
I am trying to do a std map insertion. The kwy is a std pair and value is a short. But I am getting the above mentioned error.
What am I doing wrong here? Pleas help.
The insert function takes a pair. You need
m.insert(std::make_pair(std::make_pair("A", "a1"), 10));
Alternatively, you could use the emplace function:
m.emplace(std::make_pair("A", "a1"), 10);
As a side note, in programmers' vernacular, the word "throw" has a specific meaning relating to exceptions. In your case you are just getting a compilation error.
There is simply no insert method with key and value arguments i.e map<...>::insert(K key, V value). Instead it accepts key-value pair, so this code should work:
#include <iostream>
#include <map>
#include <utility>
int main()
{
std::pair<std::string, std::string> p;
std::map< std::pair<std::string, std::string>, short> m;
auto&& key = std::make_pair("A", "a1");
short value = 10;
auto&& key_value_pair = std::make_pair(key, value);
//Structured bindings are c++17
auto&&[IT, wasInserted] = m.insert(key_value_pair);
return 0;
}
I would recommend using C++17 method try_emplace which has key and value arguments:
auto&&[IT, wasInserted] = m.try_emplace(key, value);

Error in a function used to find the position of an element in a vector of string (c++)

I have the following function that works fine that I use to find the position of a string in a string vector:
int FindIndexString(vector <string> *Names, string *name)
{
auto it = find(Names->begin(), Names->end(), name->c_str());
if (it == Names->end())
{
error("FindIndexString: %s not found",name->c_str());
}else{
auto index = distance(Names->begin(), it);
return((int)index);
}
}
I need to use this function in a R program that complains about the "auto" specifier and I cannot compile with C++11.
I changed the function to
int FindIndexString(vector <string> *Names, string *name)
{
vector<string>::iterator it;
it = find(Names->begin(), Names->end(), name->c_str());
int pos = (int)distance(Names->begin(), it);
if(it==Names->end())
{
error("FindIndexString: %s not found",name->c_str());;
}else{
return(pos);
}
}
but it doesn't work and I get the following error
In function 'int FindIndexString(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*, std::string*)':
F_Utils.cpp:92: error: no matching function for call to 'find(__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, const char*)'
Any help?
You need to #include <algorithm> for std::find

RHEL 6.0 Two Similar Pieces of Code: One Compiles One Does Not

Can someone explain why the top piece of code will not compile and the bottom one will?
#include <iterator>
#include <iostream>
#include <set>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream testFile;
testFile.open("opengl_functions", ios::in);
set<string> myset(istreambuf_iterator<string>(testFile), istreambuf_iterator<string>());
set<string>::iterator it;
for (it = myset.begin(); it != myset.end(); ++it ) {
}
}
//using namespace std;
//
//int main ()
//{
// int myints[] = {75,23,65,42,13};
// set<int> myset (myints,myints+5);
// set<int>::iterator it;
// cout << "myset contains:";
// for ( it=myset.begin() ; it != myset.end(); it++ )
// cout << " " << *it;
// cout << endl;
// return 0;
//}
--
[mehoggan#hogganz400 opengl_parser]$ make
g++ -o parser -Wall ./parser.cpp
./parser.cpp: In function ‘int main(int, char**)’:
./parser.cpp:17: error: request for member ‘begin’ in ‘myset’, which is of non-class type ‘std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >(std::istreambuf_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::char_traits<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::istreambuf_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::char_traits<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > (*)())’
./parser.cpp:17: error: request for member ‘end’ in ‘myset’, which is of non-class type ‘std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >(std::istreambuf_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::char_traits<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::istreambuf_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::char_traits<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > (*)())’
make: *** [parser] Error 1
Your compiler believes that myset is a function declaration. Look up "the most vexing parse". I always run into it with istream iterators, so I always declare them beforehand. As a side benefit, I find it much easier to read:
std::istreambuf_iterator<string> begin(testFile), end;
std::set<std::string> myset(begin, end);
However, I don't believe that will compile either, but for a different reason. istreambuf_iterator can only be templated on character types. You'll want to use an istream_iterator instead.
std::istream_iterator<string> begin(testFile), end;
std::set<std::string> myset(begin, end);
It is interpreting your declaration of "myset" as a function.
Also: for strings, you need istream_iterator, not istreambuf_iterator:
set<string> myset((istream_iterator<string>(testFile)), (istream_iterator<string>()) );

error: '__pred' cannot be used as a function

I can't find what I'm doing wrong here. The function eta does what I ask but when I use it in the loop I get the attached error.
bool eta(map<string, TLorentzVector> map_jets, string jet){
return( fabs(map_jets[jet].PseudoRapidity()) > 2.5 );
}
and then
vector<pair<string,double> > jets_pt( vec_jets.size() );
for( vector<pair<string,double> >::iterator it = jets_pt.begin(); it != jets_pt.end(); ++it)
jets_pt.erase(remove_if(jets_pt.begin(),jets_pt.end(),eta(map_jets,it1->first)),jets_pt.end);
I get the error
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator, _OutputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _OutputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1291: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1216: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:338: instantiated from '_InputIterator std::find_if(_InputIterator, _InputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1287: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:260: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:264: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:268: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:272: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:280: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:284: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:288: error: '__pred' cannot be used as a function
mv: cannot stat `/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/srt_dep_tmp.27294': No such file or directory
gmake[2]: *** [/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/libAna-shared/Functions.o] Error 1
gmake[1]: *** [src.lib] Error 2
gmake: *** [Ana.all] Error 2
First off, the only valid signature for remove_if takes a single function as the predicate argument:
jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), eta),
jets_pt.end());
This means that eta must be a function returning bool and taking precisely one argument whose type is the value type of the container:
bool eta(const std::pair<string, double> & p)
{
// do something useful with p
}
If this doesn't fit your bill because you need additional state information in the predicate, then you need to make it a function object:
struct Eta
{
const std::map<string, TLorentzVector> & map_jets;
Eta(const std::map<string, TLorentzVector> & m) : map_jets(m) { }
bool operator()(const std::pair<string, double> & p) const
{
std::map<string, TLorentzVector>::const_iterator it = map_jets.find(p->second);
return it == map_jets.end() ?
false :
std::fabs(it->second.PseudoRapidity()) > 2.5;
}
};
Now you have to use remove_if with an instance of Eta:
jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), Eta(map_jets)),
jets_pt.end()); // ^^^^^^^^^^^^^
Note that your use of the for loop is extremely suspicious; you should double-check that.
std::remove_if takes a value of the contained type (bool pred (container::value_type), or a functor which overloads operator() appropriately).
For std::map, you have to do it like explained here remove_if equivalent for std::map .
You're passing a bool (the results of calling eta) to remove_if as
the third argument. You need to pass it a predicate: a pointer to a
function taking a single argument and returning something which can be
converted to a bool, or a functional object. In the error messages,
__pred is your third argument; remove_if calls it here with a
pair<string, double>, and expects a bool in return. You have to
provide something which can be called like this. (You might want to
look into boost::bind.)
And BTW: passing a map by value to eta might not be a good idea.
It's going to slow things down considerably if the map is big.

Shallow/deep copy of std::map

How would I best implement these? I thought of something like this:
using namespace std;
shape_container
shape_container::clone_deep () const
{
shape_container* ptr = new shape_container();
copy( data.begin(), data.end(), (*ptr).begin() );
return *ptr;
}
shape_container
shape_container::clone_shallow () const
{
return *( new shape_container(*this) );
}
The member data is defined as follows:
std::map<std::string, shape*> data;
This doesn't work, unfortunately. Here's the compiler errors, I don't really understand them:
g++ -Wall -O2 -pedantic -I../../UnitTest++/src/ -I./libfglwin/include/ -I. -c shape_container.cpp -o shape_container.o
/usr/include/c++/4.2.1/bits/stl_pair.h: In member function ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>& std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>::operator=(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>&)’:
/usr/include/c++/4.2.1/bits/stl_pair.h:69: instantiated from ‘static _OI std::__copy<<anonymous>, <template-parameter-1-2> >::copy(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, bool <anonymous> = false, <template-parameter-1-2> = std::bidirectional_iterator_tag]’
/usr/include/c++/4.2.1/bits/stl_algobase.h:315: instantiated from ‘_OI std::__copy_aux(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >]’
/usr/include/c++/4.2.1/bits/stl_algobase.h:340: instantiated from ‘static _OI std::__copy_normal<<anonymous>, <anonymous> >::__copy_n(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, bool <anonymous> = false, bool <anonymous> = false]’
/usr/include/c++/4.2.1/bits/stl_algobase.h:401: instantiated from ‘_OutputIterator std::copy(_InputIterator, _InputIterator, _OutputIterator) [with _InputIterator = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OutputIterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >]’
shape_container.cpp:70: instantiated from here
/usr/include/c++/4.2.1/bits/stl_pair.h:69: error: non-static const member ‘const std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>::first’, can't use default assignment operator
/usr/include/c++/4.2.1/bits/stl_algobase.h: In static member function ‘static _OI std::__copy<<anonymous>, <template-parameter-1-2> >::copy(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, bool <anonymous> = false, <template-parameter-1-2> = std::bidirectional_iterator_tag]’:
/usr/include/c++/4.2.1/bits/stl_algobase.h:268: note: synthesized method ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>& std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>::operator=(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>&)’ first required here
Somehow this looks unnecessarily complicated to me. Is that
true and can I make it better?
BTW, I have clone() methods
in the classes I derived from shape. Perhaps I can use them
for the clone_deep method? Are they ok? They look something
like this:
class shape
{
public:
/* Many methods. */
virtual shape* clone () const = 0;
protected:
colorRGB color_;
std::string name_;
};
class triangle2d : public shape
{
public:
/* Many methods. */
triangle2d* clone() const;
private:
point3d a_, b_, c_;
};
triangle2d*
triangle2d::clone() const
{
return new triangle2d(*this);
}
Usually a clone function would return a pointer to a new instance. What you are returning is an object by value which is copy constructed from a dynamically allocated isntance that is then leaked.
If you want to return by value then you should not use new.
E.g.
shape_container shape_container::clone_shallow () const
{
return *this;
}
If the data member is just a std::map instance, then it will be copied as part of your shallow clone in any case so there is no need to do the std::copy in the deep clone case, it's not trying to do anything different.
If you wanted to do a std::copy of a map you would need to use a std::insert_iterator.
I think that it might be easier to do a clone of each shape after the fact, though.
e.g.
shape_container shape_container::clone_deep() const
{
shape_container ret(*this);
for (std::map<std::string, shape*>::iterator i = ret.data.begin(); i != ret.data.end(); ++i)
{
i->second = i->second->clone();
}
return ret;
}
First of all your example leaks memory because you new a shape_container in your methods but then it gets copied out through the return value. You should be returning pointers as with your shape example.
The compiler errors look to be related in some way to the copying since it's complaining it can't generate an assignment operator for you. Again, try using pointers and that issue should go away.
If you do deep copy of map then you have to a new create map with all element with deep copy.
Think about reference counting approach , it will be better approach.
One option is to wrap your shape type in a type that performs a deep
copy of the object:
class shape_deep_copy_wrapper {
// ...
public:
shape_deep_copy_wrapper (shape * shape)
: m_my_shape (shape)
{
}
shape_deep_copy_wrapper (shape_deep_copy_wrapper const & rhs)
: m_my_shape (rhs.m_my_shape.deep_copy ())
{
}
// ...
private:
shape * m_my_shape;
};
Then construct a map with this type:
typedef std :: map < shape_deep_copy_wrapper , ... > DeepCopy ;
typedef std :: map < shape* , ... > ShallowCopy ;