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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:31:39+00:00 2026-06-12T11:31:39+00:00

I have data in stl containers (vector). Each node in the vector is a

  • 0

I have data in stl containers (vector). Each node in the vector is a structure which also contains stl strings.

struct record
{
string name;
string location;
int salary;
}

vector< record > employees;

I want to serialize employees but I also want to encrypt it before serializing.

my encryption function looks like this:

Encode(const char * inBfr, const int in_size, char ** outBfr, int& out_size )

By searching it looks like the stl standard doesn’t require the memory of my structure to be contiguous so I can’t just grab the memory of employees variable. Is there any other smart way that I can use this encoding function with my stl based structures/container? It is good for me that Encode function works in plain char * buffers so I know exactly what goes in and out but stl structures are not and I am tring to find a nice way so I can use stl with this function.

I am also opening to using any other stl containers if that helps.

  • 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-12T11:31:41+00:00Added an answer on June 12, 2026 at 11:31 am

    Although the element in the std::vector<T> are guaranteed to be laid out contiguously, this doesn’t really help: the record you have may include padding and, more importantly, will store the std::string‘s content external to the std::string object (in case the small string optimization is used, the value may be embedded inside the std::string but it will also contain a couple of bytes which are not part of the std::strings value). Thus, you best option is to format your record and encrypt the formatted string.

    The formatting is straight forward but personally I would encapsulate the encoding function into a simple std::streambuf so that the encryption can be done by a filtering stream buffer. Given the signature you gave, this could look something like this:

    class encryptbuf
        : public std::streambuf {
        std::streambuf* d_sbuf;
        char            d_buffer[1024];
    public:
        encryptbuf(std::streambuf* sbuf)
            : d_sbuf(sbuf) {
            this->setp(this->d_buffer, this->d_buffer + sizeof(this->d_buffer) - 1);
        }
        int overflow(int c) {
            if (c != std::char_traits<char>::eof()) {
                *this->pptr() = std::char_traits<char>::to_char_type(c);
                this->pbump(1);
            }
            return this->pubsync()? std::char_traits<char>::eof(): std::char_traits<char>::not_eof(c);
        }
        int sync() {
            char* out(0);
            int   size(0);
            Encode(this->pbase(), this->pptr() - this->pbase(), &out, size);
            this->d_sbuf->sputn(out, size);
            delete[] out; // dunno: it seems the output buffer is allocated but how?
            this->setp(this->pbase(), this->epptr());
            return this->d_sbuf->pubsync();
        }
    };
    
    int main() {
        encryptbuf    sbuf(std::cout.rdbuf());
        std::ostream eout(&sbuf);
        eout << "print something encoded to standard output\n" << std::flush;
    }
    

    Now, creating an output operator for your records just printing to an std::ostream can be used to create an encoded

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

Sidebar

Related Questions

I have a DLL which needs to access data stored in STL containers in
we have a data structure struct MyData { int length ; char package[MAX_SIZE]; };
Hello I have started writing common data structure library in C similar to STL.
I have a large tree that grows as my algorithm progresses. Each node contains
I have data from travel diaries which has been read in from a csv
I have data coming from an external system (in CSV form). The data contains
I'm trying to write a class which contains several std::vectors as data members, and
Suppose I have some data stored in a container of unique_ptr s: struct MyData
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have .so files that inside of them use some STL containers. Problem is

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.