添加到向量而不复制结构体数据 [英] add to vector without copying struct data

查看:33
本文介绍了添加到向量而不复制结构体数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这大概是一个简单的 C++ 问题,但我正在重新学习 C++ 并且不知道一些基础知识.我有一个类,其中包含一个带有对象向量的结构,因此如下所示:

This is presumable a simple C++ question, but I'm relearning C++ and don't know some of the basics. I have a class that includes a struct with a vector of objects in it, so something like this:

struct my_struct{
    Irrelevant_Object object,
    vector<tuple> tuple_list;
}

结构体和元组(另一个结构体)由架构预定义并在我的方法中提供给我;所以我不能改变它们.我想生成一个元组并将其插入到原来的空 tuple_list 中.

The struct and the tuple (another struct) are predefined by the architecture and given to me in my method; so I can't change them. I want to generate and insert a tuple into the originaly empty tuple_list.

简单的解决方案是有一个方法,它分配一个新的元组对象,填充元组数据,然后调用 tuple_list.push_back() 并传入分配的元组.但这需要分配一个新的元组,以便 push_back 方法将(大)元组结构的所有内容复制到向量的已定义内存空间中.所以我要支付分配/删除的费用,以及将元组内容复制到向量中以这样做的较小费用.这似乎相当低效,并且由于此方法将位于函数的关键路径中,因此我更喜欢更快的方法(不可否认,我怀疑此方法会成为瓶颈,并且我知道早期优化 == 不好.但是,我多问这个问题是为了了解 C++ 语法,然后是出于迫切需要在我的代码中实际执行此操作).

The simple solution is have a method which allocates a new tuple object, fills in the tuple data, then call tuple_list.push_back() and pass in the allocated tuple. But this would require allocating a new tuple only to have the push_back method copy all of the contents of the (large) tuple struct into an already defined memory space of the vector. So I'm paying the expense of an allocation/delete as well as the lesser expense of copying the tuple contents into the vector to do it this way. It seems rather inefficent, and since this method would be in the critical path of the function I would prefer something faster (admitedly I doubt this method would be the bottle-neck, and I know early optimization == bad. However, I'm asking this question more to learn something about C++ syntax then out of a deperate need to actually do this in my code).

所以我的问题是,有没有一种更快的方法来填充我的元组列表的内容而不分配和复制元组?如果这是一个数组,我可以使数组尽可能大,然后将对 tuple_list[0] 的引用传递给创建元组的函数.这样,函数就可以填充数组中已分配元组的空内容,而无需分配新元组或从一个元组复制到另一个元组.出于好奇,我试图用向量来做这件事,结果当我的迭代器指向 0x0 时出现了段错误,所以我认为该语法不适用于向量.那么有没有快速完成这项任务的方法?

So my question is, is there a quicker way to fill the contents of my tuple list without allocating and copying a tuple? If this was an array I could make the array as large as I want, then past a reference to tuple_list[0] to the function that creates the tuple. That way the funciton could fill the empty contents of the already allocated tuple within the array without allocating a new one or copying from one tuple to another. I tried to do that with the vector out of curiousity and ended up with a seg fault when my itterator pointed to 0x0, so I assume that syntax doesn't work for vectors. So is there a quick way of doing this assignment?

因为这是一个既要学习语言又要实际使用的问题,请随意添加您认为有趣的任何其他切线相关的东西,我正在寻找学习.

Since this is a question as much to learn the language as for actual use feel free to throw in any other tangentally relevant stuff you think are interesting, I'm looking to learn.

谢谢.

推荐答案

在 C++11 中,您可以使用 std::vector::emplace_back,它就地构造新对象,因此使用此方法时没有复制.

In C++11, you can use std::vector::emplace_back, which constructs the new object in-place, therefore there is no copying when you use this method.

通过使用这个方法,你可以做到:

By using this method, you could do this:

my_struct some_struct;
some_struct.tuple_list.emplace_back(1, 5, "bleh");

假设你的 tuple 对象包含这个构造函数:

Assuming your tuple object contains this constructor:

tuple::tuple(int, int, const std::string&)

编辑:您还可以使用 移动语义 来存储预先分配的元组:

Edit: You can also use move semantics to store a pre-allocated tuple:

my_struct some_struct;
tuple a_tuple;
/* modify a_tuple, initialize it, whatever... */
some_struct.push_back(std::move(a_tuple)); // move it into your vector

或者在 tuple 存储在向量中后使用对它的引用:

Or use a reference to the tuple after it has been stored in the vector:

my_struct some_struct;
some_struct.tuple_list.emplace_back(1, 5, "bleh");
// store a reference to the last element(the one we've just inserted)
tuple &some_tuple = some_struct.tuple_list.back(); 
some_tuple.foo();

在上述所有解决方案中,您只创建了一个 tuple,同时避免了复制.

On all of the above solutions you're creating only one tuple while also avoiding copying.

这篇关于添加到向量而不复制结构体数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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