Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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? [message #52421] Sun, 22 September 2019 20:16 Go to next message
laleksic is currently offline  laleksic
Messages: 2
Registered: 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 Go to previous messageGo to next message
kekhuadong2 is currently offline  kekhuadong2
Messages: 5
Registered: 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 Go to previous messageGo to next message
laleksic is currently offline  laleksic
Messages: 2
Registered: 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 Go to previous messageGo to next message
kekhuadong2 is currently offline  kekhuadong2
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

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 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12050
Registered: 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 Go to previous message
Didier is currently offline  Didier
Messages: 488
Registered: November 2008
Location: France
Senior Member
Nice, I didn't know Vector class could do this Smile
Previous Topic: Simple way to develope 2D Game
Next Topic: Questions on the editor and hotkeys. And yet.
Goto Forum:
  


Current Time: Thu Oct 17 12:05:07 CEST 2019

Total time taken to generate the page: 0.01060 seconds