0

I am trying to move the map array but for this I need to put everything in buffer, here is my code.

#include <iostream>
#include <map>
#include <string>

    std::map<std::int, std::string> maphack;
    maphack.emplace(1, "CS");
    maphack.emplace(2, "PBG");
    maphack.emplace(3, "Maicraft");
    maphack.insert_or_assign(3, "GTA5");

    for (auto &a : maphack) {
        std::cout << a.first << " : " << a.second << '\n';
    }

How to put everything above in the buffer?

char buffer[64];

send(sock, buffer, AmountToSend, 0);
chip
  • 35
  • 6
  • 3
    What you're trying to do is known as _serialization_. You'll need to figure out a format or layout for storing that map as an array of bytes. – Etienne de Martel Sep 23 '19 at 13:36
  • An example of industry standard serialization tool is google protobuf. – Jeffrey Sep 23 '19 at 13:37
  • Thanks, I’ll look for serialization examples – chip Sep 23 '19 at 13:40
  • I didn't find anything but tried, I learned what serialization is and that’s all :) – chip Sep 23 '19 at 14:24
  • Or is your problem how to save your serialized data into `buffer`? If this is the case, don't write to `std::cout` but instead to an `std::stringstream`. Then you can access the serialized string via `std::stringstream::str()`. – churill Sep 23 '19 at 14:44

1 Answers1

1

The simple answer is to use std::ostringstream and std::istringstream which allows to use std streams machinery with a string storage. Which can be converted to char* with c_str() method if needed later, full example is below.

The better answer can be to use more heavy lifting library for serialization, the choice of the format will depend on your program. Do you need binary or text format? Do you want other programs (in other programming languages) be able to read it? Do you need to support different endian system? Do you need to handle error when deserializing malformed input? Boost, Qt, google protobuf, and other ones are out there.

#include <iostream>
#include <map>
#include <sstream>
#include <string>

void write_map(const std::map<int, std::string> &m, std::ostream &out) {
  for (auto &a : m) {
    out << a.first << " " << a.second << '\n';
  }
}

std::map<int, std::string> read_map(std::istream &in) {
  std::map<int, std::string> m;
  int i;
  std::string s;
  while (in >> i >> s) {
    m.emplace(i, s);
  }
  return m;
}

int main() {
  std::map<int, std::string> m;
  m.emplace(1, "CS");
  m.emplace(2, "");
  m.emplace(3, "Maicraft");
  m.insert_or_assign(3, "GTA5");

  std::ostringstream out;
  write_map(m, out);
  std::string data = out.str();
  std::cout << "Data:\n" << data << std::endl;

  // send data over socket
  // ...

  std::istringstream in(data);
  auto m1 = read_map(in);
  std::cout << "Read:\n";
  write_map(m1, std::cout);
  std::cout << std::endl;
}
Alex Maystrenko
  • 540
  • 5
  • 10
  • [`while (!in.eof())` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). Also, this code will only work when the strings are non-empty and contain no whitespace. Which may be fine for a small example, but the limitations should be mentioned. – interjay Sep 23 '19 at 14:56
  • @interjay thanks, the eof() can go to infinite loop in case of error, i updated the answer to be a litlle bit more robust. – Alex Maystrenko Sep 23 '19 at 15:07