Search on this site
Search in forums

Home » Community » Newbie corner » How do I construct a Vector from a C-style array?
How do I construct a Vector from a C-style array? Sun, 22 September 2019 20:16
 laleksic Messages: 2Registered: September 2019 Junior Member
Hello,
Given a C-style array (pointer + length), how would I construct a Vector from it?

Edit: To clarify, how do I wrap a Vector around it without copying the data?

[Updated on: Mon, 23 September 2019 10:15]

Report message to a moderator

Re: How do I construct a Vector from a C-style array? [message #52423 is a reply to message #52421] Mon, 23 September 2019 09:56
 huadong Messages: 5Registered: September 2019 Promising Member
Are you looking what the code like this?

#include <Core/Core.h>

using namespace Upp;

int main()
{
int a[] = {1, 2, 3, 4};
int *p = a;
int n = 4;

Vector<int> v;
for (auto begin = p, end = p+n; begin != end; ++begin)
v << *begin;

for (int &x : v)
printf("x = %d\n", x);
}
Re: How do I construct a Vector from a C-style array? [message #52424 is a reply to message #52423] Mon, 23 September 2019 10:06
 laleksic Messages: 2Registered: September 2019 Junior Member
Thanks,
This copies the elements one by one in a loop.

I was thinking is there a way to instantly (without copying) wrap a C-style array in a Vector? Since if I understand, Vector is a wrapper around C-style arrays.
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: 5Registered: 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

Re: How do I construct a Vector from a C-style array? [message #52426 is a reply to message #52421] Mon, 23 September 2019 11:47
 mirek Messages: 13984Registered: November 2005 Ultimate Member
laleksic wrote on Sun, 22 September 2019 20:16
Hello,
Given a C-style array (pointer + length), how would I construct a Vector from it?

Edit: To clarify, how do I wrap a Vector around it without copying the data?

```	int x[3] = { 1, 2, 3 };
Vector<int> h;
h.SetRange(0, SubRange(x, 3));
DDUMP(h);
```
Re: How do I construct a Vector from a C-style array? [message #52469 is a reply to message #52426] Thu, 03 October 2019 22:58
 Didier Messages: 680Registered: November 2008 Location: France Contributor
Nice, I didn't know Vector class could do this
 Previous Topic: Simple way to develope 2D Game Next Topic: Questions on the editor and hotkeys. And yet.
Goto Forum:

Current Time: Thu Jun 13 08:52:22 CEST 2024

Total time taken to generate the page: 0.02174 seconds