C++ 字符串追加和运算符 += 之间的区别
Difference between c++ string append and operator +=
两行之间有什么明显的区别吗?我的同事说使用 += "更快",但我不明白为什么它们应该有任何不同:
string s1 = "hello";
string s2 = " world";
// Option 1
s1 += s2;
// Option 2
s1.append(s2);
澄清一下,我不是在问这两个函数之间的用法差异- 我知道append()
可以用于更广泛的用途,并且operator +=
更专业。我关心的是如何处理这个特定的例子。
根据有关字符串::op+=/在线 c++ 标准草案的标准,我不希望有任何区别:
basic_string& operator+=(const basic_string& str(;
(1(效果:调用追加(str(。
(2(返回:*这个。
在Microsoft STL 实现中,运算符+=
是一个内联函数,它调用append()
。以下是实现,
- 字符串 (1(:
string& operator+= (const string& str)
basic_string& operator+=(const basic_string& _Right) {
return append(_Right);
}
- C弦(2(:
string& operator+= (const char* s)
basic_string& operator+=(_In_z_ const _Elem* const _Ptr) {
return append(_Ptr);
}
- 字符(3(:
string& operator+= (char c)
basic_string& operator+=(_Elem _Ch) {
push_back(_Ch);
return *this;
}
- 来源:GitHub:Microsoft/STL
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 这 4 个 lambda 表达式之间有什么区别?
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- (double) 和 double() 之间的区别
- & 和 * 之间的区别
- std::is_convertible 和 std::convertible_to 之间的区别(在实践中)?
- 析构函数和'delete'之间的区别
- 在 typedef 内部使用 const 关键字和在 typedef 外部使用 const 关键字之间有区别吗?
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- OpenMP #pragma omp for v/s #pragma omp parallel for 之间的区别?
- S() 与 S{} 之间的区别?