Nor*_*löw 10 c++ string templates boost ostream
我目前正在使用非常聪明的软件包,boost::const_string直到http://libcxx.llvm.org/在Ubuntu上预打包或GCC使其__versa_string(在标头中ext/vstring.h)成为其默认字符串实现.libcxx std::string以及__versa_string默认情况下使用_small -string optimization(SSO).std::ostream但缺少输出到a的默认支持.代码
#include <iostream>
#include <boost/const_string.hpp>
const_string<char> x;
std::cout << x << endl;
Run Code Online (Sandbox Code Playgroud)
除非我们强行x进入一个成为的c字符串c_str(),否则不起作用
std::cout << x.c_str() << endl;
Run Code Online (Sandbox Code Playgroud)
编译和按预期工作.我添加了以下行const_string.hpp
template <typename T>
inline std::ostream & operator << (std::ostream & os, const boost::const_string<T> & a)
{
return os.write(a.data(), a.size());
}
Run Code Online (Sandbox Code Playgroud)
这应该提高性能比x.c_str(),因为size()是已知的,并不需要通过搜索来计算NULL作为c_str().我为我工作,但我不确定它是否适用于所有情况.我错过了什么吗?
我错过了什么吗?
是的,只需包含const_string/io.hpp. 然而它所做的只是:
return o << std::basic_string<char_type, traits_type>(s.data(), s.size());
Run Code Online (Sandbox Code Playgroud)