Python和C++参考
我对C ++中的参考及其与Python的重叠有疑问。我在Python中写了一个功能,该功能吸收X和Y,并将Y添加到X中。此后,我打印了结果。
def func(x, y):
x = x + y
return x
c = [5,4,6,4]
d = [6,7,8,9]
t = func(c,d)
print(t)
print(c)
当Python通过引用通过所有内容时,人们会期望变量C会更改并包含d。但是,事实并非如此。 要查看这是否对C ++也是如此,我编写了一个向量函数,将两个向量添加在一起。
vector<int> FuncAdd(vector<int> &x, vector<int> &y){
x.insert(x.end(), y.begin(), y.end());
return x;
}
int main(){
vector<int> y = {3,4,5,6};
vector<int> x = {4,5,6,7};
vector<int> c = FuncAdd(x, y);
for(int i : x){
cout << i << " ";
}
cout << endl;
for(int i : c){
cout << i << " ";
}
cout << endl;
这里的结果是X确实改变了,现在包含y。为什么C ++会更改向量,而Python不会更改其列表?谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在Python中,当您使用
+
用于串联两个列表创建新列表并返回时。这意味着,当您编写X + Y
时,会创建一个新列表。这也意味着在x = x + y
之后,本地变量x
现在将参考新创建的列表(由于x + y )。如果要更改原始列表在场,则可以使用
扩展
方法,如下示例中所述。请注意,而不是写
x = x + y
,然后返回x
您可以只写了返回x + y
,效果将相同,如下所示:如果要更改原始列表在场您可以使用List的
Extend
方法,如下所示:类似地,在C ++中,您正在使用< code> std :: vector :: insert
std :: vector
的成员函数将插入元素被调用的向量(这是vector, X
)。而且,由于您已通过引用传递了向量X
,因此更改将反映在原始矢量中。In Python, when you use
+
for concatenating two lists a new list is created and returned. This means that when you wrotex + y
, a new list is created. This also means that afterx = x + y
, the local variablex
will now refer to the newly created list(as a result ofx + y
). If you want to change the original list in-place you could instead use theextend
method as explained in the below example.Note instead of writing
x = x + y
and then returningx
you could just have writtenreturn x + y
and the effect will be the same, as shown below:If you want to change the original list in-place you can use list's
extend
method as shown below:Similarly, in C++ you're using the
std::vector::insert
member function ofstd::vector
that will insert the elements into the vector on which the member function was called(which is the vectorx
). And since you've passed the vectorx
by reference, the change will be reflected in the original vector.是的,是的,在调用函数时,在Python中,您会获得对呼叫者中相同对象的引用。如果修改该对象,则在函数返回后会看到该修改。
但是语句
x = x + y
di 不是修改x
,它创建了一个全新的参考x
替换从函数开始的那个点开始的旧一个。Yes it's true, in Python when you call a function you get a reference to the same object that was in the caller. If you modify that object, you'll see that modification after the function returns.
But the statement
x = x + y
does not modifyx
, it creates a brand new referencex
that replaces the old one from that point of the function onwards.在Python中,对象引用按值传递。
您将其称为“ C”
因此,您的列表是一个对象[5,4,6,4],当将变量C传递到函数时,
,该对象按值传递,但它将是相同的对象。因此,x是一个不同的变量,但包含相同的对象,直到...
您与另一个对象分配x
即x+y
现在,X具有不同的对象,与C的不同之处不一样
,Append()会起作用,因为它本质上是对同一对象的操作。
如果仍然不清楚,请参阅下面的链接
通过
In python Object references are passed by value.
So your list is an object [5,4,6,4] and you gave it the name 'c'
When you pass variable c to a function, the object is passed by value but it will be the same object.
So x is a different variable but contains the same object, until...
You assign x with another object
i.e. x+y
Now x has a different object, not the same as what c has
Thats why, append() would work because it is essentially an operation on the same object.
Refer to below link if it is still unclear
Python pass by value or reference