简体   繁体   English

在STL算法中使用结构成员

[英]Using a struct member in STL algorithms

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

struct Point
{
    int x;
    int y;
    Point(int x, int y) :
        x(x),
        y(y)
    {}
};

int main()
{
    vector<Point> points;
    points.push_back(Point(1, 2));
    points.push_back(Point(4, 6));

    vector<int> xs;

    for(vector<Point>::iterator it = points.begin();
        it != points.end();
        ++it)
    {
        xs.push_back(it->x);
    }

    copy(xs.begin(), xs.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    return 0;
}

I'm wondering how I would achieve the same result as the for loop above using an STL algorithm? 我想知道如何使用STL算法获得与上述for循环相同的结果? I've tried a few things using for_each, but wasn't able to get it to work. 我已经尝试过使用for_each进行一些操作,但是无法使其正常工作。

You wouldn't use std::for_each , but rather std::transform (you're transforming a point into a single number.) 您不会使用std::for_each ,而是使用std::for_each std::transform (将点转换为单个数字。)

For example: 例如:

#include <algorithm> // transform resides here
#include <iostream>
#include <iterator>
#include <vector>

struct Point
{
    int x;
    int y;

    Point(int x, int y) :
    x(x),
    y(y)
    {
    }
};

int point_to_int(const Point& p)
{
    return p.x;
}

int main()
{
    std::vector<Point> points;
    points.push_back(Point(1, 2));
    points.push_back(Point(4, 6));

    std::vector<int> xs;
    std::transform(points.begin(), points.end(),
        std::back_inserter(xs), point_to_int);

    std::copy(xs.begin(), xs.end(),
        std::ostream_iterator<int>(std::cout, " "));

    std::cout << std::endl;

    return 0;
}

Because you know the size of the container you'll be transforming, you might get a slight performance improvement from the following. 因为您知道将要转换的容器的大小,所以您可能会从以下内容获得稍微的性能改进。 I also find it more readable: 我还发现它更具可读性:

std::vector<int> xs;
xs.reserve(points.size());
std::transform(points.begin(), points.end(),
        std::back_inserter(xs), point_to_int);

And with boost::lambda along with boost::bind : 并带有boost::lambdaboost::bind

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>

struct Point
{
    int x;
    int y;

    Point(int x, int y) :
    x(x),
    y(y)
    {
    }
};

int main()
{
    using namespace boost;

    std::vector<Point> points;
    points.push_back(Point(1, 2));
    points.push_back(Point(4, 6));

    std::vector<int> xs;
    xs.reserve(points.size());
    std::transform(points.begin(), points.end(),
        std::back_inserter(xs), bind(&Point::x, lambda::_1));

    std::copy(xs.begin(), xs.end(),
        std::ostream_iterator<int>(std::cout, " "));

    std::cout << std::endl;

    return 0;
}

Removes the need to specify a function elsewhere. 无需在其他地方指定函数。 This keeps the code close to the calling site, and generally improves readability. 这样可以使代码保持靠近调用站点的位置,并通常提高了可读性。

In C++0x, it will simply be: 在C ++ 0x中,它将简单地是:

std::transform(points.begin(), points.end(),
    std::back_inserter(xs), [](const Point& p){ return p.x; } );

(To the best of my knowledge, anyway) (据我所知)

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

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