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 408883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:44:07+00:00 2026-05-12T17:44:07+00:00

I need to store a collection of ints and doubles (representing nominal and real

  • 0

I need to store a collection of ints and doubles (representing nominal and real valued data) in c++. I could obviously store them all in a std::vector<double> , but this feels a bit wrong and doesn’t get the aesthetics bonus points.

I could also cook up something based on polymorphism, but I also need the collection to be really efficient: both storing and retrieving the data in the collection should be as fast as possible. I find it hard to judge whether such a solution would be maximally efficient.

I also found boost::variant, which might be of help here.

Additional info: the number of items in the collection will be small (<100) and known when initializing the collection.

Summarizing: I could obviously solve this in countless ways, but I am unsure what would be a good solution when (i) efficiency is really important and (ii) I also want to write somewhat nice code. What is my best bet here?

Edit, additional info: The collection represents a ‘row’ in a larger data set, its elements represent the values of certain ‘columns’. The properties of the rows are known, so it is known what kind of data is stored at which position. The ‘efficiency’ I am talking about is primarily the efficiency of retrieving the int/double value of a certain column, although fast setting of values is important too. I have some functions that operate on the data that need to retrieve it as fast as possible. Example:

typedef std::vector<double> Row;

void doubleFun(Row const &row)
{
    // Function knows there's always a double at index 0
    double value = row[0];
    ...
}

void integerFun(Row const &row)
{
    // Function knows there's always an integer at index 1
    int value = row[1];
    ...
}

After some more thought and reading the suggestions so far, it seems that just storing int columns and double columns in two separate vectors is a solid solution. The collection Row could then just define two different members for retrieving nominal and real data that the functions can use.

Just storing as a vector<double> is okay too I guess, but it depends on how fast the conversion between double and int is (which is probably pretty impressive).

Sorry for being a little unclear at first, I hope it’s clearer and now and that I can get some more thoughts on the matter.

  • 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-05-12T17:44:07+00:00Added an answer on May 12, 2026 at 5:44 pm

    Is ordering an important point in your container ?

    If not so:

    class MyContainer
    {
        std::vector<double> doubles;
        std::vector<int>    ints;
    
        push(double value) { doubles.push_back(value); }
        push(int value)    { ints.push_back(value); }
    
       ....
    };
    

    The iterator part (to browse the whole container) could be a little trickier…

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

Sidebar

Related Questions

In my application I need to store a small collection of temporary data. In
I need to store a huge number of elements in a std::vector (more that
My requirements: I need to store a collection of objects inside a class, objects
If I have a collection of around 100 entities that I need to store,
I need to store some data that looks a little like this: xyz 123
I need to store a collection of strings in session, and I have a
I have the need to store a list/collection/array of dynamically created objects of a
I have a collection of xml documents. I need to store the metadta and
I need to store key/value info in some type of collection. In C#, I'd
I need to store a growing large number of objects in a collection. While

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.