I would casually say that C + + has been "copying" Python in recent years? I will only say that I am using C + + to learn Python.
Don't believe me? To follow my study?
Literal quantity
Python supported the binary as a literal 1 as early as the 2.6 release, and recently c++14 gradually matured, just supported by this 2:
Copy the Code code as follows:
static const int primes = 0B10100000100010100010100010101100;
Not to mention Python has the concept of raw string literals in the 1.5 era 3, we C + + is not too late, C++11 also has a similar approach:
Copy the Code code as follows:
Const char* Path = r "C:\Python27\Doc";
Range Loop
Python Writing for loops is a very comfortable thing:
Copy the 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 the Code code as follows:
for (int x:mylist)
Std::cout << x;
Type Auto Derivation
Is there really a type concept in Python? (Laughter
Copy the Code code as follows:
x = "Hello World"
Print (x)
C++11 also learned this trick, only to keep the old lady's binding cloth (auto).
Copy the Code code as follows:
Auto x = "Hello World";
Std::cout << x;
Meta-group
The tuple (tuple) in Python has long been a envy, and Python has been there since the beginning.
Copy the Code code as follows:
Triple = (5, "Hello", True)
Print (Triple[0])
Well, I'll use c++11 Tiger:
Copy the Code code as follows:
Auto triple = Std::make_tuple (5, "Hello", true);
Std::cout << std::get<0> (triple);
Some people say, Python Dafa is good, but also can reverse the analysis of variables?
Copy the Code code as follows:
X, y, z = triple
Hum, C + + can't?
Copy the Code code as follows:
Std::tie (x, y, z) = triple;
Lists
Python, Lists is built-in type 4, creating a list is incredibly simple:
Copy the Code code as follows:
MyList = [1, 2, 3, 4]
Mylist.append (5);
In the past we can say, what is this, std::vector almost also capable of this matter. Can Python powder take seriously, can you initialize like above? This let Bjarne Stroustrup father Heard, secretly ashamed, so in c++11 the whole out of a initializer_list to respond 5.
Copy the Code code as follows:
Auto MyList = std::vector {1,2,3,4};
Mylist.push_back (5);
And then again, Python creates a Dictionary that's as simple as 6.
Copy the Code code as follows:
Mydict = {5: "foo", 6: "Bar"}
Print (Mydict[5])
Cut, C + + itself has a map type, and now a hash table unordered_map, more like:
Copy the Code code as follows:
Auto Mydict = Std::unordered_map {{5, ' foo '}, {6, ' Bar '}};
Std::cout << mydict[5];
Lambda expression
Python presents a great artifact, a LAMBDA expression in 1994:
Copy the 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 the Code code as follows:
def adder (amount):
Return Lambda x:x + amount
...
Print (Adder (5) (5))
C++11 not to be outdone, the entire concept of the Capture-list 8.
Copy the Code code as follows:
Auto Adder (int amount) {
return [=] (int x) {return x + amount;};
}
...
Std::cout << Adder (5) (5);
Built-in algorithms
Python has many built-in powerful algorithm functions, such as filter:
result = Filter (mylist, lambda x:x >= 0)
C++11 can also use std::copy_if to do the same thing:
Copy the Code code as follows:
Auto result = Std::vector {};
Std::copy_if (Mylist.begin (), Mylist.end (), Std::back_inserter (Result), [] (int x) {return x >= 0;}); Functions such as
are not uncommon, and are echoed in Python: Transform, any_of, all_of, Min, max.
mutable Parameters
Python supports mutable parameters from the outset. You can define a variable parameter function, the number can be indeterminate, and the type can be different. The
Copy Code code is as follows:
def foo (*args):
for x in args:
print (x);
Foo (5, "Hello", True)
C++11 initializer_list can support variable-type parameters (C + + Primer 5th 6.2.6). The
Copy Code code is as follows:
void foo (std::initializer_list 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, it can be seen that @Milo Yip is also a fellow person.
Continue
Do you feel good? Want to make a fist? Look at this repo. There are more ways to teach you to learn Python in C + +.