简体   繁体   English

如何正确使用 std::transform 和 std::mem_fn?

[英]How to use std::transform and std::mem_fn correctly?

I have implemented a C++ program, which creates three different Person objects.我已经实现了一个 C++ 程序,它创建了三个不同的 Person 对象。 These objects are made shared and stored in a vector.这些对象被共享并存储在一个向量中。

The function getVector() takes a const std::vector<std::shared_ptr<Person>>& as input and returns a std::vector<Person*> .函数getVector()接受一个const std::vector<std::shared_ptr<Person>>&作为输入并返回一个std::vector<Person*> Each element in the result vector corresponds to the respective element in the input vector.结果向量中的每个元素对应于输入向量中的相应元素。

The function uses std::transform together with std::mem_fn .该函数将std::transformstd::mem_fn

The implementation of that function (and the corresponding call with std::cout 's) doesn't work.该函数的实现(以及与std::cout的相应调用)不起作用。 I think that I use std::transform with std::mem_fn in a wrong way (wrong arguments, etc.).我认为我以错误的方式(错误的参数等)将std::transformstd::mem_fn使用。

I don't know how to make the function getVector() working.我不知道如何使函数getVector()工作。

Here is my source code so far:到目前为止,这是我的源代码:

main.cpp主程序

#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

struct Person
{
    std::string first_name;
    std::string last_name;
    int age;

    Person* getPerson(const std::shared_ptr<Person>& person)
    {
        Person* p = person.get();
        return std::move(p);
    }

    // Overloaded << operator.
    friend std::ostream& operator<<(std::ostream& os, const Person& person)
    {
        os << person.first_name << ", " << person.last_name << ", " << person.age << "\n";
        return os;
    }

    friend std::string to_string(const Person& obj)
    {
        std::ostringstream ss;
        ss << obj;
        return ss.str();
    }
};

std::vector<Person*> getVector(const std::vector<std::shared_ptr<Person>>& people);

int main(void)
{
    Person person01;
    person01.first_name = "Ashley";
    person01.last_name = "Graham";
    person01.age = 23;

    Person person02;
    person02.first_name = "Ada";
    person02.last_name = "Wong";
    person02.age = 45;

    Person person03;
    person03.first_name = "Leon S.";
    person03.last_name = "Kennedy";
    person03.age = 26;

    std::shared_ptr<Person> ps01 = std::make_shared<Person>(person01);
    std::shared_ptr<Person> ps02 = std::make_shared<Person>(person02);
    std::shared_ptr<Person> ps03 = std::make_shared<Person>(person03);

    std::vector<std::shared_ptr<Person>> peoples;

    peoples.emplace_back(std::move(ps01));
    peoples.emplace_back(std::move(ps02));
    peoples.emplace_back(std::move(ps03));

    std::cout << "\nContent of input vector:\n";
    std::cout << "====================================\n";
    for (auto p : peoples)
    {
        std::cout << *p;
    }
    std::cout << "\n";

    std::cout << "Calling function 'getVector()'...\n";

    std::vector<Person*> result = getVector(peoples);

    std::cout << "\nContent of result-vector:\n";
    std::cout << "====================================\n";
    for (auto r : result)
    {
        std::cout << *r;
    }
    std::cout << "\n";

    return 0;
}

// std::transform with std::mem_fn.
std::vector<Person*> getVector(const std::vector<std::shared_ptr<Person>>& people)
{
    Person* person;
    std::shared_ptr<Person> p;
    std::vector<Person*> result;

    std::transform(people.begin(), people.end(), std::back_inserter(result), 
        std::bind1st(std::mem_fn(&Person::getPerson), &p));

    return result;
}

Here are the instructions for the g++ compiler:以下是 g++ 编译器的说明:

g++ -std=c++11 -Wall -Wextra main.cpp -o main

Just use a lambda.只需使用 lambda。 It makes the code a lot easier to understand and you can get rid of the GetPerson function.它使代码更容易理解,您可以摆脱GetPerson函数。 Doing so would make your transform call look like这样做会使您的转换调用看起来像

std::transform(people.begin(), 
               people.end(), 
               std::back_inserter(result), 
               [](const auto& elem){ return elem.get(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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