gpt4 book ai didi

c++ - 如何为 std::string 对象预分配内存?

转载 作者:IT老高 更新时间:2023-10-28 13:02:25 33 4
gpt4 key购买 nike

我需要将一个文件复制到一个字符串中。我需要某种方式为该字符串对象预分配内存,以及一种将文件内容直接读入该字符串内存的方法吗?

最佳答案

std::string has a .reserve method用于预分配。

std::string s;
s.reserve(1048576); // reserve 1 MB
read_file_into(s);

关于c++ - 如何为 std::string 对象预分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3303527/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com