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

  • Home
  • SEARCH
  • 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 8519175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:10:37+00:00 2026-06-11T06:10:37+00:00

How might I initialise a std::vector from an array of structs, where the struct

  • 0

How might I initialise a std::vector from an array of structs, where the struct contains a union of different types. In other words, the array is used to store a number of values of a specific type, which can be int, char* etc.

This is my solution so far but I’m looking for a better approach:

The convert function returns a vector<int> if it stores ints or a vector<std::string> if it stores char*.

The Value type below is a struct containing a union called value. The Container class below points to a buffer of such Values.

// union member getter
class Getter
{
public:

    void operator()(int8_t& i, const Value& value)
    {
        i = value.value.i;
    }

    void operator()(std::string& s, const Value& value)
    {
       s = std::string(value.value.s);
    }

    ...
};

template<class T>
std::vector<T> convert(Container* container)
{
    std::vector<T> c;
    c.reserve(container->nrOfValues);
    Getter g;
    for(int i=0;i<container->nrOfValues;i++)
    {
        T value;
        g(value, container->values[i]);
        c.push_back(value);
    }
    return c;
}
  • 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-11T06:10:39+00:00Added an answer on June 11, 2026 at 6:10 am

    Your problem is the union gives a different name to each value, which causes the need for a function that converts a name to a type, such as Getter::operator() returning a type and getting a named member of the union.

    There isn’t much you can do with this. You can save a variable declaration and a copy/string constructor on each item, but that’s about it.

    If you can’t modify the original struct, you could initialize the vector with a length set of default value (which must be passed in), then iterate through using the getter as:

    vector<T> v(length, defaultValue);
    typename vector<T>::iterator iter = vec.begin();
    for(int index = 0; *iter != vec.end() && index < length; ++iter, ++index) {
      converter(*iter, array[index]);
    }
    

    Notice that this starts getting cumbersome in iterating the index and the iterator and verifying both are still valid in case of an accident…

    If you can modify the original struct:

    class Ugly { // or struct, it doesn't matter
    public:
      union {
        char* s;
        int i;
      } value;
    
      Ugly(char* s) {
        value.s = s;
      }
    
      Ugly (const int& i) {
        value.i = i;
      }
    
      operator std::string() const {
        return std::string(value.s);
      }
    
      operator int() const {
        return value.i;
      }
    };
    

    Then your for loop becomes:

    for(int i=0;i<container->nrOfValues;i++)
    {
        c.push_back(container->values[i]);
    }
    

    Note: You might create the vector and pass it as an argument to the copy function since it involves copying the data over during the return.

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

Sidebar

Related Questions

I'm trying to design a class which has two vectors of large sequences. std::vector<double>
i have a vector of vertices from mygraph, and i topologically sort the vertices.
I have this declaration of a multidimensional vector std::vector< vector < vector < ofxImage
For example, one might initialize a character array as: char[] myArray = new char[3];
i'm just beignning to learn about structs and seperating things into different files. At
How does a std::vector<std::string> initialize its self when the following code is invoked std::vector<std::string>
Non-POD derived class PayloadMessage contains an array data member (_payload) whose elements appear to
I have about 70-150 different structs X with an unsigned integral ID field. They
Error % javac StringTest.java StringTest.java:4: variable errorSoon might not have been initialized errorSoon[0] =
$ javac InitInt.java InitInt.java:7: variable right might not have been initialized InitInt(){} ^ 1

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.