delim.empty()) { return ToString(); } string_t result; if (m_Data.empty()) { return result; } // Hope we don't overflow the size type. size_type st = (delim.size() * (m_Data.size() - 1)) + m_totalSize + 1; result.reserve(st); // If you need reasons to love C++11, here is one. struct adder { string_t m_Joiner; adder(const string_t &s): m_Joiner(s) { // This constructor is NOT empty. } // This functor runs under accumulate() without reallocations, if 'l' has reserved enough memory. string_t operator()(string_t &l, const string_t &r) { l += m_Joiner; l += r; return l; } } adr(delim); auto iter = m_Data.begin(); // Skip the delimiter before the first element in the container. result += *iter; return std::accumulate(++iter, m_Data.end(), result, adr); }
}; // class StringBuilder
|