www.digitalmars.com         C & C++   DMDScript  

D - [Beginner] How to add elements to a dynamic array?

reply Dave Sieber <dsieber spamnot.sbcglobal.net> writes:
I want to build an array dynamically, similar to std::vector in C++ :

std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

Can this be done with D arrays?

-- 
dave
Mar 26 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
int[] v;
v ~= 1;
v ~= 2;
v ~= 3;

"Dave Sieber" <dsieber spamnot.sbcglobal.net> wrote in message
news:Xns94B860C93E900dsiebersbc 63.105.9.61...
 I want to build an array dynamically, similar to std::vector in C++ :

 std::vector<int> v;
 v.push_back(1);
 v.push_back(2);
 v.push_back(3);

 Can this be done with D arrays?

 --
 dave

Mar 26 2004
parent Dave Sieber <dsieber spamnot.sbcglobal.net> writes:
"Ivan Senji" <ivan.senji public.srce.hr> wrote:

 int[] v;
 v ~= 1;
 v ~= 2;
 v ~= 3;

Thank you, Ivan. I had seen this notation in the docs, but the doc said it was for array concatenation. In my case, I wanted to add a single element, not another array, so I assumed it was not the correct way. IAC, it compiles with your suggested notation. Now, to test it... :-) -- dave
Mar 26 2004