首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让`std::vector<unsigned字符串从``std::char>` `窃取内存

让`std::vector<unsigned字符串从``std::char>` `窃取内存
EN

Stack Overflow用户
提问于 2018-06-24 04:09:53
回答 1查看 390关注 0票数 0

假设我们有一个保存原始数据缓冲区的std::string s,但是我们想要std::vector<uint8_t> v。缓冲区长度以百万为单位。有没有一个简单的方法可以让v窃取s的内存,从而避免复制缓冲区?

就像std::vector<uint8_t>::vector(std::string&&)会做的那样,但是从STL的外部以某种方式来做。

或者,有没有可能通过与ss.str()一样高效的操作从std::stringstream ss获取v

EN

回答 1

Stack Overflow用户

发布于 2018-06-25 01:03:05

好的,这里有很多评论,让我们试着把一些东西放在一起,因为我需要练习,可能会获得一些点数更新:没有:(。

我对“现代C++”还是个新手,所以请相信我的话。可能最晚需要C++17,我没有仔细检查过。欢迎任何批评,但我更喜欢编辑我自己的帖子。请记住,当阅读这篇文章时,OP实际上想要做的是从文件中读取他的字节。谢谢。

重复数据删除更新:根据@的评论,进行了调整,以处理文件大小在调用stat()和调用fread()之间变化的情况。并随后用std::ifstream替换了fread,我想我们现在就到这里了。

代码语言:javascript
复制
#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。当然,没有实际的文件可供读取,所以...

也是:您是否考虑过编写自己的简单容器来映射文件的视图?这只是个想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51004667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档