Container access in std.container
David Nadlinger
see at klickverbot.at
Mon Mar 28 07:48:21 PDT 2011
On 3/28/11 4:17 PM, Ishan Thilina wrote:
> I know that D has some Containers implemented by default( such as a a List,
> Red-black tree, Array). In C++ these data structures can be used as follows.
>
> #include<vector>
>
> int main(){
>
> std::vector<int> myVector;
> std::vector<int>::iterator myIterator;
>
> }
>
> Then I can use "myIterator" to manipulate "myVector".
>
> But in D the containers are embedded in the std.container( as far as I
> understood it) and I can't do "import std.container" too. So how can I access
> the built in containers?
Hi Ishan,
First, to avoid confusion in further discussions, the term »built-in« is
usually used when referring to the types which are part of the D
language itself (e.g. the built-in arrays and associative arrays, i.e.
int[] and int[string]). The types std.container, on the other hand,
could be in any other library as well, so I wouldn't call them built-in,
but rather just Phobos containers.
What exactly do you mean by »I can't do "import std.container"«?
Assuming you have a working D2 environment, you should be able to use
them like this:
---
import std.container;
import std.stdio;
void main() {
auto rb = redBlackTree(4, 1, 2, 3);
foreach (e; rb) {
writeln(e);
}
}
---
To get a range over all elements (in an container-defined order), use
the slicing operator [], e.g. rb[] – if I remember correctly, the
std.container ddoc page has more information on commonly supported
operations.
David
More information about the Digitalmars-d-learn
mailing list