Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

bind

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 :)

like image 341
shuttle87 Avatar asked Dec 23 '10 07:12

shuttle87


2 Answers

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.

like image 109
BЈовић Avatar answered Sep 19 '22 01:09

BЈовић


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.

like image 28
Kirill Lykov Avatar answered Sep 21 '22 01:09

Kirill Lykov