小编Zee*_*ter的帖子

使用不带前缀"std"的std :: sort(),也没有"using namespace std;" 编译成功

正如sort()其定义namespace std必须始终用作std::sort.但即使没有,下面的代码也能正确编译std.

#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> nums = {4,3,1,7,2,0};
    sort(nums.begin(),nums.end());
}
Run Code Online (Sandbox Code Playgroud)

ideone.com

但是这段代码没有.

#include <array>
#include <algorithm>

int main()
{

    std::array<int,5> nums = {4,1,8,9,6};
    sort(nums.begin(),nums.end());
}
Run Code Online (Sandbox Code Playgroud)

使用gcc 4.8.4with -std=c++11flag启用.

从这两个代码片段中可以清楚地知道std::vector这与此有关.但我无法弄明白.

c++ stdvector

10
推荐指数
1
解决办法
828
查看次数

如何调用'按值调用'和'按引用调用'重载函数?

我有两个重载函数,一个是"按值调用",另一个是"按引用调用".

int f (int a)
{
    //code
}

int f (int &a)
{
    //code
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我通过const int,它会调用"按值传递"功能,为什么?

const int a=3;
f(a);//calls the call by value function.Why?
Run Code Online (Sandbox Code Playgroud)

c++ overloading reference lvalue

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

标签 统计

c++ ×2

lvalue ×1

overloading ×1

reference ×1

stdvector ×1