是boost::container::string连续的内存

Is the memory of boost::container::string contiguous?

本文关键字:内存 连续 container boost string      更新时间:2023-10-16

在c++98中,std::string的内存(c++11要求它是连续的)可能不是连续存储,那么boost::container::string呢?它保证内存是连续的吗?

如果它是连续的,它可以更自然地与遗留api一起工作。

boost::container::string str("some data");    
old_api(&str[0]);

不需要再复制到vector

boost::container::string str("some data");
std::vector<char> buffer(str.begin(), str.end());
old_api(&str[0]);

谢谢

是,boost::containter::string内存是连续的。

除非您使用一些非普通reference, const_refeferencepointer类型的时髦分配器,否则任何boost::container::basic_string专门化都具有连续内存。

查看string的data()方法:它保证(无条件的)常数时间,并且每个i对应[0, size()]中的data()+i == &operator[](i)