g++ doesn't like:
vector<int> x;
x += 1,2,3,4,5;
vector<string> y(x.size());
transform(x.begin(), x.end(), y.begin(), lexical_cast<string>);
The error message is:
error: no matching function for call to 'transform(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __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> > > > >, <unresolved overloaded function type>)'
Which clearly indicates there is an issue with lexical_cast as the last argument to transform... Is there a way to avoid writing a function object that wraps lexical_cast?
Thanks!
This is untested, but you could try:
transform(x.begin(), x.end(), y.begin(), lexical_cast<string, int>);
lexical_cast is a template with two template parameters. Normally the second one is deduced from type deduction from the argument, but you aren't providing an argument, so you need to explicitly specify it.
Related
I am trying out a project in C++ and I am trying to enter a value from an integer type vector to a pair type vector.
I have tried emplace_back and push_back but it doesn't work. Most probably due to the pair typed vector.
Note: I am using boost libraries, not sure if that is helpful.
typedef unsigned int objecttype;
typedef string operationtype;
typedef unsigned int checkob;
typedef pair<objecttype, operationtype> objectops;
vector<vector<unsigned int>> parameters;
//lines returning values to “parameters”
vector<checkob> checkParams = parameters.at(0); // works fine
vector<objectops> objectParams = parameters.at(1); // below error
The error message:
error test.cpp:273:30: error: no viable conversion from 'vector<unsigned int, allocator<unsigned int>>' to 'vector<model:: objectops, allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >>>'
stl_vector.h:326:7: note: candidate constructor not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > >::value_type' (aka 'std::vector<unsigned int, std::allocator<unsigned int> >') to 'const std::vector<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > &' for 1st argument
stl_vector.h:344:7: note: candidate constructor not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > >::value_type' (aka 'std::vector<unsigned int, std::allocator<unsigned int> >') to 'std::vector<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > &&' for 1st argument
stl_vector.h:383:7: note: candidate constructor not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > >::value_type' (aka 'std::vector<unsigned int, std::allocator<unsigned int> >') to 'initializer_list<std::vector<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::value_type>' (aka 'initializer_list<std::pair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >') for 1st argument
You are trying to assign std::vector<unsigned int> to std::pair<unsigned int, std::string>, which is obviously not possible.
You can either
assign a vector of std::pair<unsigned int, std::string> to
objectParams
or a list initialization using a pair of the element of the vector
of vector of unsigned int egers(i.e. unsigned int) of parameters
and a std::string.
You probably mean this:
vector<objectops> objectParams ={ { parameters.at(0).at(0), "string"} };
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ std::pair<objecttype, operationtype>
// ^^ ^^ for std::vector<objectops>
I been maintaining some statistics in a map, the below are my typedefs to create the map
typedef Time TOTALTIME,MINM,MAXM,AVRG;
typedef std::map<std::string,std::map<status,int>> RequestStatus;//status is an enum
typedef std::tuple<int,TOTALTIME,MINM,MAXM,AVRG,RequestStatus> Attributes ;
typedef std::map<std::string,Attributes> requestStatistics ;
requestStatistics sampleStruct //my final map
And i have been initializing like
sampleStruct.insert(make_pair(someskey,std::make_tuple(1,TOTALTIME(),MINM(),MAXM(),AVRG(),sampleStatus[functionName][status::READY]=1)));
other way I have tried
sampleStruct[somekey]=std::make_tuple(1,TOTALTIME(),MINM(),MAXM(),AVRG(),sampleStatus[functionName][status::READY]=1)
Both doesn't pass the compiler always endup in compile error, lemme put out some compile error I,ve got
std::map<endstor::Data::statistics::status, int, std::less<endstor::Data::statistics::status>, std::allocator<std::pair<const endstor::Data::statistics::status, int> > >,
std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<endstor::Data::statistics::status, int, std::less<endstor::Data::statistics::status>, std::allocator<std::pair<const endstor::Data::statistics::status, int> > > > > > > > >]
insert(const_iterator __position, _Pair&& __x)
1)was the structure I've created is valid?were the assignments are ok?
The last parameter in your insert or make_tuple calls is sampleStatus[functionName][status::READY]=1, which is an int, while the tuple is expecting a RequestStatus. This type difference would be the cause of your error.
I have the following class:
class Foo
{
public:
explicit Foo(std::vector<std::string>& functionCalls)
{
}
};
typedef boost::shared_ptr<Foo> FooPtr;
Which I try to use like this:
std::vector<std::string> functionCalls;
FooPtr foo = boost::make_shared<Foo>(functionCalls);
I compiles fine in VS20012 but it wont compile in gcc 4.3.4.
Here's the compiler error:
boost/boost_1_54_0/boost/smart_ptr/make_shared_object.hpp: In function 'typename boost::detail::sp_if_not_array<T>::type boost::make_shared(const A1&) [with T = Foo, A1 = 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> > > >]':
main.cc:39: instantiated from here
boost/boost_1_54_0/boost/smart_ptr/make_shared_object.hpp:711: error: no matching function for call to 'Foo::Foo(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> > > >&)'
Foo.h:23: note: candidates are: Foo::Foo(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> > > >&)
Foo.h:21: note: Foo::Foo(const Foo&)
If I change the construct of Foo to take a const then it compiles fine. However, I need to pass by reference. What do you think this issue is?
As documented here, without C++11 support make_shared can only take its arguments by const reference.
With C++11 support, it can take any reference types, and forward them to the constructor. Presumably that's what VS is doing.
As mentioned in the documentation, you can pass a non-const reference by wrapping it in boost::ref:
FooPtr foo = boost::make_shared<Foo>(boost::ref(functionCalls));
I want to use boost::bind to create a boost::function inserting a new key-value pair into a boost::unoredered_map but I got few compilation errors.
typedef boost::unordered_map<
std::string, std::string > dict_type;
inline void insert( const std::string& key, const std::string& value ){
typedef std::pair<dict_type::iterator, bool> out_type;
dict_type::value_type to_insert(key,value);
boost::function<void()> f = boost::bind<out_type>(
&dict_type::insert
,obj_
,boost::cref(to_insert)
);
}
The error below looks like bind cannot find the right overload for unordered_map::insert. In this case I specify exactly the right overload but this time it doesn't work. Do you know what it is?
../include2/llve_clorder_id.h:32: error: no matching function for call to
'bind(<unresolved overloaded function type>,
boost::unordered_map<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, boost::hash<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::equal_to<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > > >&, const
boost::reference_wrapper<const std::pair<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >)'
The problem is that boost::unordered_map contains more than one insert, so &dict_type::insert is ambiguous. The simplest solution is to define a function to call the correct overload:
void insert(dict_type & dict, dict_type::value_type const & value) {
dict.insert(value);
}
boost::function<void()> f = boost::bind(
insert
,boost::ref(obj_)
,boost::cref(to_insert)
);
or you could specify the overload explicitly:
boost::function<void()> f = boost::bind(
static_cast<out_type (dict_type::*)(dict_type::value_type const &)>(&dict_type::insert)
,obj_
,boost::cref(to_insert)
);
In C++11, you can avoid the problem with a lambda:
std::function<void()> f = [&](){obj_.insert(to_insert);};
http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#Troubleshooting suggests that you can sometimes work around problems with overloaded functions by casting the pointer-to-member-function to the desired type. Using a temporary variable to stop it becoming completely unreadable, it would look like:
typedef std::pair<typename dict_type::iterator, bool> out_type;
typename dict_type::value_type to_insert(key,value);
out_type (dict_type::*ins) (typename dict_type::value_type const&) const = &dict_type::insert;
boost::function<void()> f = boost::bind(
ins
,obj_
,boost::cref(to_insert)
);
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.