www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - phobos std.container example

reply novice2 <sorry noem.ail> writes:
Hi.
Woud anybody please show phobos std.container usage sample?
I need list/array of objects/structures.
Every item have list/array of another objects/structures.
I need possibility:
1) to construct such lists/arrays;
2) to enumerate items
3) to search item by "name" field
4) insert/remove/replace item

I tried to read docs with dmd2 distribution, but failed to understand. I never
used templates. Sorry my dumbness.

Tanks.
Dec 30 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
novice2:

 3) to search item by "name" field
Do you need an associative array then? They are built-in and they don't need template syntax.
 I tried to read docs with dmd2 distribution, but failed to understand.
dmd2 docs are not written in a easy, they often lack examples, etc.
 I never used templates.
I suggest you to start experimenting with D2 templates, because they are used often in D2 code. Bye, bearophile
Dec 30 2010
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
novice2 wrote:

 Woud anybody please show phobos std.container usage sample?
 I need list/array of objects/structures.
SList is a singly-linked list, and Array is an array.
 Every item have list/array of another objects/structures.
 I need possibility:
 1) to construct such lists/arrays;
Trivial. :)
 2) to enumerate items
You mean iterate over the elements?
 3) to search item by "name" field
You can only linear search on an SList. You can do better with Array if it's ok to sort the elements before-hand; then you can binary search.
 4) insert/remove/replace item
Trivial.
 I tried to read docs with dmd2 distribution, but failed to understand.
You're not alone. :) An SList example: import std.stdio; import std.container; import std.algorithm; void main() { SList!int myList; foreach (i; 0 .. 10) { if (i % 2) { /* Insert at the front */ myList.insert(i); } else { /* Insert at the end */ myList.insertAfter(myList[], i); } } /* Remove the element at the front */ myList.removeFront(); /* Iterate over all of the elements */ foreach (element; myList) { writeln(element); } } Using it with user types is as simple as using your type instead of int: import std.stdio; import std.container; import std.algorithm; import std.string; struct Point { double x; double y; string toString() { return format("(%s,%s)", x, y); } } void main() { SList!Point myList; foreach (i; 0 .. 10) { if (i % 2) { /* Insert at the front */ myList.insert(Point(i, i)); } else { /* Insert at the end */ myList.insertAfter(myList[], Point(i, i)); } } /* Remove the element at the front */ myList.removeFront(); /* Iterate over all of the elements */ foreach (element; myList) { writeln(element); } } Other containers would be used similarly. Ali
Dec 31 2010
parent novice2 <sorry noem.ail> writes:
thank you Ali and bearophile!
Dec 31 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
This might be not what you want, but there is a more complete alternative  
to std.container which works with D2:

http://www.dsource.org/projects/dcollections

You may find more familiarity there.

-Steve
Dec 31 2010