Strings, Vectors, and Arrays

  • string: A variable-length sequence of characters.
  • vector: A variable-length sequence of objects of a given type.

Namespace using Declarations

using namespace

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using std::cin;
using std::cout;

int main()
{
int i;
cin >> i;
cout << i;
return 0;
}

** Headers Should Not Include using Declarations ** : To avoid the namespace is used every program that includes that header files.

Library string Type

string: A variable-length sequence of characters.

1
2
#include <string>
using std::string;

Defining and Initializing strings

  • string s1: default initialization; s1 is the empty string.
  • string s2 (s1): s2 is a copy of s1.
  • string s2 = s1: s2 is a copy of s1.
  • string s3 (“value”): s3 is a copy of the string literal, not including the null.
  • string s4 (n, “c”): initialize s4 with n copies of the character ‘c’.
  • char *pc; string s5 (pc);

Operations on strings

  • os << s: output.
  • is >> s: input.
  • getline(is, s): reads a line of input from is into s, return is. it can contain the space of line.
  • s.empty(): true or false.
  • s.size(): return the num of chars in s. return std::size_type
  • s[n]: return the n th char of s.

Dealing with the Characters in a sting

Function | True Confition of c |
——– | ——————————————————- |
isalnum(c) | letter or digit |
isalpha(c) | letter |
iscntrl(c) | control char |
isdigit(c) | digit |
isgraph(c) | ( not space) and printable |
islower(c) | lowercase letter |
isprint(c) | printable |
ispunct(c) | punctuation char |
isspace(c) | white space (space, tab, vertical tab, return, newline, or formfeed) |
isupper(c) | uppercase letter |
isxdigit(c) | hexadecimal digit |
tolower(c) | uppercase letter ? lowercase : unchanged |
toupper(c) | lowercase letter ? uppercase : unchanged |

1
2
3
4
5
6
7
8
9
for(auto c : s)
cout << c << endl;

for(atuo &c : s)
c = toupper(c);
cout << s << endl;

for(decltype(s.size()) index = 0; index != s.size() && !isspace(s[index]); ++index)
s[index] = topupper(s[index]);

访问特定位置的字符:下标表示或迭代器

Library vector Type

A collection os objects, all of which have the same type. A vector is often referred to as a container because it “contains” other objects. A vector is a class template.

using std::vector;

Templates are not themselves functions or classes —- instructions to compiler for generating classes or functions. It can be said that template is the abstract class.

1
vector<int> ivec;

Defining and Initializing vectors

  • vector v1: T type elements of v1 and v1 is empty.
  • vector v2 (v1): v2 has a copy of each element in v1.
  • vector v2 = v1: v2 has a copy of each element in v1.
  • vector v3 (n, val): v3 has n elements with value val.
  • vector v4 (n): v4 has n copies of a value-initialized object.
  • vector v5 {a, b, …}: emelents of v5.
  • vector v5 = {a, b, …}: elements of v5.

对比

  • (): 进行构造
  • {}: 首先判断是否可以填充:元素类型是否都符合定义;如果无法填充则进行构造。

Adding Elemenets to a vector

v.push_back(elements);

关键限制:由于vector的动态特性,使得其可以方便的增加元素,但是有一限制,使用range for时,无法进行添加元素,因为range for使用迭代器进行循环,在size改变后,迭代器失效。

Introducing Iterators

迭代器:支持迭代器的容器封装有方法start(), end().
Iterator Operations

1
auto b = vec.start(). c = vec.end();
  • *iter: refernece to the element by the iter.
  • iter -> mem: (*iter).mem. mem可以是函数
  • ++iter / –iter: 下/上一个元素。
  • == / !=

使用for循环中对iter的边界进行判定时,优先使用!=而不是<,一些容器的迭代器不支持<

container_type::iterator/const_iterator it;**

begin/end返回的类型取决于指向的object是不是const。

cbegin()/cend(): 固定返回const_iterator

Iterator Arithmetic

    • n / - n
  • += n / -= n
  • iter2 - iter1: 两个迭代器之间的元素个数
  • = < <=

Arrays

similar to the library vector but offers a different trade-off between performeance and flexibility.

固定长度,但是在性能上相对vector有所提升

定义数组需要留出一位给null。可以使用字符串字面值初始化数组。不能将数组的内容拷贝给其他数组进行初始化,也不能用数组为其他数组赋值。

1
2
3
int (*parray)[10] = &arr; // 指向一个含有10个整数的数组
int (&arrref)[10] = arr; // 引用一个含有10个整数的数组
int *(&array)[10] = ptrs; // array是数组的引用,数组内有10个int指针

数组元素下标访问:size_t,定义于cstddef文件中(C中的stddef.h)

begin(), end(): 针对array,获得开始和结尾地址。地址相减的结果是ptrdiff_t类型。

C风格字符串

定义在cstring中

  • strlen(p): 返回p的长度,不计算空字符
  • strcmp(p1, p2): 比较p1和p2是否相等,(p1 > p2) ? 正数 : 负数
  • strcat(p1, p2): 将p2附加到p1后,返回p1
  • strcpy(p1, p2): 将p2拷贝给p1,返回p1

Multidimensional Arrays

1
2
3
4
5
6
7
8
int ia[2][4] = {
{0124},
{4567}
};

int ia[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
int ia[2][4] = {{0}, {4}}; // 初始化每行首个元素
int ia[2][4] = {0, 1, 2, 4}; // 初始化首行元素