What Is the Simplest Way to Convert Array to Vector

What is the simplest way to convert array to vector?

Use the vector constructor that takes two iterators, note that pointers are valid iterators, and use the implicit conversion from arrays to pointers:

int x[3] = {1, 2, 3};
std::vector<int> v(x, x + sizeof x / sizeof x[0]);
test(v);

or

test(std::vector<int>(x, x + sizeof x / sizeof x[0]));

where sizeof x / sizeof x[0] is obviously 3 in this context; it's the generic way of getting the number of elements in an array. Note that x + sizeof x / sizeof x[0] points one element beyond the last element.

converting array to vector

This is how one converts an array to a vector as part of the vector definition:

std::vector<int> v(num1, num1+4);

This is how one converts an array to a vector not as part of the vector definition:

std::vector<int> v;
v.assign(num1, num1+4);

Or perhaps you meant "convert my program to use vectors instead of arrays":

std::vector<int> num1 = { 2, 4, 1, 8};

Best way to convert an array of integers to a vector? in java

NB: Before I answer the question, Vector is deprecated and should never be used. You're looking for List. Whatever property Vector has that makes you think you need it, I really doubt it.

The problem here is that List (and Vector) are generified, and generics cannot be a primitive type. So, int[] and Integer[] aren't the same thing, and for the same reason, you can't casually turn an int[] into a List<Integer>. Instead, Arrays.asList(someIntArray) gives you a list with only one element in it, and that list is of type List<int[]> - in other words, nums is treated as the single element of a list of int arrays.

There is no method in the java.* libraries that turns an int[] into a List<Integer> (or Vector<Integer>, naturally).

An easy-ish way to do it:

List<Integer> list = Arrays.stream(nums).mapToObj(x -> x).toList();

This will first create an IntStream of your numbers. It then maps the int values to Integer values, using the identity operation which gets automatically sugared into x -> Integer.valueOf(x) (if you find it more readable like so, then go ahead and write it like that), and then turns the whole thing into a list. If toList() doesn't work - you're on java pre-16, write .collect(Collectors.toList()) instead of .toList().

If you don't like the above, a simple for loop would then do the job. This works just as well:

List<Integer> list = new ArrayList<Integer>();
for (int n : nums) list.add(n);

Converting between C++ std::vector and C array without copying

You can get a pointer to the first element as follows:

int* pv = &v[0];

This pointer is only valid as long as the vector is not reallocated. Reallocation happens automatically if you insert more elements than will fit in the vector's remaining capacity (that is, if v.size() + NumberOfNewElements > v.capacity(). You can use v.reserve(NewCapacity) to ensure the vector has a capacity of at least NewCapacity.

Also remember that when the vector gets destroyed, the underlying array gets deleted as well.

Converting between an ActionScript Array (Object[]) and a Vector. Object

For the Array to Vector, use the Vector.<TYPE>() function that accept an array and will return the vector created :

var aObjects:Array = [{a:'1'}, {b:'2'}, {c:'3'}];
// use Vector function
var vObjects:Vector.<Object> = Vector.<Object>(aObjects);

For the other there is no builtin function, so you have to do a loop over each Vector item and put then into an Array

Java Convert Object[] Array to Vector

return new Vector(Arrays.asList(elements));

Now, it may look as if you are copying the data twice, but you aren't. You do get one small temporary object (a List from asList), but this provides a view of the array. Instead of copying it, read and write operations go through to the original array.

It is possible to extends Vector and poke its protected fields. This would give a relatively simple way of having the Vector become a view of the array, as Arrays.asList does. Alternatively, just copying data into the fields. For Java ME, this is about as good as it gets without writing the obvious loop. Untested code:

return new Vector(0) {{
this.elementData = (Object[])elements.clone();
this.elementCount = this.elementData.length;
}};

Of course, you are probably better off with a List than a Vector. 1.4 has completed its End of Service Life period. Even 1.5 has completed most of its EOSL period.



Related Topics



Leave a reply



Submit