U++ Forum - RDF feed
https://www.ultimatepp.org/forums/
How do I construct a Vector from a C-style array?
https://www.ultimatepp.org/forums/index.php?t=rview&goto=52421&th=10800#msg_52421
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?]]>laleksic2019-09-22T18:16:56-00:00Re: How do I construct a Vector from a C-style array?
https://www.ultimatepp.org/forums/index.php?t=rview&goto=52423&th=10800#msg_52423
#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);
}]]>huadong2019-09-23T07:56:56-00:00Re: How do I construct a Vector from a C-style array?
https://www.ultimatepp.org/forums/index.php?t=rview&goto=52424&th=10800#msg_52424
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.]]>laleksic2019-09-23T08:06:08-00:00Re: How do I construct a Vector from a C-style array?
https://www.ultimatepp.org/forums/index.php?t=rview&goto=52425&th=10800#msg_52425
#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);
}]]>huadong2019-09-23T08:33:20-00:00Re: How do I construct a Vector from a C-style array?
https://www.ultimatepp.org/forums/index.php?t=rview&goto=52426&th=10800#msg_52426
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?
]]>mirek2019-09-23T09:47:32-00:00Re: How do I construct a Vector from a C-style array?
https://www.ultimatepp.org/forums/index.php?t=rview&goto=52469&th=10800#msg_52469
]]>Didier2019-10-03T20:58:05-00:00