Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7897443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:10:04+00:00 2026-06-03T08:10:04+00:00

This is presumable a simple C++ question, but I’m relearning C++ and don’t know

  • 0

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;
}

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.

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).

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.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:10:05+00:00Added an answer on June 3, 2026 at 8:10 am

    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");
    

    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
    

    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();
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an Nhibernate question that has bothered me for some time... If I
OK, this feels like a question that should be easy to answer, but as
This is a weired problem, I have implemented simple quick sort as follows.. static
This should be exceptionally simple. I have a zoo object with 500 times series
A minor question that I hope admits of a simple answer that I'll kick
In short: is there some way I can modify a class definition such that
Probably a simple thing to fix, but for some reason the content column on
I have some fundamental points/questions about OpenGL, not all involving code but concepts as
In discussion about my answer to this question , there was some disagreement over
I'm attempting upload multiple files in ASP.NET MVC and I have this simple foreach

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.