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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:28:04+00:00 2026-06-13T09:28:04+00:00

I have an object defined as : std::unordered_map<std::string, std::vector<int>> large_obj; It can store very

  • 0

I have an object defined as :

std::unordered_map<std::string, std::vector<int>> large_obj;

It can store very large amount of data (containing many rows) and it works pretty well.
However, I want to backup large_obj into a file, and in some time later, I will load it again from file to another object.

What is the most optimized method to read/write this object instead of writing each row in a file in a pre-defined structure?

Keeping in mind that:

  • Multiple calls to read() are more expensive than single call
  • Binary files are faster than text files.

Which save time for object I/O?

  • 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-13T09:28:05+00:00Added an answer on June 13, 2026 at 9:28 am

    I’d not really care about the overhead of read() and write() — just use a buffered stream and a data format that can be read and written without skipping back and forth in the data stream.

    The serialized stream you are writing out should be close enough to your data representation so you can take over large chunks of the data by simple copy, but still abstract enough to allow reconstruction from older versions of the data format or on machines with differing internal representations.

    I usually define a header that includes a magic number, a data format version and a set of values that capture the machine specific parts. For your case, that would be

    struct header {
        char magic[4];
        uint32_t endianness;           // 0x01020304
        uint32_t version;              // incremented when format changes
        // paranoia
        uint8_t char_bit;              // std::numeric_limits<char>::digits
        // sizeofs for all types format is dependent on
        uint8_t sizeof_int;            // sizeof(int)
    };
    

    When reading back data, you compare the values in the header against what you’d expect — if a case appears where this does not match, you can add deserialization code that handles this.

    For the rows, I’d use an encoding like

    uint16  string_length;
    char    string_data[];        // string_length bytes, padding if odd
    uint16  vector_length;
    int     vector_data[];        // vector_length ints
    

    This can be saved and restored efficiently. When your requirements change, just increment the version number, define a new format and adapt the parser code to create the new in-memory representation.

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

Sidebar

Related Questions

I have a 3D vector defined like this... std::vector<std::vector<std::list<Object*> > > m_objectTiles; I have
i have a object in javascript and some already defined functions. but how can
I am having a struct defined in C++ which contains int and std::string arrays
I have a large amount of data to process in c++ and find that
Well, the question is kinda simple. I have a object defined as: public class
I have the following object defined function BusinessUnit(id, name, code) { this.id = ko.observable(id);
I have a static object defined in my logging class, along the lines of:
Suppose I have an entity object defined as public partial class Article { public
Question in title pretty much sums it up. I have some resource object defined
I have a serial of object defined as: public class Foo { public DateTime

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.