安装c++编译器:

sudo apt-get install g++

安装python环境解释器:

sudo apt-get install python

简单for循环对比,

c++

#include <iostream>

using namespace std;

int main()
{
    int a=5;

    for(a;a<10;a++)
    {
       cout << "a = " << a << endl;
    }    
    
    return 0;

}

python:

for a in range (5,10):
    if a < 10:
        print "a = ",a
        a+=1
    else:
        break

在ubuntu下的执行情况:

c++程序:

g++ c++_for.cpp -o c++_for //编译c++_for.cpp这个c++源文件并生成c++_for可执行文件
./c++_for                  //执行c++_for可执行文件

python程序:

python python_for.py

 

while循环对比:

c++:

#include <iostream>

using namespace std;

int main()
{
    int a=5;

    while(a<10)
    {
       cout << "a = " << a << endl;
       a++;
    }    
    
    return 0;

}

python:

a = 5

while a < 10:
        print "a = ",a
        a+=1

类定义的对比:

c++:

#include <iostream>

class A
{
    public:
    int i;
    void test()
    {
        std::cout << i << std::endl;
    }
};

int main()

{

    A a;

    a.i = 10;     
    a. test();

    return 0;
}

python:

class A: 
    i = 10  
    def test(self): 
        print self.i
a = A()  
a.test()

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐