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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:57:39+00:00 2026-06-14T17:57:39+00:00

This question applies to any type of static data. I’m only using int to

  • 0

This question applies to any type of static data. I’m only using int to keep the example simple.

I am reading in a large XML data file containing ints and storing them in a vector<int>. For the particular data I’m using, it’s very common for the same value to be repeated consecutively many times.

<Node value="4" count="4000">

The count attribute means that the value is to be repeated x number of times:

for(int i = 0; i < 4000; i++)
    vec.push_back(4);

It seems like a waste of memory to store the same value repeatedly when I already know that it is going to appear 4000 times in a row. However, I need to be able to index into the vector at any point.

For larger data objects, I know that I can just store a pointers but that would still involve storing 4000 identical pointers in the example above.

Is there any type of strategy to deal with an issue like this?

  • 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-14T17:57:40+00:00Added an answer on June 14, 2026 at 5:57 pm

    Use two vectors. The first vector contains the indices, the second one the actual values.

    Fill in the indices vector such that the value for all indices between indices[i-1] and indices [i] is in values[i].

    Then use binary search on the indices array to locate the position in the values array. Binary search is very efficient (O(log n)), and you will only use a fraction of the memory compared to the original approach.

    If you assume the following data:

    4000 ints with value "4"
    followed by 200 ints with value "3"
    followed by 5000 ints with value "10"
    

    You would create an index vector and value vector and fill it like this:

    indices = {4000, 4200, 9200}; // indices[i+1] = indices [i] + new_count or 0
    values = {4,3,10};
    

    As suggested in the other answers, you should probably wrap this in an operator[].

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

Sidebar

Related Questions

This question actually applies to filtering using any group of checkboxes. I'm using the
I believe this question applies to any of the For Html helpers, but my
I am using PrimeFaces p:messages but I think this question applies equally to h:messages
I will use Java as an example, but this question really applies across all
I'm going to use PHP in my example, but my question applies to any
This question applies to any database table design, where you would have system default
I believe this question applies equally well to C# as to Java, because both
I'm coming largely from a c++ background, but I think this question applies to
This question is kinda aimed towards PHP but it probably applies to other languages
I am using Ruby on Rails, but I think this question could be applied

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.