在STL算法中使用struct成员

时间:2009-09-19 04:59:28

标签: c++ stl

#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;
}

我想知道如何使用STL算法获得与上面的for循环相同的结果?我已经尝试了一些使用for_each的东西,但是无法让它工作。

1 个答案:

答案 0 :(得分:7)

您不会使用std::for_each,而是std::transform(您正在将某个点转换为单个数字。)

例如:

#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;
}

因为您知道要转换的容器的大小,所以您可能会从以下方面获得轻微的性能提升。我也发现它更具可读性:

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

boost::lambda以及boost::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;
}

无需在其他地方指定功能。这使代码保持靠近调用站点,并且通常可以提高可读性。

在C ++ 0x中,它只是:

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

(据我所知,无论如何)