Even if you can write it concisely in Python, you need to write a huge amount of template code in C ++ 98. For reference, I arranged the C ++ and Python code side by side.
In a nutshell, "Syntactic sugar and dynamic typing are great."
(Because it is about to be written, there may be more items in the future)
Point get_distance(const Point& point1, const Point& point2) {
return sqrt( pow(point1.x - point2.x, 2) + pow(point1.y - point2.y, 2);
}
def get_distance(point1, point2):
pt1_x, pt1_y = point1
pt2_x, pt2_y = point2
return math.sqrt((pt1_x - pt2_x) ** 2, (pt1_y - pt2_y) ** 2)
C ++: In addition to types, modifiers such as const, *, &, and templates are annoying. (I / O is specified, and syntax check at compile time is helpful, but ...) Python: Dynamically typed, no type definition required. The type has a value, not a variable.
vector<double> calculate(const vector<Line>& lines) {
vector<double> result;
vector<Line>::const_iterator ite = lines.begin();
while( ite != lines.end() ) {
double value =Do something(*ite);
result.push_back(value);
++ite;
}
return result;
}
def calculate(lines):
result = [] // []In list format, {}Then the dictionary.
for line in lines:
value =Do something(line)
result.append(value)
return result
C ++: In C ++ 98, there is no syntactic sugar (C ++ 11 has syntactic sugar).
python: Object creation, element access, syntax sugar in loops. The above case can be described more concisely using list comprehensions.
def calculate(lines):
result = [Do something(line) for line in lines]
return result
void foo() {
Point pt1(0,1);
Point pt2(0,5);
Line line(pt1, pt2);
double score;
double distance;
calculate(line, &score, &distance);
...
}
void calculate(const Line& line, double* out_score, double* out_distance) {
*out_distance = get_distance(line.point1, line.point2);
*out_score = get_score(line);
}
double get_distance(const Point& pt1, const Point& pt2) {...}
def foo():
pt1 = (0,1)
pt2 = (0,5)
line = (pt1, pt2)
distance, score = caclculate(line)
...
def calculate(line):
distance = get_distance(*line)
score = get_score(line)
return distance, score
def get_distance(pt1, pt2):
...
C ++: If you want to group values, you need to use a generic pair or define a struct / class like Point or Line. This is also a trade-off with syntax checking.
Python: Tuples come in handy when you want to pass a small pair as an argument. Overuse will reduce readability.
void foo(const Line& line, double x) {
double y1 = func(line, x);
double y2 = func(line, x+1);
}
//Find the y coordinate corresponding to x on the straight line.
// ※:y=1 or x=Do not consider lines such as 0.
double func(const Line& line, double x) {
//Get tilt
double slope = get_slope(line.point1, line.point2);
double const_value = get_const_value(line.point1, slope);
double y = x * slope + c;
return y;
}
def foo(line, x):
func = get_func(line)
y1 = func(x)
y2 = func(x + 1)
def get_func(line):
slope = get_slope(*line)
const_value = get_const_value(line[0], slope)
return (lamda x: x * slope + c)
Lambda expressions are provided in python, so you can easily write small things. In C ++, writing a class improves reusability, but the threshold for writing is high.
You can easily expand tuples and lists into individual arguments, or create named arguments.
Example: Reduce C ++ function overloads.
doule get_distance(const Point& pt1, const Point pt2) {
return //Calculation result
}
double get_distance(const Line& line) {
return get_distance(line.point1, line.point2);
}
def get_distance(point1, point2):
return ...
def foo(line):
get_distance(*line) //If line is a tuple or sequence, point1 automatically,Expand to point2.
Recommended Posts