比较C++中的#include和使用命名空间std与Python中的import

Comparing #include and using namespace std in C++ with import in Python

本文关键字:中的 std Python 命名空间 import C++ #include 比较      更新时间:2023-10-16

我在互联网上读了很多关于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.husing 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());
}
}