English 中文(简体)
我怎么做:make_pair weable with :bind*?
原标题:How can I make std::make_pair usable with std::bind*?
最佳回答

您需要<代码>std:ptr_fun,使一个便捷功能点变成一个可调用的双功能物体(或一个非功能物体,如果你通过单一功能的话):

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

int main() {
    std::vector<int> intvec;
    intvec.push_back(0);
    intvec.push_back(1);
    std::vector<std::pair<int,bool> > pairvec(intvec.size());
    std::transform(
        intvec.begin(),
        intvec.end(),
        pairvec.begin(),
        // this is the significant line
        std::bind2nd(std::ptr_fun(std::make_pair<int, bool>), true)
    );
    std::cout << pairvec[1].first << " " << pairvec[1].second << "
";
}

www.un.org/Depts/DGACM/index_spanish.htm

template <class Arg1, class Arg2, class Result>
pointer_to_binary_function<Arg1,Arg2,Result>
ptr_fun(Result (*)(Arg1,Arg2));

简略版本:

template <class Arg, class Result>
pointer_to_unary_function<Arg,Result>
ptr_fun(Result (*)(Arg));
问题回答

Use lambda don t need to use bind adaptor.

    std::vector<int> start1 = list_of(1)(2)(3)(4)(5);
    std::vector<int> start2 = list_of(10)(20)(30)(40)(50);
    std::vector<Pair> w_vecofpair; // vector of pair
    w_vofpair.reserve(start1.size()); 
    // create pair using lambda
    std::transform( std::begin(start1), std::end(start1), std::begin(start2), // ranges 
        std::back_inserter(w_vecofpair),  // result 
        [](int a,int b) { return std::make_pair(a,b);}); // pair creator

    for (auto& pairInt : w_vecofpair)
    {
        std::cout << pairInt << "
"; 
    }

    // bind 2nd arg to some value, say 2
    std::transform( std::begin(start1), std::end(start1), std::begin(start2), 
        std::back_inserter(w_vecofpair), [](int a, int b) { return std::make_pair(a,2);});

    for (auto& second : w_vecofpair | map_values)
    {
        std::cout << "The second value of our bind 2nd is: " << second << "
";
        assert(second==2);
    }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签