小编jus*_*now的帖子

在int的情况下交换失败并且在字符串的情况下工作

#include <iostream>
#include <string>
#include <algorithm>    
int main()
{
 std::string str1 = "good", str2 = "luck";
 swap(str1,str2); /*Line A*/
 int x = 5, y= 3;
 swap(x,y); /*Line B*/
}
Run Code Online (Sandbox Code Playgroud)

如果我评论B行代码编译(http://www.ideone.com/hbHwf),而注释行A代码无法编译(http://www.ideone.com/odHka),我收到以下错误:

error: ‘swap’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

为什么我在第一种情况下没有收到任何错误?

c++ swap

4
推荐指数
1
解决办法
1440
查看次数

标签 统计

c++ ×1

swap ×1