boost::arrayの勉強

boost::arrayは配列をイテレータで操作できる事を知ったので、勉強してみた。

題材はウェイポイントを管理するオブジェクトについて。

ソースコード

https://gist.github.com/a321013519a4f98197c7

#include <iostream>
#include <boost/array.hpp>

class Point {
public:
    Point(double _x, double _y): x(_x), y(_y) {}
    double x;
    double y;
};

int main()
{
    typedef boost::array<Point, 5> Waypoints;
    Waypoints wps = {{
        Point(  0, 0),
        Point(100, 0),
        Point(100, 100),
        Point(  0, 100),
        Point(  0, 0)
    }};

    for(Waypoints::iterator pt = wps.begin(); pt != wps.end(); pt++) {
        using namespace std;
        cout << (*pt).x << ", " << (*pt).y << endl;
    }
}

実行結果

0, 0
100, 0
100, 100
0, 100
0, 0

ソースコードについて

座標の表示部分で

cout << *pt.x << ", " << *pt.y << endl;

というふうに書きたかったけど、以下のようなコンパイルエラーが出た。

main.cpp: In function ‘int main()’:
main.cpp:24: error: request for member ‘x’ in ‘pt’, which is of non-class type ‘Point*’
main.cpp:24: error: request for member ‘y’ in ‘pt’, which is of non-class type ‘Point*’

だから、*ptをカッコでくくった。

追記

pt->xでメンバ変数にアクセスできた。

参考資料

boost::arrayについて知れたhttps://sites.google.com/site/boostjp/tips/array