我如何将对变量的引用传递给向量? [英] How do i passed a reference to variable into a vector?
问题描述
如何将变量的引用传递给向量,这样每次我在向量中更改它的值时,它都会反映回原始变量。
How do I pass a reference of a variable into a vector so that everytime I change it's value in the vector, it would reflect back to the original variable.
int a = 10;
int b = 20;
int c = 30;
std::vector <int> d {a, b, c};
我已经可以看到我只将值传递给向量。
如何将其作为参考通过?
我尝试过:
我的知识非常浅薄。我尝试了几种使用指针的方法,但到目前为止还没有工作。
I can already see that I only pass as value to the vector.
how do I make it pass as reference?
What I have tried:
As my knowledge is very shallow. I tried few ways using pointers but none has work so far.
推荐答案
简短的回答是你不能 -std :: vector
引用存在严重问题,它被设计为保存值。
所以你需要的东西看起来像是对<$的引用c $ c> int 但实际上是一个值。类似......
The short answer is you can't -std::vector
has severe problems with references, it was designed to hold values.
So what you need is something that looks like a reference to anint
but is actually a value. Something like...
class int_ref
{
public:
int_ref( int &ref ) : ref_( ref ){}
operator int &( )
{
return ref_;
}
private:
int &ref_;
};
这看起来就像 int
那样你创建一个向量
,你可以通过它们改变价值:
This looks enough like an int
that if you make a vector
of them you can change values through them:
auto main()->int
{
int a = 10, b = 20, c = 30;
std::vector<int_ref> nums{ a, b, c };
std::copy( begin( nums ), end( nums ), std::ostream_iterator<int>( std::cout, "\n" ));
for( auto n : nums ) n+= 10;
std::copy( begin( nums ), end( nums ), std::ostream_iterator<int>( std::cout, "\n" ) );
}
现在这可能是也可能不是一个好主意...... int_ref
有一些微妙的行为问题,这意味着它不是对 int
的引用,但如果您只想通过引用向量修改其他对象那个对象然后把自己打倒了。如果你有一个需要这种东西的用例,它可能没问题,但一般来说,像这样的转换/强制转换操作符的类是痛苦的。如果你没有意识到正在发生什么,它们违反了大多数程序员对世界如何工作的期望并且可能成为维护的噩梦。
Now this may or may not be a good idea... int_ref
has some subtle behavioural problems that means it's not quite a reference to an int
but if all you want to do is modify some other objects through a vector of references to that object then knock yourself out. If you have a use case that needs this sort of thing it might be okay but in general classes with conversion/cast operators like this one are a pain in the bum. They violate most programmers expectations as to how the world works and can be a maintenance nightmare if you don't realise what's going on.
在C ++中,指针可以是变量的引用。因此,您可以使用指针来存储向量中每个整数值的引用:
In C++, pointers can be references of variables. So, you may use pointer to store the reference of each of the integer value in your vector:
int a = 10;
int b = 20;
int c = 30;
std::vector<int*> v{ &a, &b, &c }; //line A
cout << "Original values are:" << endl;
for (auto item : v)
{
cout << *item << endl;
}
(*v[1]) += 10; // adding 10 to b by ref
cout << "Updated values are:" << endl;
for (auto item : v)
{
cout << *item << endl;
}
如果你认为这太难了,试着想一下单一的情况变量:
If you think that it's too difficult, try to just think of the case of a single variable:
int* pValue = &other;
并应用统一初始化,这样你就可以获得A行。
我认为你需要修改一些关于数组指针和指针数组的基本概念在您开始迈向现代C ++之前,您可以使用经典的C ++。
and apply the uniform initialization such that you can get line A.
I think you'll need to revise some basic concepts about pointer-of-array and array-of-pointer in the classic C++ before you can jump start your journey to modern C++.
这篇关于我如何将对变量的引用传递给向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!