是否有c++标准模板库类提供有效的字符串连接功能,类似于c#的StringBuilder或Java的StringBuffer?
当前回答
std:: string。Append函数不是一个好的选择,因为它不接受很多形式的数据。一个更有用的替代方法是使用std::stringstream;像这样:
#include <sstream>
// ...
std::stringstream ss;
//put arbitrary formatted data into the stream
ss << 4.5 << ", " << 4 << " whatever";
//convert the stream buffer into a string
std::string str = ss.str();
其他回答
string在c++中是等价的:它是可变的。
c++中一个方便的字符串构建器
就像许多人之前回答的那样,std::stringstream是选择的方法。 它工作得很好,有很多转换和格式化选项。在我看来,它有一个非常不方便的缺点:你不能把它用作一行代码或一个表达式。 你必须这样写:
std::stringstream ss;
ss << "my data " << 42;
std::string myString( ss.str() );
这很烦人,尤其是当你想在构造函数中初始化字符串的时候。
原因是:a) std::stringstream没有转换到std::string的操作符;b) stringstream的操作符<<()不会返回stringstream引用,而是返回std::ostream引用——这不能进一步作为字符串流计算。
解决方案是重写std::stringstream并给它更好的匹配操作符:
namespace NsStringBuilder {
template<typename T> class basic_stringstream : public std::basic_stringstream<T>
{
public:
basic_stringstream() {}
operator const std::basic_string<T> () const { return std::basic_stringstream<T>::str(); }
basic_stringstream<T>& operator<< (bool _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (char _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (signed char _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (unsigned char _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (short _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (unsigned short _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (int _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (unsigned int _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (long _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (unsigned long _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (long long _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (unsigned long long _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (float _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (double _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (long double _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (void* _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (std::streambuf* _val) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (std::ostream& (*_val)(std::ostream&)) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (std::ios& (*_val)(std::ios&)) { std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (std::ios_base& (*_val)(std::ios_base&)){ std::basic_stringstream<T>::operator << (_val); return *this; }
basic_stringstream<T>& operator<< (const T* _val) { return static_cast<basic_stringstream<T>&>(std::operator << (*this,_val)); }
basic_stringstream<T>& operator<< (const std::basic_string<T>& _val) { return static_cast<basic_stringstream<T>&>(std::operator << (*this,_val.c_str())); }
};
typedef basic_stringstream<char> stringstream;
typedef basic_stringstream<wchar_t> wstringstream;
}
有了这个,你可以写
std::string myString( NsStringBuilder::stringstream() << "my data " << 42 )
甚至在构造函数中。
我必须承认我没有衡量性能,因为我还没有在一个环境中使用它大量使用字符串构建,但我认为它不会比std::stringstream差很多,因为一切都是通过引用完成的(除了转换为字符串,但这是一个复制操作在std::stringstream)
因为std::string在c++中是可变的,你可以使用它。它有一个+=运算符和一个附加函数。
如果需要附加数值数据,请使用std::to_string函数。
如果你想更灵活地将任何对象序列化为字符串,那么可以使用std::stringstream类。但是你需要实现你自己的流操作符函数,让它与你自己的自定义类一起工作。
我想添加一些新的东西,因为以下原因:
我第一次尝试就失败了
Std::ostringstream的操作符<<
效率,但通过更多的尝试,我能够使一个StringBuilder在某些情况下更快。
每次我添加一个字符串时,我只是在某个地方存储一个对它的引用,并增加总大小的计数器。
我最终实现它的真正方式(恐怖!)是使用一个不透明的缓冲区(std::vector < char >):
1字节报头(2位来告诉以下数据是:移动的字符串,字符串还是字节[]) 6位表示字节的长度[]
对于byte []
我直接存储短字符串的字节(用于顺序内存访问)
用于移动的字符串(附加std::move的字符串)
指向std::string对象的指针(我们拥有所有权) 如果有未使用的保留字节,在类中设置一个标志
为字符串
指向std::string对象的指针(无所有权)
还有一个小优化,如果最后插入的字符串被移动,它检查自由保留但未使用的字节,并存储进一步的字节在那里,而不是使用不透明缓冲区(这是为了节省一些内存,它实际上使它稍微慢一点,可能也取决于CPU,而且很少看到字符串有额外的预留空间)
这最终比std::ostringstream略快,但它有一些缺点:
我假设固定长度的字符类型(所以1,2或4字节,不适合UTF8),我不是说它不会为UTF8工作,只是我没有检查它的懒惰。 我使用了糟糕的编码实践(不透明的缓冲区,容易使它无法移植,顺便说一句,我相信我的是可移植的) 缺乏ostringstream的所有特性 如果在合并所有字符串之前删除了一些引用的字符串:未定义行为。
结论?使用 std:: ostringstream
它已经解决了最大的瓶颈,而在矿的实现速度上提高了几个百分点是不值得的。
如果必须将字符串插入/删除到目标字符串或长字符序列的随机位置,Rope容器可能是值得的。 下面是一个来自SGI实现的例子:
crope r(1000000, 'x'); // crope is rope<char>. wrope is rope<wchar_t>
// Builds a rope containing a million 'x's.
// Takes much less than a MB, since the
// different pieces are shared.
crope r2 = r + "abc" + r; // concatenation; takes on the order of 100s
// of machine instructions; fast
crope r3 = r2.substr(1000000, 3); // yields "abc"; fast.
crope r4 = r2.substr(1000000, 1000000); // also fast.
reverse(r2.mutable_begin(), r2.mutable_end());
// correct, but slow; may take a
// minute or more.
推荐文章
- cplusplus.com给出的错误、误解或坏建议是什么?
- 找出质数最快的算法是什么?
- c++枚举类可以有方法吗?
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- 将析构函数设为私有有什么用?
- main()中的Return语句vs exit()
- 为什么c#不提供c++风格的'friend'关键字?
- 在函数的签名中添加关键字
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 为什么在标准容器中使用std::auto_ptr<>是错误的?
- 用比较double和0
- 保护可执行文件不受逆向工程的影响?
- 在c++中字符串前面的“L”是什么意思?
- 为什么std::map被实现为红黑树?
- 空括号的默认构造函数