C++和Python语法对照

Python

C++

i = 10

print(type(i))

#include

using namespace std;

int main() {

int i = 0;

cout << typeid(i).name()<< endl;

system("pause");

return 0;

}

for i in range(1,10):

print(i)

#include

using namespace std;

int main() {

for (int i = 0; i < 10; i++) {

cout << i << endl;

}

system("pause");

return 0;

}

i = 10

if i > 9:

print("True")

else:

print("False")

#include

using namespace std;

int main() {

int i = 10;

if (i > 9) {

cout<<"True"<

}

else

{

cout << "False" << endl;

}

system("pause");

return 0;

}

int a=0;

float b = 0;

std::string c = "abc";

a = 0

b = 0

c = "abc"

while (true)

{


}

while True:


#include

std::map x;

x["one"] = 1;

x["two"] = 2;

x = {"one":1, "two":2}

strs = ["a","b","c"]

ints = [1,2,3]

#追加元素

ints.append(4)

#翻转

x = reversed(ints)

#or

y = x[::-1]

std::vector strs{"a","c","d"};

std::vector ints{1,2,3};

//追加元素

ints.pus_back(4);

//翻转

#include

std::reverse(begin(ints),end(ints));

def print(str):

print(str)

return True

bool print_(std::string str)

{

std::cout<std::endl;

return true;

}

print("Hello World")
std::cout << "Hello World" << std::endl;

def square(x):

return x*x

int square(int x){

return x*x;

}

for i, x in enumerate(items):

print i, x

std::size_t i = 0;

for(const auto & x: items) {

std::cout << "Item " << i++ << " = " << x << std::endl;

}

a, b = b, a

swap(a, b);

i = int(string)

int i = std::atoi(string);

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章