D: It is not clear how to use std.container structs

I am trying to learn how to use the various container structures available in std.container and I am having trouble understanding how to do the following:

1) How to create an empty container? For example, suppose I have a custom class Fooand you want to create an empty DList that should contain objects Foo. What syntax should I use?

2) Let aand btwo DList!int. I try to call a ~ b, and the compiler tells me that I can’t. However, I see that DListthis operator has been overloaded. What did I miss?

+3
source share
2 answers

There are three ways to create a new container,

  • , std.container.make:

    auto list1 = make!(DList!int)();
    auto list2 = make!(DList!int)(1, 2);
    
  • :

    auto list1 = DList!int();
    
  • :

    auto tree1 = redBlackTree!int();
    auto tree2 = redBlackTree!int(1, 2);
    

, , - . make, , , .

, DList!T, DList!T. :

auto list1 = make!(DList!int)(1, 2);
auto list2 = make!(DList!int)(3, 4);
list1 ~= list2[]; //notice the square brackets, which represent obtaining a slice (InputRange) from the `DList`, or more specifically calling `opSlice`.

, :

import std.container;
import std.stdio;

class Foo {
  int i;

  this(int i) {
    this.i = i;
  }

  void toString(scope void delegate(const(char)[]) sink) const {
    // this is a non allocating version of toString
    // you can use a normal `string toString()` function too
    import std.string : format;
    sink("Foo: %s".format(i));
  }
}

void main() {
  auto odds = make!(DList!Foo)();
  odds.insert(new Foo(3));
  odds.insert(new Foo(5));
  writeln("Odds: ", odds[]);

  odds.insertFront(new Foo(1));
  writeln("Odds: ", odds[]);

  auto evens = make!(DList!Foo)();
  evens.insert(new Foo(2));
  evens.insert(new Foo(4));

  writeln("Evens: ", evens[]);

  odds.insert(evens[]);
  writeln("Odds then evens: ", odds[]);

  odds ~= evens[];
  writeln("Odds then evens x 2: ", odds[]);
}

: http://dpaste.dzfl.pl/f34b2ec8a445

+5
  • , , . DList DList, Foo, auto foos = DList!Foo();
  • DList, , , a ~ b. , ~=, a ~ b a ~= b

:

import std.stdio;
import std.container;

class Foo {
    private int _val;

    public this(int arg) { _val = arg; }
    public int val() @property { return _val; }
    public void val(int arg) @property { _val = arg; }
}

int main() {
    auto foos = DList!Foo();
    auto a = DList!Foo();
    a.insert(new Foo(5));
    auto b = DList!Foo();
    b.insert(new Foo(11));

    foos ~= a[]; // slice
    foos ~= b[]; // slice
    // writeln(foos[0].val); // "Error: no [] operator overload for type DList!(Foo)"
    writeln(foos.front.val); // prints 5
    writeln(foos.back.val); // prints 11

    return 0;
}

, DList . - , ! ( ) http://dlang.org/phobos/std_container.html, , length. , a ~ b ! , Array! T. , std.container , c [x] ( c - ). DList! T .

Array DList. ( -), , , length.

:

import std.stdio;
import std.container;

class Foo {
    private int _val;

    public this(int arg) { _val = arg; }
    public int val() @property { return _val; }
    public void val(int arg) @property { _val = arg; }
}

int main() {
    auto foos = Array!Foo();
    auto a = Array!Foo();
    a.insert(new Foo(5));
    auto b = Array!Foo();
    b.insert(new Foo(11));

    foos = a ~ b; // concatenation works, ah joy! :)

    writeln(foos[0].val); // indexed access! yeah! prints 5
    writeln(foos[1].val); // prints 11

    // Lets the user press <Return> before program returns
    stdin.readln();

    return 0;
}

EDIT: IRC - DList (, ) , ( - std.container).

+2

All Articles