但是string_view显然没有这样的需求,它本身不占有资源。那复制就好了,交换还是置空徒增开销。
对于unique_ptr这样的类型来说,正好move构造完原始对象就被置空了,只是因为这样实现比较方便而已。
swap(std::basic_string<CharT, Traits, Alloc>&lhs, std::basic_string<CharT, Traits, Alloc>&rhs)noexcept(/* see below */); (C++20 起) 为std::basic_string特化std::swap算法。交换lhs与rhs的内容。等价于lhs.swap(rhs)。 参数 lhs, rhs-要交换内容的 string ...
std::basic_string<CharT,Traits,Allocator>::replace std::basic_string<CharT,Traits,Allocator>::copy std::basic_string<CharT,Traits,Allocator>::resize std::basic_string<CharT,Traits,Allocator>::swap std::basic_string<CharT,Traits,Allocator>::find std::basic_string<CharT,Traits,Allocator>::rfind...
inline static void swap(std::string& a1, std::string& a2) { std::string temp( std::move(a1)); a1 = std::move( a2 ); a2 = std::move( temp ); } I ran this function 1000000 times and it took 78ms on average, but the std one just took 13ms. I just looked at t...
noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value || std::allocator_traits<Allocator>::is_always_equal::value) (since C++17) Example Run this code #include <iostream> #include <string> int main() { std::string a = "AAA"; std::string b = "BBBB"; std::...
#include <iostream> #include <string_view> int main() { std::string_view a = "AAA"; std::string_view b = "BBBB"; std::cout << "Before swap:\n" "a = " << a << "\n" "b = " << b << "\n\n"; a.swap(b); std::cout << "After swap:\n" "a = " << a <<...
c_ctr();这段代码相当不堪,建议用c++的swap算法或 string的swap函数来实现。include <string> include <iostream> using namespace std;int main(){ string name("这是先前的");cout<<name<<endl;string swapname("这是调换后的");name.swap(swapname);cout<<name<<endl;return 0;} ...
Before swap, one = "one" two = "two" Before swap, one = "two" two = "one" See also (constructor) constructs a basic_stringbuf object (public member function) swap (C++11) swaps two string streams (public member function of std::basic_stringstream<CharT,Traits,Allocator>) ...
C++ 字符串库 std::basic_string_view constexpr void swap(basic_string_view& v) noexcept; (C++17 起) 将视图与 v 的内容交换。 参数v - 要与之交换的视图 返回值(无) 复杂度常数。 Online version Offline version retrieved 2019-12-07 12:57. 本页面最后修改于2018年4月14日 (星期六) 10:21。