快速起步

快速起步

准备

C++是编译型语言,编写完代码需要编译然后运行,而Python是解释型语言,可以直接运行;简单起见,可以在以下两个网页进行对比学习:C++在线编译运行;Python3在线运行。

Hello,World!

  • C++
    #include 
    
    int main(){
        printf("Hello,Cpp!");
    }
    
  • Python
    print('Hello,Python!')
    

在C++中,代表语句的结束,而{}则用来包裹代码段,在Python中,换行即可代表语句的结束,开始的缩进是用来包裹不同的代码段的,示例如下:

  • C++
    if(true){
        printf("at first branch!\n");
        printf("at first branch!\n");
    }
    else {
        printf("at second branch!\n");
        printf("at second branch!\n"); 
    }
    
    printf("Finish!");
    
  • Python
    if True:
        print('at first branch!')
        print('at first branch!')
    else:
        print('at second branch!')
        print('at second branch!')
        
    print('Finish!')
    

注释

  • 单行注释

    • C++ 中单行注释以//开始,以\n结束;
    • Python 中单行注释以#开始,以\n结束;
  • 多行注释

    • C++ 中多行注释以/*开始,以*/结束;
    • Python 中多行注释以'''开始和结束;

基础类型

Python与C++在类型上不同之处在于C++是静态类型,Python是动态类型,简单来讲就是,C++常量、变量声明都需要指定其是何类型,Python则不需要。

  • 布尔类型

    //C++
    bool bVal = true;
    bVal = false;
    
    #Python
    bVal = True
    bVal = False
    
  • 整数类型

    C++中的整数类型比较多,支持8、16、32、64位有符号无符号整型的声明与操作。

    //C++
    int nVal = 10;
    long long llVal = 10ll;
    ...
    

    Python中并没有这么多区分:

    #Python
    nVal = 10
    
  • 浮点类型

    C++中有单精度浮点数float和双精度浮点数double,其中float的字面量需要缀f

    //C++
    float fVal = 10.0f;
    double dVal = 10.0;
    

    Python中没有单精度浮点数:

    #Python
    fVal = 10.0
    dVal = 10.0
    
  • 字符串

    C++中的字符串有两种,一种是字面量,另一种是字符数组,行为特征为以\0结尾的字符数组,字面量为常量内容无法进行修改,而字符串数组如果不是声明为常量是可以进行修改的。

    //C++ 
    static const char* strVal = "this is a string"; 
    char strVal1[20]  = {"this is a string"};
    

    Python中的字符串声明是常量,无法修改。

    #Python
    strVal = 'this is a string'
    
  • 列表

    C++中有arrayvectorlist等来表示一组元素,其中array内存连续,大小不变;vector内存连续,大小动态生长。

    而Python中的列表操作起来像是vector,声明类似于数组:

    #Python
    squares = [1,4,9,16,25] #声明
    item = squares[0] #元素访问
    squares[1] = 5 #修改元素
    squares.append(36) #追加元素
    len(squares) #获取长度
    ...
    

流程控制

  • 分支if/else

    C++中的分支操作为ifelse ifelse,分别对应Python中的ifelifelse:

    //C++
    int x = 0;
    if(x < 0){
        //分支1
    }
    else if(x == 0){
        //分支2
    }
    else if(x == 1){
        //分支3
    }
    else {
        //分支4
    }
    
    #Python
    x = int(0)
    if x < 0:
        #分支1
    elif x==0:
        #分支2
    elif x==1:
        #分支3
    else:
        #分支4
    
  • 循环

    C++中循环的一种写法为for(初始操作;循环条件;中间处理),而Python中对应循环的写法为for 值 in 可迭代对象:;还有C++中的while(循环条件),Python中对应写法为while 条件::

    //C++
    for(auto i = 0 ; i < 10 ; i ++){
        printf("%d",i);
    }
    
    int i = 0;
    while( i < 10) {
        printf("%d",i);
        i++;
    }
    
    #Python
    numbers = [0,1,2,3,4,5,6,7,8]
    for i in numbers:
        print(i)
    
    i = 0
    while i < 10:
        print(i)
        i = i+1
    
  • 跳转

    C++中可以使用break来跳出本次循环,使用continue来直接执行循环中的下一次操作,在Python中使用方法一样,不一样之处在于可以使用else来在循环判定条件不满足时执行相应操作。

    //C++
    for(auto n = 2; n < 10; n++ ){
        bool bVal = false;
        for(auto x = 2; x < n ; x++){
            if(n %x == 0){
                printf("%d equals %d*%d\n",n,x,n/x);
                bVal = true;
                break;
            }
        }
        if(!bVal)
            printf("%d is a prime number\n",n);
    }
    
    #Python
    for n in range(2,10):
        for x in range(2,n):
            if n % x == 0:
                print(n,'equal',x,'*',n//x);
                break
        else:
            print(n,'is a prime number')
    

标准输入输出

  • 标准输出

    C中标准输出指的是stdout,即命令行输出,还有stderr是错误信息的输出流,有相关的函数族来完成相应操作:

    //C
    putchar('\n');//无格式输出
    printf("%d",10);//格式化输出
    printf("%s is %d","the value ",100);//可变参数输入
    

    C++中对标准输出的定义为std::cout:

    std::cout<<'\n';
    std::cout<<10;
    std::cout<<"the value is "<<100;
    

    Python中使用print向标准输出输出信息:

    #Python
    print()
    print(10)
    print('the value is',100)
    
  • 标准输入

    C中标准输入指的是stdin,从命令行接收输入信息:

    //C
    auto ch = getchar();
    int nVal = 0;
    scanf("%d",&nVal);
    

    C++中对标准输出定义为std::cin:

    //C++
    int nVal = 0;
    std::cin>>nVal;
    

    Python中使用input来从命令行接收输入信息:

    #Python
    strVal = input()
    nVal = int(input('Please enter an integer:')) 
    

函数

C++中函数声明一般在头文件中供别的模块使用,然后在源文件中实现,譬如计算斐波那契数列的函数:

```
//C++

//fibonacci.h
int fibonacci(int nVal);
//fibonacci.cpp
int fibonacci(int nVal){
    assert(nVal >= 0);
    if(nVal == 0)
        return 0;
    if(nVal == 1)
        return 1;
    return fibonacci(nVal-1)+fibonacci(nVal-2);
}
//demo.cpp
#include "fibonacci.h"
int main(){
    auto nVal = fibonacci(10);
    return 0;
}

```

在Python中函数声明及实现如下:

```
#Python
#fib.py
def fib(n):
    a,b = 0,1
    while a

你可能感兴趣的:(快速起步)