我很难实现这样的事情:
int a = 5;
int& b = a;
pair <int, int> test;
test.first = b;
a = 1000;
Run Code Online (Sandbox Code Playgroud)
test.first显然,值不会改变,但是我希望它能够改变,所以我试图创建pair <int&, int&>,但我不能因为编译器.
我的目标是test.first改变,我该如何实现它(不使用int*指针,是)?
在C++ 11中,您可以使用reference_wrapper.你的代码会变成
#include <functional>
int a = 5;
auto b = ref(a);
b.get() = 3;
cout<<a<<endl;
pair <reference_wrapper<int>, int> test(b, 0);
a = 1000;
cout<<test.first<<endl;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |