初学者系列:C++中using的用法

#include 

int main() {
    std::cout << "Hello, World!" << std::endl;  // 必须使用 std:: 前缀
    return 0;
}

上面是标准输出一句话的使用方法

下面给同学们介绍引入空间的方法:

1.通过  using namespace std  引入整个命名空间,之后可以直接使用:

2.通过  using std::cout 引入单个标识符:

#include 
using namespace std;  // 引入 std 命名空间

int main() {
    cout << "Hello, World!" << endl;  // 无需 std:: 前缀
    return 0;
}
#include 
using std::cout;  // 仅引入 cout
using std::endl;  // 仅引入 endl

int main() {
    cout << "Hello, World!" << endl;  // 可直接使用 cout 和 endl
    return 0;
}

你可能感兴趣的:(C++,c++,开发语言)