【发布时间】:2014-08-08 10:47:45
【问题描述】:
cppreference 页面说 std::basic_string::swap 具有恒定的复杂性。正如我假设的那样,这意味着复制内容不会发生,只有指针交换或类似操作。我写了一段测试代码,体验了一下,在VS2010下确实可以移动内容。测试代码:
std::string s1("almafa");
std::string s2("kortefa");
std::cout << "s1.c_str(): "<< (void*)s1.c_str() << std::endl;
std::cout << "s2.c_str(): "<< (void*)s2.c_str() << std::endl;
std::cout << "SWAP!" << std::endl;
s1.swap(s2);
std::cout << "s1.c_str(): "<< (void*)s1.c_str() << std::endl;
std::cout << "s2.c_str(): "<< (void*)s2.c_str() << std::endl;
g++ 4.6.3 上的输出
s1.c_str(): 0x22fe028
s2.c_str(): 0x22fe058
SWAP!
s1.c_str(): 0x22fe058
s2.c_str(): 0x22fe028
VS2010 上的输出
s1.c_str(): 000000000022E2D0
s2.c_str(): 000000000022E320
SWAP!
s1.c_str(): 000000000022E2D0
s2.c_str(): 000000000022E320
是与标准有偏差还是发生了我不知道的事情?
【问题讨论】:
-
使字符串 loooooooooooooooooooooonger.
标签: c++ visual-studio-2010 stl