比较C++中的#include和使用命名空间std与Python中的import
Comparing #include and using namespace std in C++ with import in Python
我在互联网上读了很多关于C++中#include
语句和using namespace std
短语的页面,我需要澄清一下。既然我已经知道Python,我就用它来比喻。首先,在C++中
#include library
和Python
import library
都是一样的。然后,在C++中
#include library
using namespace std;
和Python
from library import *
都是一样的。
例如,如果我们比较第一个类比,我们知道在C++中
#include <iostream>
int main()
{
std::cout << "hello" << std::endl;
return 0;
}
类似于Python中的以下代码(类似于使用std和#include):
import math
def main():
print math.sqrt(12)
如果我们比较第二个类比,我们在C++中就有
include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
和Python
from math import *
def main():
print sqrt(12)
是相似的。
如果我错了,你能纠正我吗?
#include
是"复制粘贴此文件"。例如,您可以将一个文件#include
自身,并获得这种荒谬性。您也可以在文件中有一个代码片段,并在任何您想要的地方#include
它。
不要这样做:
//forloop.txt
for(int i=0;i<SIZE;i++){
ARRAY[i] = VALUE;
}
//prime.txt
2147483647
//main.cpp
#include<iostream>
using std::cout;
using std::string;
int main(){
int prime =
#include "prime.txt"
;
int arr[10];
#define ARRAY arr
#define SIZE 10
#define VALUE prime
#include "forloop.txt"
#undef VALUE
#undef SIZE
#undef ARRAY
for(int i=10;i-- > 0;){
cout<<arr[i]<<'n';
}
}
using
指令不是严格需要的。C++有一个"名称空间"的概念,例如,它可以防止您的max
函数被认为与cmath
中的math
函数不同。它看起来像这样:
namespace std{
int max(int i, int j){
if (i<j)
return j;
return i;
}
//some variable declaration and initialization
ostream cout(MAKE_BUFFER(stdout)); //std::ostream, if used outside
}
int max = 5;
int main(){
std::cout<<std::max(3,::max)<<'n'; //::max refers to global name "max"
}
using namespace std;
实际上意味着"如果你在全局范围内找不到一个名称,试着把std::
放在前面,看看这是否是一个名称。"人们说说说using namespace std
是不好的做法,部分原因是如果你的#include "F.h"
和F.h
有using namespace std
,那么你的代码也会使用命名空间std(由于复制粘贴#include
)。
Python不是一种编译语言。当你说import X
时,你是在给解释器一个命令来做一些事情(运行一些代码并命名)。#include
更像是告诉编译器在这一点上应该做什么来继续编译。
import
在Python中的意思是"附加此模块中的名称"。所以import blah as x
的意思是,"blah
中的每个变量(包括函数)都可以作为x.THING
访问"。它还调用模块的初始化内容,这是有意义的,因为在使用变量之前需要对其进行初始化。
Java也是一种编译语言。在Java中,import
语句不处理文件,而是处理类。Java中的每一段代码都必须属于一个类。
但与其他两种语言不同,import
并不是严格必要的。CCD_ 25实际上更接近C++的CCD_ 26。import
只是为您已经可以使用的类添加名称,但仅限于当前类。
以下是一些使用导入的代码。
import java.util.Scanner;
public class Example{
public static void main(String blargs[]){
Scanner cin = new Scanner(System.in);
System.out.println("Type in your name and press Enter: ");
System.out.println("Hello "+cin.next());
}
}
这是相同的程序,不使用导入。
public class Example{
public static void main(String blargs[]){
java.util.Scanner cin = new java.util.Scanner(System.in);
System.out.println("Type in your name and press Enter: ");
System.out.println("Hello "+cin.next());
}
}
这是相同的程序,使用所有长名称(import java.lang.*;
在每个Java源文件中都是隐式的)。
public class Example{
public static void main(java.lang.String blargs[]){
java.util.Scanner cin = new java.util.Scanner(java.lang.System.in);
java.lang.System.out.println("Type in your name and press Enter: ");
java.lang.System.out.println("Hello "+cin.next());
}
}
这是相同的程序,使用所有的导入。
import java.util.Scanner;
import static java.util.System.out;
import static java.util.System.in;
public class Example{
public static void main(String blargs[]){
Scanner cin = new Scanner(in);
out.println("Type in your name and press Enter: ");
out.println("Hello "+cin.next());
}
}
- 如何从存储在std::映射中的std::集中删除元素
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 理解GCC中的std::pow实现
- 捕获lambda中的std::数组
- 用C++中的std::condition_variable将线程置于死锁中会有风险吗
- 标准::unordered_map 中的 std::array 的值初始化
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- main() 中的 std::cout 在调试期间不会在调试控制台中打印任何内容
- 迭代器库中的 std::size() 不适用于传递给函数的 C 样式数组
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 如何处理给定代码中的 std::out_of_range 错误?
- 为什么目录中的 std::filesystem::file_size 留给实现?
- C++ - 向量中的 std::unique_ptr <> 为 nullptr
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- 访问基于范围的循环(如for_each)中的std::map迭代器
- 意外的模板与VC++中的std::void_t匹配
- 为什么一个表达式中的 std::string 连接给出的结果与逐个字符不同的结果?
- 如何识别容器中的 std::function 并将其删除?
- 列表中的 std::min_element 不返回最小值
- 将组合框信号连接到 Qt 中的 std::function 时出现问题