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 string
s:
int main() {
string word;
while (cin >> word) // read until end-of-file
cout << word << endl; // write each word followed by a new line
return 0;
}
Using getline
to Read an Entire Line:
int main() {
string line;
// read input a line at a time until end-of-file
while (getline(cin, line))
cout << line << endl;
return 0;
}
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.
string s("Hello, World");
for (auto &c : s)
c = toupper(c);
cout << s << endl;
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.
for (decltype(s.size()) index = 0; index != s.size() && !isspace(s[index]); index++)
s[index] = toupper(s[index]);
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 vector
s
vector
s
In order for a class to support list initialization, the class should be defined as:
#include <iostream>
#include <initializer_list>
class MyClass {
public:
MyClass(std::initializer_list<int> list) {
for (auto item : list) {
std::cout << item << ' ';
}
std::cout << std::endl;
}
}
int main() {
MyClass obj2{6,7,8,9,10};
return 0;
}
List Initializer or Element Count?
vector<int> v1(10); // v1 has ten elements with value 0
vector<int> v2{10}; // v2 has one element with value 10
vector<int> v3(10, 1); // v3 has ten elements with value 1
vector<int> v4{10, 1}; // v4 has two elements with values 10 and 1
More subtle cases:
vector<string> v5{"hi"};// list initialization: v5 has one element. THIS IS ONLY SUPPORTED BY C++ 11
vector<string> v6("hi"); // error:can’t construct a vector from a string literal
vector<string> v7{10}; // v7 has ten default-initialized elements
vector<string> v8{10, "hi"}; // v8 has ten elements with value "hi"
{}
: 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.
vector<int>::size_type // OK
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.
auto b = v.begin(), e = v.end();
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.

string s("some string");
if (s.begin() != s.end()) {
auto it = s.begin();
*it = toupper(*it);
}
As with size_type, the library types that have iterators define types named iterator
and const_iterator
that represent actual iterator types:
vector<int>::iterator it; // it can read and write vector<int> elements
string::iterator it2; // it2 can read and write characters in a string
vector<int>::const_iterator it3; // it3 can read but not write elements
string::const_iterator it4; // it4 can read but not write characters
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