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?
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 previous message
huadong is currently offline  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

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Simple way to develope 2D Game
Next Topic: Questions on the editor and hotkeys. And yet.
Goto Forum:
  


Current Time: Mon Apr 29 01:25:43 CEST 2024

Total time taken to generate the page: 0.05588 seconds