C ++在向量中使用无法分配的对象 [英] C++ Use Unassignable Objects in Vector

查看:116
本文介绍了C ++在向量中使用无法分配的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 std :: vector 中存储对象列表,但对象包含引用,不能分配给。但是,我可以复制构造对象。



我可以想到的唯一选项是使用指针来包装对象,并在需要分配时重新安装指针,但是语法会显着降低可读性



不起作用:

  std :: vector< MyObject> myVector; 
//填充向量
// ...
myVector [1] = object1;

智能指针牺牲可读性:

  std :: vector< std :: unique_ptr< MyObject>> ptrVector; 
//填充向量
// ...
ptrVector [1] = std :: unique_ptr< MyObject>(new MyObject(object1));

是否有任何其他方法在 std中使用不可分配的对象:: vector

解决方案

因为我不能替换 std :: vector ,或者使用不同的方式来让你做你需要的。

$ b但是,如果可以修改 MyObject 的定义,它可能是一个选项,所以它使用 std :: reference_wrapper 而不是常规引用。这将允许 MyObject 是可分配的。



示例:

  #include< vector> 
#include< functional>
#include< iostream>

struct MyObject
{
// int& _i;
std :: reference_wrapper< int> _一世;
MyObject(int& i):_ i(i){}
};


int main(){
std :: vector< MyObject> vec;
int i1 = 3;
int i2 = 4;
MyObject c1(i1);
MyObject c2(i2);

/ *存储对象。 * /
vec.push_back(c1);

/ *分配给它。 * /
vec [0] = c2;

/ *现在确认它是正确的对象。 * /
for(const MyObject& it:vec)
std :: cout< it._i<< std :: endl;

/ *修改其内部引用的值。 * /
vec [0] ._ i.get()= 5;
/ *确认原来的int改变了值。 * /
std :: cout<< i2 ==<< i2<< std :: endl;

return 0;
}

注意事项:现有代码可能已包含直接分配到引用成员(即在上面的代码中被称为 _i 的成员)。这些分配旨在改变引用所指对象的值。当用 std :: reference_wrapper 替换引用时,所有直接赋值 _i = x 必须替换为 _i.get()= x ,否则程序的语义完全改变。



可以使用const引用 const T& std :: reference_wrapper< const T> 。使用上面的示例, MyObject 的定义然后更改为:

  struct MyObject 
{
std :: reference_wrapper< const int> _一世;
MyObject(const int& i):_ i(i){}
};


I want to store a list of objects in an std::vector, but the objects contain a reference and cannot be assigned to. I can, however, copy-construct the object.

The only option I can think of is to use pointers to wrap the objects and reseat the pointers when they need to be assigned, but the syntax for this would significantly reduce readability, especially when using iterators, and I would prefer an alternative.

Doesn't work:

std::vector<MyObject> myVector;
//fill the vector
//...
myVector[1] = object1;

Smart pointers sacrifice readability:

std::vector<std::unique_ptr<MyObject>> ptrVector;
//fill the vector
//...
ptrVector[1] = std::unique_ptr<MyObject>(new MyObject(object1));

Are there any other methods to use unassignable objects in an std::vector?

解决方案

This isn't a direct answer to your question as I can't offer a replacement for std::vector, or a different way of using it that allows you to do what you need to.

However, if it is possible to modify the definition of MyObject, it may be an option to change it so it uses std::reference_wrapper instead of conventional references. That would allow MyObject to be assignable.

Example:

#include <vector>
#include <functional>
#include <iostream>

struct MyObject
{
  //int &_i;
  std::reference_wrapper<int> _i;
  MyObject(int &i):_i(i) {}
};


int main() {
  std::vector<MyObject> vec;
  int i1 = 3;
  int i2 = 4;
  MyObject c1(i1);
  MyObject c2(i2);

  /* Storing object. */
  vec.push_back(c1);

  /* Assigning to it. */
  vec[0] = c2;

  /* Confirming that it's the correct object now. */
  for (const MyObject &it : vec)
    std::cout << it._i << std::endl;

  /* Modifying the value of its internal reference. */
  vec[0]._i.get() = 5;
  /* Confirming that the original int changed value indeed. */
  std::cout << "i2 == " << i2 << std::endl;

  return 0;
}

Caveat: Existing code may already contain direct assignments to the reference member (i.e. the member called _i in the code above). Those assignments were intended to change the value of the object the reference refers to. When replacing the reference with a std::reference_wrapper, all direct assignments _i = x must be replaced with _i.get() = x, otherwise the semantics of the program change entirely.

(EDIT) If the references used are const-references const T&, a std::reference_wrapper<const T> can be used. Using the example above, the definition of MyObject then changes to this:

struct MyObject
{
  std::reference_wrapper<const int> _i;
  MyObject(const int &i):_i(i) {}
};

这篇关于C ++在向量中使用无法分配的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆