Learn Python_python through C + +

Source: Internet
Author: User

I would casually say that C + + in recent years began to "copy" Python? I'll just say, I'm learning Python in C + +.

Don't believe it? To follow me to learn?

Literal amount

Python supported the binary as a literal 1 as early as version 2.6, and recently c++14 gradually matured, just to support this 2:

Copy Code code as follows:

static const int primes = 0B10100000100010100010100010101100;

Let alone Python in the 1.5 era has a raw string literals concept 3, we C + + is not too late, C++11 also has a similar approach:

Copy Code code as follows:

Const char* Path = r "C:\Python27\Doc";
Range Loop

Python Writing for loops is a very refreshing thing to do:

Copy Code code as follows:

For x in MyList:
print (x);

As we all know, I can finally do the same thing in c++11:

Copy Code code as follows:

for (int x:mylist)
Std::cout << x;

Type Auto Derivation

Is there really a type concept in Python? (Laughter

Copy Code code as follows:

x = "Hello World"
Print (x)

C++11 also learned this trick, but kept the old lady's laceless cloth (auto).

Copy Code code as follows:

Auto x = "Hello World";
Std::cout << x;

META Group

Python's tuple (tuple) has been admired for so long that Python has it from the start.

Copy Code code as follows:

Triple = (5, "Hello", True)
Print (Triple[0])

Well, I'll use c++11 suit:

Copy Code code as follows:

Auto triple = Std::make_tuple (5, "Hello", true);
Std::cout << std::get<0> (triple);

It's been said that Python Dafa is good and can converse into variables.

Copy Code code as follows:

X, y, z = triple

Hmm, C + + not?

Copy Code code as follows:

Std::tie (x, y, z) = triple;

Lists

In Python, Lists is a built-in type 4, creating a list that is incredibly simple:

Copy Code code as follows:

MyList = [1, 2, 3, 4]
Mylist.append (5);

Before we could say, there was something about it, and std::vector could do it. Can Python powder be serious, can you initialize like the above? This let Bjarne Stroustrup father Heard, secretly ashamed, so in c++11 the whole out a initializer_list respond 5.

Copy Code code as follows:

Auto MyList = std::vector<int>{1,2,3,4};
Mylist.push_back (5);

And, you know, Python creates a Dictionary that's just as simple as 6.

Copy Code code as follows:

Mydict = {5: "foo", 6: "Bar"}
Print (Mydict[5])

Cut, C + + itself has a map type, now has a hash table unordered_map, more like:

Copy Code code as follows:

Auto Mydict = std::unordered_map<int, const char*>{{5, "foo"}, {6, "Bar"}};
Std::cout << mydict[5];

Lambda expression

Python sacrifices a big artifact, and the LAMBDA expression of the 1994:

Copy Code code as follows:

Mylist.sort (key = Lambda x:abs (x))

C++11 began a botched imitation:

Std::sort (Mylist.begin (), Mylist.end (), [] (int x, int y) {return std::abs (x) < Std::abs (y);});
And Python added a force in 2001, introducing the technology of Nested Scopes 7:

Copy Code code as follows:

def adder (amount):
Return Lambda x:x + amount
...
Print (Adder (5) (5))

C++11 not to be outdone, the whole out of the concept of capture-list 8.

Copy Code code as follows:

Auto Adder (int amount) {
return [=] (int x) {return x + amount;};
}
...
Std::cout << Adder (5) (5);

Built-in algorithm

Python has a lot of built-in powerful algorithm functions, such as filter:

result = Filter (mylist, lambda x:x >= 0)
C++11 can do the same thing with std::copy_if:

Copy Code code as follows:

Auto result = std::vector<int>{};
Std::copy_if (Mylist.begin (), Mylist.end (), Std::back_inserter (Result), [] (int x) {return x >= 0;});

Such functions are common in <algorithm> and are echoed in some of Python's functions: Transform, Any_of, all_of, Min, max.

Variable parameters

Python supports variable parameters from the outset. You can define a function of a variable parameter, the number of which can be indeterminate, and the type can be different.

Copy Code code as follows:

def foo (*args):
For x in args:
print (x);
Foo (5, "Hello", True)

C++11 initializer_list can support parameters with variable number of types (c + + Primer 5th 6.2.6).

Copy Code code as follows:

void foo (std::initializer_list<int> il) {
for (auto X:il)
Std::cout << x;
}

Foo ({4, 5, 6});

See here, do you find it a wonderful way to learn Python in C + +? From the answer to this question, we can see that @Milo Yip is also the same people.

Go on

Feel good? Want to do something? Look at this repo. There are more ways to teach you to use C + + to learn Python.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.