Chapter 3 Strings, Vectors, and Arrays
Disclaimer: this article is a study note for C++ Primer Chinese version (fifth edition). The original book is more detailed, this article is only used for learning exchange, unauthorized reprint is prohibited.
If you reply to C++ on Jacen technical notes, you can get a PDF of 20,000 words C++ Primer essentials.
P74-P118
A string represents a variable-length sequence of characters. A vector holds a variable-length sequence of an object of a given type.
A using declaration for the namespace
using namespace:name;
Copy the code
The header file should not contain a USING declaration.
Library type String
#include <string>
using namespace std;
Copy the code
(1) Definition and initialization
string s1;
sting s2(s1);
string s3("value");
string s3 = "value";
string s4(n, 'c');
Copy the code
(2) String operations
s.empty(); / / to empty s.s considering (); // number of characters s[n]; // reference s1+s2 to the NTH character in s; // s1 and S2 connect <,<=,>,>= // compareCopy the code
::: warning
The Standards bureau allows literal and string literals to be converted to string objects. Literals and strings are different types.
: : :
(3) Handle characters in string objects
::: tip C++ program headers should use cname instead of name.h :::
Iterate over each value in a given sequence to perform some kind of operation
for (declaration : expression)
statement
Copy the code
The library type vector
A library vector represents a collection of objects, all of which are of the same type.
Vector is a class template, not a type.
(1) Define and initialize vector
vector<T> v1;
vector<T> v2(v1);
vector<T> v2 = v1;
vector<T> v3(n, val);
vector<T> v4(n); vector<T> v5{a,b,c... } vecrot<T> v5={a,b,c... }Copy the code
If parentheses are used, the supplied values are used to construct a vector.
If we use curly braces, we initialize the vector with a list.
(2) Add elements to vector
Start by defining an empty vector to which push_back is used at run time.
(3) Other vector operations
v.empty(a); v.size(a); v.push_back(t);
v[n];
Copy the code
::: warning
Subscripts can only be performed on elements that are confirmed to exist.
: : :
Introduction to iterators
Iterator operator
*iter // Dereference, return reference
iter->mem // equivalent to (*iter).mem++iter --iter iter1 == iter2 iter1 ! = iter2 iter + n iter - n iter += n iter -= n iter1 - iter2// The result of subtracting two iterators is the distance between them>, > =, <, < =// Position comparison
Copy the code
::: warning
Any loop body that uses an iterator cannot add elements to the container to which the iterator belongs.
: : :
An array of
(1) Array, pointer
When we use an array subscript, we usually define it as size_t.
::: warning
Defining an array must specify the type of the array. Auto inference is not allowed.
No array of references exists.
If two Pointers point to unrelated objects, they cannot be compared.
: : :
Multidimensional array
A multidimensional array is really an array of arrays.
size_t cnt = 0;
for(auto &row : a)
for (auto &col : row){
col = cnt;
++cnt;
}
Copy the code
int *ip[4]; // An array of integer Pointers
int (*ip)[4]; // Point to an array of 4 integers
Copy the code
The term
Begin string and vector members that return an iterator to the first element. Also a library function that takes an array and returns a pointer to the first element of the array.
A member of the end string and vector that returns a trailing iterator. Also a library function that takes an array and returns a pointer to the next element at the end of the array.