I really not satisfied with mql4 array function.In mql4 reference they cannot explain why need to use this function.
example why I need to use arrayinitialize function
When you declare some array, it may contain some garbage there. Most likely you will have default values, like 0 or NULL but garbage may stay there as well.
By use of 'ArrayInitialize()` function you can be sure that all the values in your array are the values you've put there.
string arr2str(const int &array[])//fn to print array, ugly, ends with ,|
{
string result="|";
for(int i=0;i<ArraySize(array);i++)
{
result+=(string)i+"="+(string)array[i]+", ";
}
return result+"|";
}
void OnTick()
{
int array[8];
printf("1: %d. %s",ArraySize(array),arr2str(array));
//receive: 1: 8. |0=0, 1=0, 2=0, 3=0, 4=1995110657, 5=146315416, 6=1, 7=85975080, |
int result=ArrayInitialize(array,7);
printf("2: %d %d. %s",result,ArraySize(array),arr2str(array));
//receive: 2: 8 8. |0=7, 1=7, 2=7, 3=7, 4=7, 5=7, 6=7, 7=7, |
ExpertRemove();//to stop the test
}
As you can see, first array output (1:) has some strange data. After initialization, no problem with it (2:) - all are sevens since I put 7 as second param in the function, it could be and most likely 0 instead of 7 in your codes.
Related
I'm currently debugging some code and am confused as to how the following is possible:
void DoSomething(int cell, const std::multimap<int, const Foo*>& map) {
auto range = map.equal_range(cell);
if (range.first != map.end()) {
int iterated = 0;
for (auto iter = range.first; iter != range.second; ++iter) {
iterated++;
}
assert(iterated > 0);
}
}
based on my understanding of std::multimap this assertion should in any case always pass, yet it fails sometimes with iterated = 0.
Under what circumstances can this be possible?
Ok I figured it out.
I was under the wrong assumption that equal_range() would return end() as the first iterator if the multimap does not contain the requested key, but that's not correct.
If the multimap does not contain any elements for a certain key, it does not return map.end() for the first iterator, but instead it returns an iterator to the first element with a key Not Less than the requested key. So, if the multimap doesn't contain the key requested, if (range.first != map.end()) will still pass, since both the first as well as the second iterator will both point to the element with the next larger key, but then there will be no iteration.
I want to use doubles in my code but Dart converts them to ints (or dynamic).
I have tried to cast them using 'as' or .toDouble() but they do not stay as doubles:
(in dartpad)
main() {
print ((1.0 as double).runtimeType);
print (1.0.toDouble().runtimeType);
}
(output)
int
int
I would have expected the output of these statements to be 'double'. If I change the value to 1.1 I get a double out.
I am trying to query my textfield.text input (Let's say the user types 5 in the keypad).
Then convert the string to an integer.
Then make a for loop and run the for loop X amount of times. (X being the integer converted from the string).
I have tried the below however I can't work out the syntax/format for the for loop.
var shotCountInt = Int(numberOfShots.text!)
for item in 0..<shotCountInt {
//do something
}
The error I am getting is on the for loop which is:
Type 'Int?' does not conform to protocol 'Sequence'
I think the problem is in 0..<shotCountInt of optional Int , so
if let shotCountInt = Int(numberOfShots.text!) {
for item in 0..<shotCountInt {
// proceed here
}
}
While converting from Swift 2.3 to 3.2 I received below error.
Error : Binary operator cannot be applied to operands of type Int and String
for this if Condition i.e if (error?.code)! == "-112" which is shown in below line.
if (error?.code)! == "-112"
{
print("hello")
}
Error itself says it's different types Int and String.
You can need to typecast one or another in same form and them compare.
if (String(error?.code)!) == "-112"){
print("hello")
}
Swift is a language with a strong type system. You can compare only values of the same type.
Since the left side is Int anyway use an Int value for the right side. Creating a string is unnecessarily expensive. Don’t do that.
The most efficient (and safe) solution is
if error?.code == -112
{
print("hello")
}
You need to type-cast your error code result to a string, like so:
if String(error?.code)!) == "-112" {
print("Hello")
}
Essentially, you are taking the error?.code, "casting" it as a string by placing it in a string "container mould" and unwrapping the value (retrieving the casted result).
In addition, if you are working with an API response, you have to account for all other error codes in the else/if statement to make sure all responses are handled properly (just in case you are).
I have a question.
When working with Dart, I can't check to see if 2 arrays are equal.
(in other languages, I can do with ==)
In fact, I just can do that == with String or number.
List arr1 = [1,2,3];
List arr2 = [1,2,3];
if (arr1 == arr2) {
print("equal");
} else {
print("not equal");
}
// Output: not equal.
So I wonder how does that make sense. I mean, How we can do if the == is just work for the cases of String or number (if the values compared are the same).
How do I have to do if I want to check that kind of comparison (equal) for List, Map, ..
It just work for String & number.
arr1 and arr2 are different instances of an object of type List. By default different instances are always different.
When a class implements a custom == operator it can override this behavior. Some classes have a custom implementation by default like int and String.
This can easily be done for immutable objects but not for mutable. One reason is that usually the hashCode is calculated from the values stroed in a class and the hashCode must not change for an instance because this can for example cause that an instance stored in a map can't be retrieved anymore when the hashcode of the key changed.
As a workaround there is a library that provides helper functions to compare lists/iterables.
import 'package:collection/equality.dart';
void main(List<String> args) {
if (const IterableEquality().equals([1,2,3],[1,2,3])) {
// if (const SetEquality().equals([1,2,3].toSet(),[1,2,3].toSet())) {
print("Equal");
} else {
print("Not equal");
}
}