Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

D (The Programming Language)/d2/Strings and Dynamic Arrays

50% developed
From Wikibooks, open books for an open world
<D (The Programming Language)
(Redirected fromD (The Programming Language)/d2/Lesson 8)
Topics:Language and Phobos -DGui (example placeholder - can be any library)
PreviousLessons:0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -TNext


Lesson 8: Strings and Dynamic Arrays

[edit |edit source]

In this chapter, you will learn about strings in depth. In the meantime, you will also learn about another type: the dynamic array.

Introductory Code

[edit |edit source]

Dynamic Arrays

[edit |edit source]
importstd.stdio;voidmain(){int[]a=[1,2,3,4];int[]b=[5,6];autoc=a~b;writeln(c);// [1,2,3,4,5,6]writeln(c.length);// 6int*ptr_c=c.ptr;ptr_c[0]=3;writeln(c);// [3,2,3,4,5,6]}

Immutable and Strings

[edit |edit source]
importstd.stdio;voidmain(){// Concept:  Immutableimmutable(int)a=10;// a = 11;  Error:  cannot modify immutableimmutablea=10;// Concept:  Strings as Arraysimmutable(char)[]str="Hello";autostr1=str~"1";writeln(str1);// Hello1writeln(str1.length);// 6// str1[] = 'z'; error! str1's elements are not mutablechar[]mutablestr1=str1.dup;// Concept: Char Literalsmutablestr1[]='z';// 'z' is not a string, but a charstr1=mutablestr1.idup;// The str1 itself is mutable// only its elements are not mutablewriteln(str1);// zzzzzz}

Concepts

[edit |edit source]

Dynamic Arrays

[edit |edit source]

Dynamic Arrays versus Static Arrays

[edit |edit source]

In the previous lesson, you learned about static arrays. Dynamic arrays are different from those in that they do not have a fixed length. Essentially, a dynamic array is a structure with this info:

  1. A pointer to the first element
  2. The length of the entire array

You create a dynamic array like this:

int[]a;

You can create a dynamic array initiated with a specific length:

int[]a=newint[](5);

You will learn more about thenew keyword later.
The syntax for filling an array with a single value is the same for both static and dynamic arrays:

int[]a=[1,2,3];a[]=3;writeln(a);// [3, 3, 3]

Dynamic arrays are indexed in the same fashion as static arrays. The syntax for accessing an element at a certain index is the same:

a[2];

However, since dynamic arrays don't have a length known at compile-time, the compiler can't check if the index that you are accessing is really within that length. Code like this would compile but would also cause a runtime Range Violation error:

int[]a=[1,2,3];writeln(a[100]);//Runtime error, Range violation

Manipulation of Dynamic Arrays

[edit |edit source]

Dynamic arrays can be combined with other dynamic arrays or even with static arrays using the~ operator. Appending a new element to the array uses the same operator.

int[]a=[1,2];autob=a~[3,4];writeln(b);//[1, 2, 3, 4]b~=5;// same as b = b ~ 5;writeln(b);//[1, 2, 3, 4, 5]

You shouldn't assign a dynamic array to a static array (my_static_arr = my_dynamic_arr;) unless if you are certain thatmy_dynamic_arr.length is the equal tomy_static_arr.length. Otherwise, a runtime error will occur. You can always assign a static array to a dynamic array variable, though, since the dynamic array will automatically resize if the static array's length is too big.

int[]a=[9,9,9,9,9,9];int[3]b=[1,2,3];int[200]c;a=b;writeln(a);// [1, 2, 3]a=c;writeln(a.length);// 200int[]d=[5,4,3];b=d;// OK: lengths are both 3// c = d;  runtime error! lengths don't match.

Passing Dynamic Arrays to Functions

[edit |edit source]

Dynamic Arrays are passed by value to functions. That means, when you pass a dynamic array to a function, the structure that contains the pointer to the first element and the length is copied and passed.

voidtryToChangeLength(int[]arr){arr.length=100;}voidmain(){int[]a=[1,2];tryToChangeLength(a);writeln(a.length);// still 2}

You learned in the last chapter that you can cause things to be passed by reference instead of by value by adding theref modifier.

Array and Other Properties

[edit |edit source]

These properties apply to both static and dynamic arrays:

PropertyDescription
.initFor static arrays, it returns an array with each element initialized to its default value.
.sizeofReturns the size of the array in memory.
.lengthReturns the number of elements in the array. This is fixed in static arrays.
.ptrReturns a pointer to the first element of the array.
.dupCreates a dynamic array that is a copy of the array and returns it.
.idupSimilar to.dup but the copy's elements areimmutable.
.reverseReturns the array with its elements in reverse order.
.sortSorts in place the elements in the array and returns the result.

There are also other properties which are common to every object or expression. Two of such properties are the.init and.sizeof properties.

Immutable

[edit |edit source]

In D,immutable is astorage-class just likeauto. It converts a type to a non-modifiable type.

immutable(int)fixed_value=37;immutableintanother_value=46;

Note thatimmutable(type) means the same asimmutable type to the compiler.

Storage-Classes andauto

[edit |edit source]

When you have a storage class likeimmutable, you can omitauto for type inference.

immutablefixed_value=55;

The type of whatever is immutable is inferred from the compiler. This next code example is not valid because there is no way the compiler could infer the type:

immutablefixed_value;//Error!

Usingimmutable Variables

[edit |edit source]

This is allowed and perfectly fine code:

immutable(int)a=300;intb=a;

It only setsb equal to the value ofa.b does not have to beimmutable. That changes if you are taking a reference:

immutable(int)a=13;immutable(int)*b=&a;// int* c = &a;  Error.

You are allowed to cast theimmutable away, but if you were to modify an immutable value using that hack, the result is undefined.

immutable(int)a=7;int*b=cast(int*)&a;// Just make sure you do not modify a// through b, or else!

Strings as Arrays

[edit |edit source]

You've seen strings since Lesson 1. Astring is the exact same thing asimmutable(char)[], a dynamic array of immutable char elements. Likewise, awstring is the same asimmutable(wchar)[], and adstring is the same asimmutable(dchar)[].

String Properties

[edit |edit source]

Strings have the same built-in properties as dynamic arrays. One useful property is the.dup property, for creating a mutablechar[] copy of a string if you want to modify the individual characters of the string.

stringa="phobos";char[]b=a.dup;b[1]='r';b[4]='e';writeln(b);// probes

The.idup property is useful for creating a copy of an existing string, or for creating a string copy of achar[].

stringa="phobos";stringcopy_a=a.idup;char[]mutable_a=copy_a.dup;mutable_a[3]='t';copy_a=mutable_a.idup;writeln(mutable_a);// photoswriteln(a);// phobos

Char Literals

[edit |edit source]

Achar literal is enclosed by single-quotes. There are alsowchar anddchar literals.

autoa="a";// stringautob='b';// charautoc='c'c;// charautod='d'w;// wcharautoe='e'd;// dchar
Retrieved from "https://en.wikibooks.org/w/index.php?title=D_(The_Programming_Language)/d2/Strings_and_Dynamic_Arrays&oldid=3676107"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp