假设我们有一个保存原始数据缓冲区的std::string s,但是我们想要std::vector<uint8_t> v。缓冲区长度以百万为单位。有没有一个简单的方法可以让v窃取s的内存,从而避免复制缓冲区?
就像std::vector<uint8_t>::vector(std::string&&)会做的那样,但是从STL的外部以某种方式来做。
或者,有没有可能通过与ss.str()一样高效的操作从std::stringstream ss获取v
发布于 2018-06-25 01:03:05
好的,这里有很多评论,让我们试着把一些东西放在一起,因为我需要练习,可能会获得一些点数更新:没有:(。
我对“现代C++”还是个新手,所以请相信我的话。可能最晚需要C++17,我没有仔细检查过。欢迎任何批评,但我更喜欢编辑我自己的帖子。请记住,当阅读这篇文章时,OP实际上想要做的是从文件中读取他的字节。谢谢。
重复数据删除更新:根据@的评论,进行了调整,以处理文件大小在调用stat()和调用fread()之间变化的情况。并随后用std::ifstream替换了fread,我想我们现在就到这里了。
#include <string>
#include <vector>
#include <optional>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
using optional_vector_of_char = std::optional <std::vector <char>>;
// Read file into a std::optional <std::vector <char>>.
// Now handles the file size changing when we're not looking.
optional_vector_of_char SmarterReadFileIntoVector (std::string filename)
{
for ( ; ; )
{
struct stat stat_buf;
int err = stat (filename.c_str (), &stat_buf);
if (err)
{
// handle the error
return optional_vector_of_char (); // or maybe throw an exception
}
size_t filesize = stat_buf.st_size;
std::ifstream fs;
fs.open (filename, std::ios_base::in | std::ios_base::binary);
if (!fs.is_open ())
{
// handle the error
return optional_vector_of_char ();
}
optional_vector_of_char v (filesize + 1);
std::vector <char>& vecref = v.value ();
fs.read (vecref.data (), filesize + 1);
if (fs.rdstate () & std::ifstream::failbit)
{
// handle the error
return optional_vector_of_char ();
}
size_t bytes_read = fs.gcount ();
if (bytes_read <= filesize) // file same size or shrunk, this code handles both
{
vecref.resize (bytes_read);
vecref.shrink_to_fit ();
return v; // RVO
}
// File has grown, go round again
}
}
int main ()
{
optional_vector_of_char v = SmarterReadFileIntoVector ("abcde");
std::cout << std::boolalpha << v.has_value () << std::endl;
}Live demo。当然,没有实际的文件可供读取,所以...
也是:您是否考虑过编写自己的简单容器来映射文件的视图?这只是个想法。
https://stackoverflow.com/questions/51004667
复制相似问题