简体   繁体   中英

std vector, copying data structure

I have the following class.

class Row
{
   Table* parent_table;
   Row(Table* construct);
}
class Table
{
    std::vector<Row>;
}

I create an std::vector<Table> test; , and in a loop I create Table objects and push them back to test vector. The problem is I lose Table* inside each Row. I need the pointer so when I iterate through the my Rows, I know which table they belong to.

Is there a way to force std::vector, not to copy objects when they are pushed back?

Instead of storing Row in your std::vector , store std::unique_ptr<Row> or std::shared_ptr<Row> . These tend to be sufficient for this kind of thing (there are more complex ways you can approach this, such as storing raw pointers (complex to maintain) or rolling your own pImpl based class, or writing your own smart pointer with custom semantics).

unique_ptr only supports moving, and as such avoids your copy problem. shared_ptr instead supports shared ownership, which is probably something you don't need. I'd advise using unique_ptr .

Push elements by reference or pointer. You are pushing copies so copying the element is all C++ can do. I would personally advice you to use a vector of pointers like so: std::vector<Row*>

Actually a safer and cleaner way would be to store shared_ptr-s instead of bare pointers so that you don't have to deal with memory management that much. Something like:

std::vector<std::shared_ptr<Row> >

You are asking two unrelated questions. Please don't do that in one post. One post - one question. That makes answering really easier.

SECOND:

Is there a way to force std::vector, not to copy objects when they are pushed back?

Is already answered by others.

FIRST:

I create an std::vector test;, and in a loop I create Table objects and push them back to test vector. The problem is I lose Table* inside Row inside Table. I need the pointer so when I iterate through the my Rows, I know which table they belong to.

Most probably your ROW does not contain any backreference to the Table that contains it. That way, when you pass around the vector-of-rows or the row itself, you lose the information about who owns it. You must either pass a-vector-of-rows along with table* owner , or a row along with table* owner , OR you must make your Row more intelligent and force all Rows to contain a table* owner (that is, have the Rows contain a backreference to owner)

Of course the backreference may be of any shape . It does not need to be a pointer. You may pass Table& , a shared_ptr<Table> , a string tablename or whatever that will help you access the table either directly or through some manager. The important thing is, that either the Row must contain it, or it must be passed separately but along with the rows.

The easiest solution in this case would probably be to store an iterator to your table:

class Row
{
    std:vector<Table>::iterator parent_table;
    Row(std:vector<Table>::iterator construct);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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