Signup/Sign In

Answers

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

One method would be to use the array to initialize the vector
***
static const int arr[] = {16,2,77,29};
vector vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
***
3 years ago
Use std::string::find as follows:
***
if (s1.find(s2) != std::string::npos) {
std::cout << "found!" << '\n';
}
***
Note: "found!" will be printed if s2 is a substring of s1, both s1 and s2 are of type std::string.
3 years ago
To delete a single element, you could do:
***
std::vector vec;

vec.push_back(6);
vec.push_back(-17);
vec.push_back(12);

// Deletes the second element (vec[1])
vec.erase(std::next(vec.begin()));
***
Or, to delete more than one element at once:
***
// Deletes the second through third elements (vec[1], vec[2])
vec.erase(std::next(vec.begin(), 1), std::next(vec.begin(), 3));
***
3 years ago
In C++11 there are some nice new convert functions from std::string to a number type.

So instead of
***
atoi( str.c_str() )
***
you can use
***
std::stoi( str )
***
where str is your number as std::string.
3 years ago
***
int *ary = new int[sizeX*sizeY];

// ary[i][j] is then rewritten as
ary[i*sizeY+j]
***
3 years ago
argv and argc are how command line arguments are passed to main() in C and C++.

argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.

The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.

They can also be omitted entirely, yielding int main(), if you do not intend to process command line arguments.

Try the following program:
***
#include

int main(int argc, char** argv) {
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
***
Running it with ./test a1 b2 c3 will output
***
Have 4 arguments:
./test
a1
b2
c3
***
3 years ago
My new favorite form (ES2015)
***
Array(10).fill(1).map((x, y) => x + y)
***
And if you need a function with a step param:
***
const range = (start, stop, step = 1) =>
Array(Math.ceil((stop - start) / step)).fill(start).map((x, y) => x + y * step)
***
3 years ago
You can use the indexOf method like this:
***
var index = array.indexOf(item);
if (index !== -1) {
array.splice(index, 1);
}
***
3 years ago
***
var item = items[Math.floor(Math.random() * items.length)];
***
3 years ago
Use the concat function, like so:
***
var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);
***
The value of newArray will be [1, 2, 3, 4] (arrayA and arrayB remain unchanged; concat creates and returns a new array for the result).
3 years ago
***
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};

//////////////
// Examples //
//////////////

const dif1 = [1,2,3,4,5,6].diff( [3,4,5] );
console.log(dif1); // => [1, 2, 6]


const dif2 = ["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]);
console.log(dif2); // => ["test5", "test6"]
***
3 years ago
You can use Apache Commons IO to handle this and similar tasks.

The IOUtils type has a static method to read an InputStream and return a byte[].
***
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
***
Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray(). It handles large files by copying the bytes in blocks of 4KiB.
3 years ago