C++とpython構文の比較

7322 ワード

C++とpython構文の比較
flyfish
    
//C++
    int a = 8, b = 9;

    //python
    i = 10
    print(i)
    
//C++
    if (a > b)
    {
        std::cout << a << " is max" << std::endl;
    }
    else if (a == b)
    {
        std::cout << a << " is equal to " << b << std::endl;
    }
    else
    {
        std::cout << b << " is max" << std::endl;
    }

    //python

    if a > b:
    print(a, 'is max')
        elif a == b :
        print(a, 'is equal to', b)
    else:
    print(b, 'is max')
    
//C++
    int c[3] = { 10, 11, 12};
    for (int i = 0; i < 3; i++)//C++98
    {
        std::cout << c[i] << std::endl;
    }
    for (auto i : c)//C++11
    {
        std::cout << i << std::endl;
    }

    //python
    myarray = [10, 11, 12]
        for i in myarray :
    print(i)
  
//C++
    auto mytuple = std::make_tuple(5, 6, 7);
    std::cout << std::get<0>(mytuple) << std::endl;

    //        std::ignore           
    int x, y, z=0;
    std::tie(x,y,std::ignore)= mytuple;
    std::cout << std::get<0>(mytuple) << std::endl;

    //python
    mytuple = (5, 6, 7)
        print(mytuple[0])

    x, y, z = mytuple
    print(x)
  
//C++
    auto myvec = std::vector<int>{ 1, 2, 3, 4 };
    myvec.push_back(5);

    //python
    myvec = [1, 2, 3, 4]
        myvec.append(5);
   
    //C++
    std::map<int, const char*>(mymap) = { { 1, "a" }, { 2, "b" } };

    std::map<int, const char*>::const_iterator it;
    for (it = mymap.begin(); it != mymap.end(); ++it)
        std::cout << it->first << "=" << it->second << std::endl;

    //python
    mymap = { 1: "a", 2 : "b" }
    print(mymap[1])


    for key in mymap :
        print(key, '=', mymap[key])

    for key, value in mymap.items() :
        print(key, '=', value)

関数の定義
//C++
void myfunction(int parameter)
{

}
//python
def pyfunction(parameter) :
print(parameter)