comparison-operators

Why is operator!= removed in C++20 for many standard library types?

僤鯓⒐⒋嵵緔 提交于 2020-03-13 03:49:54
问题 According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains. What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for equality would be just as meaningless as well, wouldn't it? Similarly, operator!= of many other standard library types, including containers such as std::unordered_map::operator!= and std::unordered_set::operator!= will be removed in C++20 according

Why is operator!= removed in C++20 for many standard library types?

偶尔善良 提交于 2020-03-13 03:48:49
问题 According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains. What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for equality would be just as meaningless as well, wouldn't it? Similarly, operator!= of many other standard library types, including containers such as std::unordered_map::operator!= and std::unordered_set::operator!= will be removed in C++20 according

How to see if a certain class exists in a list

给你一囗甜甜゛ 提交于 2020-01-25 08:36:05
问题 I have a class, user , that has an attribute metadata . metadata is a list of objects, each with different classes, for example: user.metatada = [Employee(), Student(), OtherClass()] In an update script, I need to check, if a certain type exists in a list, like so: if type(Employee()) in user.metadata: replace user.metadata[indexOfThatEmployee] with new Employee() else: user.metadata.append(new Employee()) is there anyway to easily to check if a certain type exists in a list? 回答1: Got it.

How is the three-way comparison operator different from subtraction?

こ雲淡風輕ζ 提交于 2020-01-22 04:56:33
问题 There's a new comparison operator <=> in C++20. However I think in most cases a simple subtraction works well: int my_strcmp(const char *a, const char *b) { while (*a == *b && *a != 0 && *b != 0) { a++, b++; } // Version 1 return *a - *b; // Version 2 return *a <=> *b; // Version 3 return ((*a > *b) - (*a < *b)); } They have the same effect. I can't really understand the difference. 回答1: The operator solves the problem with numeric overflow that you get with subtraction: if you subtract a

How to use switch with integer 0 in PHP?

你说的曾经没有我的故事 提交于 2020-01-21 19:10:45
问题 having the integer 0 as switch parameter will take the first result "foo": $data=0; // $data is usually coming from somewhere else, set to 0 here to show the problem switch ($data) : case "anything": echo "foo"; break; case 0: echo "zero"; break; default: echo "bar"; endswitch; How do I change this, so the switch will write "zero" as expected? 回答1: The switch/case statement uses loose comparison, and, like it or not, 0 == "anything" is true : Comparison Operators [...] If you compare a number

How to use switch with integer 0 in PHP?

强颜欢笑 提交于 2020-01-21 19:09:18
问题 having the integer 0 as switch parameter will take the first result "foo": $data=0; // $data is usually coming from somewhere else, set to 0 here to show the problem switch ($data) : case "anything": echo "foo"; break; case 0: echo "zero"; break; default: echo "bar"; endswitch; How do I change this, so the switch will write "zero" as expected? 回答1: The switch/case statement uses loose comparison, and, like it or not, 0 == "anything" is true : Comparison Operators [...] If you compare a number

Parameter to use std::greater or std::less as argument

孤街醉人 提交于 2020-01-13 07:55:09
问题 I would like to make a function with a parameter that accepts either std::greater<int> or std::less<int> as the argument. I'm stuck on the syntax for the parameter, though. This is the format I tried: myFunction(int a, int b, bool *comp(int, int)) { … } … std::greater<int> bigger; myFunction(2, 3, bigger); That doesn't work, though, and I suspect the third parameter is just completely wrong. What should it actually be? cannot convert std::greater<int> to bool* (*)(int, int) 回答1: Functions

Comparing different strings in PHP with == returns true

耗尽温柔 提交于 2020-01-13 05:16:06
问题 I was just debugging a script and found that an if-statement wasn't working the way I expected it to. var_dump("6064365413078728979" == "6064365413078728452"); die(); The code above will result in the following: bool(true) With the === operator it works as expected. Anyone got any ideas why? I'm using PHP Version 5.3.13 with a wamp installation on a x64 windows machine. 回答1: <?php $a=6064365413078728979; $b=6064365413078728452; echo $a."<br>".$b; //var_dump( $a==$b ); die(); ?> When you run

Is there an operation for not less than or not greater than in python?

百般思念 提交于 2020-01-11 07:29:39
问题 Consider a following snippet: a = 0 if a == 0 or a > 0: print(a) Essentially, I want to do something when a is not negative. If instead of this, I had wanted to do something when a is not 0 , I would have simply written: if a != 0 : In the same spirit, I tried : if a !< 0 : assuming the consistency of the Python where user starts guessing the correct implementations once he/she gets used to the language. I was surprised to see that this particular operation does not exist in Python! My

Math-like chaining of the comparison operator - as in, “if ( (5<j<=1) )” [duplicate]

雨燕双飞 提交于 2020-01-08 13:25:29
问题 This question already has answers here : Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so? (5 answers) Closed 2 years ago . int j=42; if( (5<j<=1) ) { printf("yes"); } else { printf("no"); } Output: yes Why does it output yes? Isn't the condition only half true? 回答1: C does not understand math-like syntax, so if(1<j<=5) is not interpreted as you expect and want; it should be if (1 < j && j <= 5) or similar. As explained in other answers, the expression is evaluated as ((1