Strings, Vectors, and Arrays
The standard library defines a number of additional types of a higher-level nature that computer hardware usually does not implement directly.
Strings


Reading Unkown Number of strings:
Using getline to Read an Entire Line:
The string::size_type Type
These companion types of classes from standard library make it possible to use the library types in a machine-independent manner.
Because size returns an unsigned type, it is essential to remember that expressions that mix signed and unsigned data can have surprising results. For example, never write the expression s.size() < n where n is an int.
Adding Literals and strings: donot add string literals.
Dealing with the Characters in a string
string
In order to use a range for to change the characters in a string, you should use a reference as our control variable.
The value in the subscript is referred to as “a subscript” or “an index.” The index we supply can be any expression that yields an integral value. However, if our index has a signed type, its value will be converted to the unsigned type that string::size_type represents.
The result of using an out-of-range subscript is undefined.
Library vector Type
vector TypeThe process that the compiler uses to create classes or functions from templates is called instantiation.
vector is a template, not a type. Types generated from vector must include the element type, for example, vector<int>.
Defining and Initializing vectors
vectors
In order for a class to support list initialization, the class should be defined as:
List Initializer or Element Count?
More subtle cases:
{}: prefer list initialization, can also identify element counter. If there is no way to use the initializers to list initialize the object, then those values will be used to construct the object. If list initialization isn’t possible, the compiler looks for other ways to initialize the object from the given values.
vector Opetrations
vector OpetrationsThe body of a range for must not change the size of the sequence over which it is iterating.
The size member returns a value of the size_type defined by the corresponding vector type.
As with strings, subscripts for vector start at 0; the type of a subscript is the corre- sponding size_type;
Introducing Iterators
All of the library containers have iterators, but only a few of them support the subscript operator.
The iterator returned by end is an iterator positioned “one past the end” of the associated container (or string). If the container is empty, begin returns the same iterator as the one returned by end.

As with size_type, the library types that have iterators define types named iterator and const_iterator that represent actual iterator types:
If a vector or string is const, we may use only its const_iterator type. With a nonconst vector or string, we can use either iterator or const_iterator.
The type returned by begin and end depends on whether the object on which they operator is const. If the object is const, then begin and end return a const_iterator; if the object is not const, they return iterator.
Last updated