[英]Concatenate boost::array and std::string
我有2個Boost陣列:
boost::array<int, 3> a = [1, 2, 3];
boost::array<int, 3> b = [4, 5, 6];
我需要將它們與字符串連接在一起:
std::string this_string = "abc";
這樣最終結果將是“ 123abc456”
怎么做?
最好的方法是將ostringstream實例用作緩沖區:
std::ostringstream buffer;
for(auto x: a)
buffer << x;
buffer << this_string;
for(auto x: b)
buffer << x;
std::string result = buffer.str();
assert(result == "123abc456");
這比連接字符串更有效,而且簡單明了。
您可以為boost::array
和std::string
重載'+'
,並使用std::to_string
如下所示:
template<typename T, std::size_t N>
std::string operator+ ( const boost::array<T,N>& arr, const std::string & x )
{
std::string s;
for( const auto& i:arr)
{
s += std::to_string(i) ;
}
return s+x ;
}
看here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.