python - 如何使用 C++ 中的引用实现类似 python 的行为?

标签 python c++ reference

在 python 中,如果我这样做:

a = []
b = a 
a.append(1)
b[0] == 1 


这工作正常,因为 a 和 b 都指向堆上的底层对象。

什么是等效的 C++ 代码?

最佳答案

What would be the equivalent C++ code?



以下将是一个体面的翻译:
std::vector<int> a;
auto& b = a;
a.push_back(1);
b[0] == 1;

拥有引用语义的关键是使用引用变量。

关于python - 如何使用 C++ 中的引用实现类似 python 的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60393903/

相关文章:

Rcpp 中的 C++ 内置随机工件

c# - C#<->C++ DLLImport "Attempted to read or write protected memory."问题

python - Numpy 1D 与 nD 数组(按引用索引与按值索引)

python - SOAP 1.2 python 客户端

c++ - cstdio 流与 iostream 流?

c++ - c++ 'pass by reference if possible' 可以吗?

c++ - `this == &x` 是确定指针 (this) 和引用 (x) 是否指向同一对象的正确方法吗?

python - 如何运行 Python CGI 脚本

python - Ffmpeg 映射和 filter_complex 子进程 - Python

c++ - 需要帮助让我的开关盒看起来更好