vimpunk / mio

Cross-platform C++11 header-only library for memory mapped file IO
MIT License
1.71k stars 157 forks source link

Is there a way to directly copy a string to mmap_sink? #109

Closed L-Super closed 2 weeks ago

L-Super commented 2 weeks ago

When the string is large, iterating and assigning values becomes inefficient.

  std::string data{"word"};
  try {
    mio::mmap_sink mmap(path, 10);
    for (int i = 0; i < data.size(); ++i) {
      mmap[i] = data[i];
    }
  } catch (std::system_error e) {
    std::cout<<"error: "<<e.what()<<"\n";
  }
spudwa commented 2 weeks ago

memcpy(&mmap[0], data.cstr(), data.size)

L-Super commented 2 weeks ago

memcpy(&mmap[0], data.cstr(), data.size)

Thanks! I found that std::copy is also a good way.