简体   繁体   English

如何在C ++中将对一个矢量的对象的引用存储到另一矢量中?

[英]How to store references to objects from one vector in another vector in C++?

I've got a vector std::vector<MyClass> myclass_vec(10) with 10 initialized objects of MyClass . 我有一个向量std::vector<MyClass> myclass_vec(10)其中包含10个MyClass初始化对象。 Now I would like to loop over this vector and store a reference to every MyClass object in another vector std::vector<MyClass> myclass_vec_refs . 现在,我想遍历此向量,并将对每个MyClass对象的引用存储在另一个向量std::vector<MyClass> myclass_vec_refs The reason why I would like to store references is so because I don't have to copy the objects and obviously, refer to the same object as in myclass_vec . 之所以要存储引用,是因为不必复制对象,并且显然要引用与myclass_vec相同的对象。

For some reason, this doesn't work out as aspected. 出于某种原因,这并不能解决问题。 Do I have to declare std::vector<&MyClass> myclass_vec_refs like so? 我是否必须像这样声明std::vector<&MyClass> myclass_vec_refs

As I was looking through other questions asked here I read about std::unique_ptr . 当我浏览此处提出的其他问题时,我读到了有关std::unique_ptr If I change std::vector<std::unique_ptr<MyClass>> myclass_vec(10) then I wouldn't able to have a reference or pointer in myclass_vec_refs since they are declared unique. 如果我改变std::vector<std::unique_ptr<MyClass>> myclass_vec(10)那我就不能有一个参考或指针myclass_vec_refs因为他们宣称是唯一的。 Correct me please if I'm mistaken. 如果我弄错了,请纠正我。

Another approach was using std::shared_ptr . 另一种方法是使用std::shared_ptr Since it holds a reference counter I would be able to have myclass_vec_refs point to objects in myclass_vec , but I read this introduces quite some overhead and share_ptr should only be used as a last resort. 由于它拥有一个引用计数器,我将能够有myclass_vec_refs点中的对象myclass_vec ,但我读了这个介绍颇有一些开销和share_ptr只应作为最后的手段。

I also don't know if referencing like I'm attempting works out. 我也不知道是否像我正在尝试的那样进行引用。 What happens if an object in myclass_vec is deleted? 如果删除myclass_vec的对象会myclass_vec Is the myclass_vec_refs vector resized by -1 since the object doesn't exist anymore or is it just pointing to bad memory? 因为该对象不再存在,或者myclass_vec_refs向量是否已调整为-1,还是仅指向错误的内存?

Is it possible to emplace_back a reference in the myclass_vec_refs vector? 是否可以在myclass_vec_refs向量中进行emplace_back引用? Since this creates the object in-place I guess this doesn't work and only push_back can be used? 由于这会在原地创建对象,所以我猜想这是行不通的,只能使用push_back吗?

You cannot make a vector of references. 无法建立参考向量。 Why? 为什么?

A reference must refer to an actual object at all times, and vectors by design must be able to create "empty" objects (ie default constructor) dynamically for you. 引用必须始终引用一个实际对象,并且矢量设计必须能够为您动态创建“空”对象(即默认构造函数)。

You can however create a vector of pointers. 但是,您可以创建指针向量。

If the other vector is modified in any way, your pointers will become invalid. 如果以任何方式修改了另一个向量,则指针将变为无效。 If this is a problem to you, use a map or set instead. 如果这是你的问题,使用地图设置来代替。

As answered here: Strange Template Deduction 如这里的回答: 奇怪的模板推导

The trick is to use std::reference_wrapper<> 诀窍是使用std::reference_wrapper<>

#include <algorithm>
#include <iostream>
#include <vector>

template<typename container_ty_, class Comp>
auto where(container_ty_& V, Comp&& comp)
{
    using value_type = typename container_ty_::value_type;
    using reference =
    std::conditional_t<
      std::is_const<container_ty_>::value,
        std::reference_wrapper<const value_type>,
        std::reference_wrapper<value_type>
    >;

    std::vector<reference> cursor;

    for(auto& VAL : V)
        if(comp(VAL))
            cursor.push_back(VAL);

    return cursor;
}

int main(int argc, char** argv) {
    std::vector<int> tVect = {0, 5, 2, 1, 7, 9};

    //Why must std::vector<int> be passed...
    auto vec = where(tVect, [](const int& V) -> bool { return V > 5; });

    std::for_each(vec.begin(), vec.end(), [] (int& v) { std::cout << v++ << std::endl; });
    std::cout << std::endl;
    std::for_each(tVect.begin(), tVect.end(), [](const int& v) { std::cout << v << std::endl; });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM