简体   繁体   中英

What libraries do I need to use std::placeholders?

Currently I am trying to generate combinations and I am using the following code:

#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>

template<class RandIt, class Compare>
bool next_combination(RandIt first, RandIt mid, RandIt last)
{
    std::sort(mid, last, std::bind(std::less<int>(), std::placeholders::_2
                                            , std::placeholders::_1));
    return std::next_permutation(first, last, std::less<int>());
}

Using g++ it fails to compile saying:

next_combo.cpp: In function ‘bool next_combination(RandIt, RandIt, RandIt)’:
next_combo.cpp:10: error: ‘bind’ is not a member of ‘std’
next_combo.cpp:10: error: ‘std::placeholders’ has not been declared
next_combo.cpp:11: error: ‘std::placeholders’ has not been declared

I thought that std::placeholders were declared in functional, but now I'm confused. Should I just use boost?

Also the rest of the project is using c++0x, so is there a better way to write this using c++0x features?

Any help is greatly appreciated :)

Off course there is a better way. Use std::greater_equal instead of doing the above. Check here what else there is in the functional.

Forgot to say - to use placeholder, you need to enable c++0x features.

why don't you use lambdas instead of cumbersome bind? if you use C++0x, it has sense to write

std::sort(v.begin(), v.end(), [](int l, int r)->bool { return l > r; });

instead. Note, that you don't need to specify the return type explicitly there. I wrote this way just to make it clearer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM