Home » Community » Newbie corner » How do I construct a Vector from a C-style array?
|
|
|
Re: How do I construct a Vector from a C-style array? [message #52425 is a reply to message #52424] |
Mon, 23 September 2019 10:33   |
huadong
Messages: 5 Registered: September 2019
|
Promising Member |
|
|
It seens there is no constructor directly match the c-style array, but we can setup a function for instead
#include <algorithm>
using namespace std;
#include <Core/Core.h>
using namespace Upp;
template <typename T>
Vector<T> makeVector(T *p, int n)
{
Vector<T> v;
v.SetCount(n);
copy(p, p+n, v.begin());
return v; // Vector<T> has a move constructor, and it will be called here for high performance
}
int main()
{
int a[] = {1, 2, 3, 4};
for (auto &x : makeVector(a, 4))
printf("x = %d\n", x);
}
[Updated on: Mon, 23 September 2019 11:32] Report message to a moderator
|
|
|
|
|
Goto Forum:
Current Time: Tue May 13 10:47:25 CEST 2025
Total time taken to generate the page: 0.03184 seconds
|