如何访问Vector< Point(x,y)> vec()元素? [英] How to access to a Vector<Point(x,y)> vec() elements?

查看:346
本文介绍了如何访问Vector< Point(x,y)> vec()元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我声明了这样的矢量:



Hi I've declare a vector like this:

Vector<Point> vec(3);
for(Vector<Point>::iterator i=vec.begin(); i<vec.end(); i++){}





我想读取每个点的(x,y)元素,我该怎么做?



And I want to read the (x,y) elements of each point, How can I do it?

推荐答案

假设x和y可以作为 Point 类型的成员访问,

Assuming x and y are accessible as members of your Point type,
for (Vector<point>::iterator i = vec.begin(); i != vec.end(); ++i) // srsly, what's with the post-incr in ALL code??
{
    int x = i->x; // or whatever type it is
    // do stuff
}
</point>





如果你的编译器支持基于C ++ 11范围的,那么这可能会变成



If your compiler supports the C++11 range-based for, this could become

for (Point const &p: vec) // or non-const, or by value, whatever
{
    // do stuff with p.x and p.y
}





或者使用 < algorithm> 和C ++ 11 lambda:



Or using <algorithm> and C++11 lambda's:

std::for_each(vec.begin(), vec.end(), [&]
(Point const &p) 
{
    // do stuff with p.x and p.y
});


这篇关于如何访问Vector&lt; Point(x,y)&gt; vec()元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆