reading file byLine

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 3 17:41:21 PDT 2015


On Fri, Sep 04, 2015 at 12:18:14AM +0000, Namal via Digitalmars-d-learn wrote:
[...]
> That being said, when do I have to import std.array and std.string?
> Every time I use std.array? I can obviously use arrays and strings
> without those libs.

Arrays and strings are built into the language; but many common
operations on arrays and strings are not built-in, but are provided in
the standard library, i.e., std.array and std.string.

So if you use just the plain ~, ~=, operators, then you don't need
std.array or std.string. But if you need functions like split() or
array(), then you need to import std.array.  If you're only dealing with
strings as char arrays, then that's all you need. But if you want some
string-specific functions, e.g., isNumeric(), chomp(), capitalize(),
etc., then you need std.string.

Also, some of the more generic operations that can be applied to more
than just arrays or strings, will be found in std.algorithm, such as
map(), find(), count(), startsWith(), etc..

Basically, if an operation *only* applies to strings, it should
generally be found in std.string; if it can also apply to arrays, then
it should be found in std.array. If it can be applied to more than just
arrays (that is, ranges, e.g., a file stream, a network socket, a
sequence of values produced by a generating function, etc., anything
with array-like sequential semantics that are not necessarily actual
arrays), then it will generally be found in std.algorithm.  One useful
rule-of-thumb to decide where to look is to ask yourself if an operation
on an array can also be logically applied to a linked-list. If it can,
it's probably in std.algorithm. If not (i.e. it depends on semantics
specific to arrays), then it's probably in std.array.

Hope this helps.


T

-- 
Latin's a dead language, as dead as can be; it killed off all the Romans, and now it's killing me! -- Schoolboy


More information about the Digitalmars-d-learn mailing list