digitalmars.D - D equivilent to "Vector"?
- Nick Sabalausky (19/19) Oct 20 2004 This might be a stupid question, but does D have an equivilent to the Ve...
- Ben Hinkle (12/31) Oct 20 2004 You can use a dynamic array and the concatenation ~= (or ~) operator
- Nick Sabalausky (7/45) Oct 20 2004 Whoa, that's sweet :) I probably should have thought of that, since D u...
- Sean Kelly (16/35) Oct 20 2004 Yes, though the syntax may be a bit confusing at first. Array notation ...
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.
Oct 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 theVectorclass that's in C++'s STL and Java? I want to make a list/array of abunchof strings and don't know how many I'll have ahead of time, so I want tobeable 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, butIwasn't sure if there was a standard one.
Oct 20 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 theVectorclass that's in C++'s STL and Java? I want to make a list/array of abunchof strings and don't know how many I'll have ahead of time, so I want tobeable 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, butIwasn't sure if there was a standard one.
Oct 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: Sean
Oct 20 2004