Thread overview
D equivilent to "Vector"?
Oct 20, 2004
Nick Sabalausky
Oct 20, 2004
Ben Hinkle
Oct 21, 2004
Nick Sabalausky
Re: D equivilent to
Oct 20, 2004
Sean Kelly
October 20, 2004
This might be a stupid question, but does D have an equivilent to the Vector class that's in C++'s STL and Java?  I want to make a list/array of a bunch of strings and don't know how many I'll have ahead of time, so I want to be able to do something like:

/*some type of collection */ listOfStrings;
/* loop */
{
    listOfStrings.add(/*some string*/);
}

AFAIK, a standard D array doesn't have any sort of addElement() routine. What I've been doing instead is kind of an abuse of associative arrays:

char[][char[]] strings;
/*loop*/
{
    strings[/*new string to add*/] = 1;
}
// The list of strings is now strings.keys

I know I could just write a Vector class or linked list or something, but I wasn't sure if there was a standard one.


October 20, 2004
You can use a dynamic array and the concatenation ~= (or ~) operator

char[][] listOfStrings;
...
listOfStrings ~= "some string";

to add items to the end of the dynamic array. It will resize as needed to make space.

"Nick Sabalausky" <z@a.a> wrote in message news:cl6guj$eee$1@digitaldaemon.com...
> This might be a stupid question, but does D have an equivilent to the
Vector
> class that's in C++'s STL and Java?  I want to make a list/array of a
bunch
> of strings and don't know how many I'll have ahead of time, so I want to
be
> able to do something like:
>
> /*some type of collection */ listOfStrings;
> /* loop */
> {
>     listOfStrings.add(/*some string*/);
> }
>
> AFAIK, a standard D array doesn't have any sort of addElement() routine. What I've been doing instead is kind of an abuse of associative arrays:
>
> char[][char[]] strings;
> /*loop*/
> {
>     strings[/*new string to add*/] = 1;
> }
> // The list of strings is now strings.keys
>
> I know I could just write a Vector class or linked list or something, but
I
> wasn't sure if there was a standard one.
>
>


October 20, 2004
In article <cl6guj$eee$1@digitaldaemon.com>, Nick Sabalausky says...
>
>This might be a stupid question, but does D have an equivilent to the Vector class that's in C++'s STL and Java?  I want to make a list/array of a bunch of strings and don't know how many I'll have ahead of time, so I want to be able to do something like:
>
>/*some type of collection */ listOfStrings;
>/* loop */
>{
>    listOfStrings.add(/*some string*/);
>}
>
>AFAIK, a standard D array doesn't have any sort of addElement() routine. What I've been doing instead is kind of an abuse of associative arrays:
>
>char[][char[]] strings;
>/*loop*/
>{
>    strings[/*new string to add*/] = 1;
>}
>// The list of strings is now strings.keys
>
>I know I could just write a Vector class or linked list or something, but I wasn't sure if there was a standard one.


Yes, though the syntax may be a bit confusing at first.  Array notation is postfix, so char[][5] is an array of 5 arrays of strings, similar to vector<string>(5).  Here's a more detailed example:

# import std.stdio;
#
# void main()
# {
#     char[][] vec;
#     vec.length = 2;
#     vec[0] = "hello";
#     vec[1] = "world";
#     foreach( char[] str; vec )
#         writefln( str );
# }


Sean


October 21, 2004
"Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:cl6hqe$fcq$1@digitaldaemon.com...
> You can use a dynamic array and the concatenation ~= (or ~) operator
>
> char[][] listOfStrings;
> ...
> listOfStrings ~= "some string";
>
> to add items to the end of the dynamic array. It will resize as needed to make space.

Whoa, that's sweet :)  I probably should have thought of that, since D uses ~ as the string concatenation operator and D's strings are nothing more than character arrays.  Come to think of it, I think I noticed that once when looking through the "Arrays" documentation on the main D site but never gave it much thought.

>
> "Nick Sabalausky" <z@a.a> wrote in message news:cl6guj$eee$1@digitaldaemon.com...
>> This might be a stupid question, but does D have an equivilent to the
> Vector
>> class that's in C++'s STL and Java?  I want to make a list/array of a
> bunch
>> of strings and don't know how many I'll have ahead of time, so I want to
> be
>> able to do something like:
>>
>> /*some type of collection */ listOfStrings;
>> /* loop */
>> {
>>     listOfStrings.add(/*some string*/);
>> }
>>
>> AFAIK, a standard D array doesn't have any sort of addElement() routine. What I've been doing instead is kind of an abuse of associative arrays:
>>
>> char[][char[]] strings;
>> /*loop*/
>> {
>>     strings[/*new string to add*/] = 1;
>> }
>> // The list of strings is now strings.keys
>>
>> I know I could just write a Vector class or linked list or something, but
> I
>> wasn't sure if there was a standard one.
>>
>>
>
>