3

I'm trying to copy a map (i want to copy _citymap to _the_cities) using copy_if. this is my code:

std::map <string, pair <float,float>> _citymap;

copy_if(_citymap.begin(),_citymap.end(),
std::inserter(_the_cities,_the_cities.end()),
[this](decltype(_citymap)::value_type const &kv_pair) {
return (Manhattan_Distance(kv_pair.second));});

the function Manhattan_Distance is a bool function:

bool Search:: Manhattan_Distance (const pair <float, float> &the_pair) { return (_radius >= fabs(_citymap[_city].first-the_pair.first) +
fabs(_citymap[_city].second-the_pair.second)); }

The errors I get:

  1. error C3499: a lambda that has been specified to have a void return type cannot return a value

  2. IntelliSense: class "std::map (std::string, std::pair(float, float), std::less(std::string), std::allocator(std::pair(const std::string, std::pair(float, float>>>>" has no member "second"

  3. error C2039: 'second' : is not a member of 'std::map<_Kty,_Ty>'

thank you for your help!

1 Answer 1

4

copy_if requires the predicate take an argument of the type obtained by dereferencing the iterator (or something implicitly convertible from that type), in this case map::value_type, which is an std::pair<const KeyType, ValueType>. Change your lambda expression to:

[](decltype(_citymap)::value_type const& kv_pair) {
    return Manhattan_Distance(kv_pair.second);
}

I'm assuming your Manhattan_Distance() function looks like this:

bool Manhattan_Distance(std::pair<float, float> const& the_pair)
{ return /* ... */; }

With C++14 you can even leave the decltype(...) part out of the argument type:

[](auto const& kv_pair) {
    return Manhattan_Distance(kv_pair.second);
}
Sign up to request clarification or add additional context in comments.

7 Comments

@user3617423 Is Manhattan_Distance itself a lambda? If so, you need to capture it - the copy_if predicate should be [Manhattan_Distance](decltype(...).... If that's not the case, the please update the question showing the definition of Manhattan_Distance, and where it's defined in relation to the call to copy_if.
Thank you for your help. this is the function: bool Search:: Manhattan_Distance (const pair <float, float> &the_pair) { return (_radius >= fabs(_citymap[_city].first-the_pair.first) + fabs(_citymap[_city].second-the_pair.second)); }
@user3617423 It looks like Manhattan_Distance is a non-static member function of the Search class. Assuming the copy_if call is happening in another member of the same class, you need to capture the this pointer to be able to invoke other member functions. So your lambda should be [this](decltype(...)...
@user3617423 Is it just Intellisense errors, or are you getting compiler errors too (error messages with Cxxx identifiers)? If it's the former you can ignore them. Otherwise you're going to have to post more code, and the error messages from the compiler. Indicate what lines the errors are occurring at.
@user3617423 Try explicitly specifying the return type for the lambda [this](decltype(_citymap)::value_type const &kv_pair) -> bool {...}. I don't know the cause of the error C2039 is because I don't know what line it's happening on.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.