[英]Extracting Values from vectors
我正在嘗試從向量中提取一組點並對其進行計算。 點像
std::vector<Vector> m_points1;
m_points1.push_back(Vector( -4.0, 8.0));
m_points1.push_back(Vector( -1.0, -7.0));
m_points1.push_back(Vector( 0.0, -8.0));
m_points1.push_back(Vector( 2.0, -4.0));
m_points1.push_back(Vector( 3.0, 1.0));
我無法遍歷它,就像我以前使用數組並獲取所需的值一樣。 我的代碼如下所示:
for(std::vector<Vector>::iterator i=points.begin();i != points.end();i++)
{
k=i;
for(std::vector<Vector>::iterator j=points.begin();j != points.end();j++)
{
if(k==j)
{
continue;
}
else
{
//How to get values ???
}
}
}
如何提取積分? 並且只在x坐標上進行計算嗎?
您可以將基於范圍的for循環使用,並避免一起使用迭代器。
for (auto const& ptA : points)
{
for (auto const& ptB : points)
{
if (&ptA == &ptB)
{
continue;
}
else
{
// directly use ptA and ptB
}
}
}
如果您無法使用C ++ 11,則可以在else
塊中執行以下操作
else
{
Vector const& ptA = *i;
Vector const& ptB = *j;
}
根據Vector
的實現,您將執行類似
double width = ptA.x - ptB.x;
我想到了。 所以我只寫一個簡單的例子
for(unsigned int i = 0 ; i<points.size() ; i++)
{
temp = points[i](0);
}
要遍歷向量,它需要一個無符號的迭代器,並且只訪問其中一個元素,您必須使用(x)來訪問它,其中x確定哪個元素。 在這個問題中0是x坐標
else
{
// do wathever you want using
// i->x
// i->y
// j->x
// j->y
float dist = sqrt( (i->x-j->x)*(i->x-j->x)
+ (i->y-j->y)*(i->y-j->y) );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.