Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

Solution for iOS 9:

***NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];***

language = "en-US"

***NSDictionary *languageDic = [NSLocale componentsFromLocaleIdentifier:language];***

languageDic will have the needed components

***NSString *countryCode = [languageDic objectForKey:@"kCFLocaleCountryCodeKey"];***

countryCode = "US"

***NSString *languageCode = [languageDic objectForKey:@"kCFLocaleLanguageCodeKey"];***
3 years ago
Since your coordinates belong together as pairs, why not write a struct for them?

***struct CoordinatePair
{
int x;
int y;
};***

Then you can write an overloaded extraction operator for istreams:

***std::istream& operator>>(std::istream& is, CoordinatePair& coordinates)
{
is >> coordinates.x >> coordinates.y;

return is;
}***

And then you can read a file of coordinates straight into a vector like this:

***#include
#include
#include

int main()
{
char filename[] = "coordinates.txt";
std::vector v;
std::ifstream ifs(filename);
if (ifs) {
std::copy(std::istream_iterator(ifs),
std::istream_iterator(),
std::back_inserter(v));
}
else {
std::cerr << "Couldn't open " << filename << " for reading\n";
}
// Now you can work with the contents of v
}***
3 years ago
**argc** is the number of arguments being passed into your program from the command line and **argv** is the array of arguments.

You can loop through the arguments knowing the number of them like:

***for(int i = 0; i < argc; i++)
{
// argv[i] is the argument at index i
}***
3 years ago
You can use the following:

***int *ary = new int[sizeX * sizeY];***

Then you can access elements as:

***ary[y*sizeX + x]***

Don't forget to use delete[] on ary.
3 years ago
use the **atoi()** function to convert the string to an integer:

***string a = "25";

int b = atoi(a.c_str());***
3 years ago
The erase method will be used in two ways:

Erasing single element:

***vector.erase( vector.begin() + 3 ); // Deleting the fourth element***

Erasing range of elements:

***vector.erase( vector.begin() + 3, vector.begin() + 5 ); // Deleting from fourth element to sixth element
***
3 years ago
You can try using the find function:

***string str ("There are two needles in this haystack.");
string str2 ("needle");

if (str.find(str2) != string::npos) {
//.. found.
} ***
3 years ago
Just use len(arr):

***>>> import array
>>> arr = array.array('i')
>>> arr.append('2')
>>> arr.__len__()
1
>>> len(arr)
1***
3 years ago
Assuming .indexOf() is implemented

***Object.defineProperty( Array.prototype,'has',
{
value:function(o, flag){
if (flag === undefined) {
return this.indexOf(o) !== -1;
} else { // only for raw js object
for(var v in this) {
if( JSON.stringify(this[v]) === JSON.stringify(o)) return true;
}
return false;
},
// writable:false,
// enumerable:false
})***

!!! do not make ***Array.prototype.has=function(){...*** because you'll add an enumerable element in every array and js is broken.

***//use like
[22 ,'a', {prop:'x'}].has(12) // false
["a","b"].has("a") // true

[1,{a:1}].has({a:1},1) // true
[1,{a:1}].has({a:1}) // false***

the use of 2nd arg (flag) forces comparison by value instead of reference

comparing raw objects

***[o1].has(o2,true) // true if every level value is same***
3 years ago
Consider the following PHP script:

*** $arr = array('1', '', '2', '3', '0');
// Incorrect:
print_r(array_filter($arr));
// Correct:
print_r(array_filter($arr, 'strlen'));***
3 years ago
With list slicing, see the Python tutorial about lists for more details:

***>>> l = [0, 1, 2, 3, 4]
>>> l[1:]
[1, 2, 3, 4]***
3 years ago
There’s a simple solution without the need for a 3rd party library, we can do that using java 9

***InputStream is;

byte[] array = is.readAllBytes();***

Note also the convenience methods readNBytes(byte[] b, int off, int len) and transferTo(OutputStream) addressing recurring needs.
3 years ago