I am fairly new to C++ and I have a question concerning passing elements by reference. I define the following struct
我对C ++很新,我有一个关于通过引用传递元素的问题。我定义了以下结构
struct Point
{
bool isOnEnvelop;
double x, y;
};
And the following function
以及以下功能
vector<Point> convex_hull(vector<Point> P)
{
int n = P.size(), k = 0;
vector<Point> H(2*n);
for (int i = 0; i < n; i++)
{
// IF SOMETHING ....
P[i].isOnEnvelop = true;
H[k] = P[i]
k++
}
H.resize(k-1);
return H;
}
With a main as follows
主要如下
main()
{
vector<Point> P;
// FILL P with Point and set Point.isOnEnvelop = false
vector<Point> H = convex_hull(P);
}
The algorithm is building the convex hull of a set of points in 2D space. I want to be able while looping on the elements of P to be able to detect if this point is on the convex hull (therefore checking P.at(i).isOnEnvelop) Unfortunately the state of the points are not changed after calling the function convex_hull. Should I pass the arguments by reference ? Or should I create P as vector of Point* ?
该算法正在构建2D空间中一组点的凸包。我希望能够在循环P的元素时能够检测此点是否在凸包上(因此检查P.at(i).isOnEnvelop)不幸的是,在调用函数后,点的状态不会改变convex_hull。我应该通过引用传递参数吗?或者我应该创建P作为Point *的向量?
Thanks a lot for your help
非常感谢你的帮助
Vincent
4 个解决方案
#1
4
The way you declared convex_hull
, the argument is copied:
您声明convex_hull的方式,参数被复制:
vector<Point> convex_hull(vector<Point> P){ ... }
This is very often a bad idea, since copying P
may be expensive. An alternative for that, if you want to ensure that P
itself is not modified, would be to use a const
reference.
这通常是个坏主意,因为复制P可能很昂贵。另一种方法是,如果要确保P本身不被修改,则使用const引用。
If you want to modify P
when calling the function (e.g. have modifications in main
, you must pass it by reference, and declare convex_hull
as follows (note the &
):
如果你想在调用函数时修改P(例如在main中有修改,你必须通过引用传递它,并按如下方式声明convex_hull(注意&):
vector<Point> convex_hull(vector<Point> &P){ ... }
#2
0
So after reading your comment, you want the changes to P
to be reflected in main
after calling convex_hull
, so you should just pass it by reference like so:
因此,在阅读完评论之后,您希望在调用convex_hull之后将对P的更改反映在main中,因此您应该通过引用传递它,如下所示:
std::vector<Point> convex_hull(std::vector<Point> &P)
if you do not pass by reference then the vector
will be copied and the copy will be modified not the original. Even if you do not want to modify the original you can pass as a const
reference to gain the advantage of not copying the vector
like so:
如果您没有通过引用传递,则将复制该向量,并且将修改该副本而不是原始副本。即使您不想修改原始文件,也可以将其作为const引用传递,以获得不复制向量的优点,如下所示:
std::vector<Point> convex_hull(const std::vector<Point> &P)
#3
0
if your datas are going to be modified, pass reference as argument or give the value instead
如果要修改数据,请将引用作为参数传递或改为给出值
#4
0
You can change vector<Point> P
to vector<Point> &P
so that you can modify vector P.
您可以将矢量
#1
4
The way you declared convex_hull
, the argument is copied:
您声明convex_hull的方式,参数被复制:
vector<Point> convex_hull(vector<Point> P){ ... }
This is very often a bad idea, since copying P
may be expensive. An alternative for that, if you want to ensure that P
itself is not modified, would be to use a const
reference.
这通常是个坏主意,因为复制P可能很昂贵。另一种方法是,如果要确保P本身不被修改,则使用const引用。
If you want to modify P
when calling the function (e.g. have modifications in main
, you must pass it by reference, and declare convex_hull
as follows (note the &
):
如果你想在调用函数时修改P(例如在main中有修改,你必须通过引用传递它,并按如下方式声明convex_hull(注意&):
vector<Point> convex_hull(vector<Point> &P){ ... }
#2
0
So after reading your comment, you want the changes to P
to be reflected in main
after calling convex_hull
, so you should just pass it by reference like so:
因此,在阅读完评论之后,您希望在调用convex_hull之后将对P的更改反映在main中,因此您应该通过引用传递它,如下所示:
std::vector<Point> convex_hull(std::vector<Point> &P)
if you do not pass by reference then the vector
will be copied and the copy will be modified not the original. Even if you do not want to modify the original you can pass as a const
reference to gain the advantage of not copying the vector
like so:
如果您没有通过引用传递,则将复制该向量,并且将修改该副本而不是原始副本。即使您不想修改原始文件,也可以将其作为const引用传递,以获得不复制向量的优点,如下所示:
std::vector<Point> convex_hull(const std::vector<Point> &P)
#3
0
if your datas are going to be modified, pass reference as argument or give the value instead
如果要修改数据,请将引用作为参数传递或改为给出值
#4
0
You can change vector<Point> P
to vector<Point> &P
so that you can modify vector P.
您可以将矢量