- 当两个迭代器相减得出来的数据类型名叫difference_type的带符号整形数,而相加会报错。而两个指针相减的结果类型是ptrdiff_t,属于带符号类型。
- 引用数组和指针数组的创建:
#include <iostream> #include <climits> #include <vector> int main() { int* ptrs[10]; int num[10]; //int &refs[10]; //不存在引用数组 int (*arr1)[10] = #//指向含有10个整数的数组 int (&brr1)[10] = num; //引用含有10个整数的数组 int* (&arr2)[10] = ptrs;//引用函数10个整数指针的数组 int* (*brr2)[10] = &ptrs;//指向函数10个整数指针的数组 }
- CPP11引入begin和end函数,其使用和STL的begin和end类似,只不过需要传递一个数组首元素地址,如果有两个指针指向同一个数组的元素,或者指向该数组的尾元素的下一位置。
#include <iostream> #include <climits> #include <vector> #include <iterator> int main() { int arr[] = { 0,1,2,3,4,5,6,7,8,9 }; int* beg = std::begin(arr), * en = std::end(arr); for (int* it = std::begin(arr); it != std::end(arr); ++it) { std::cout << *(it) << " "; } std::cout << std::endl; while (beg<en) { std::cout << *beg << " "; ++beg; } }
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
- c_str函数返回的是const char*类型的C风格字符串指针,c_str无法保证返回值一直有效,事实上,如果后续的操作改变了s的值就可能让之前返回的数组失去效用。
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> int main() { std::string str = "123123123"; const char* ptr = str.c_str(); std::cout << ptr << std::endl; }
123123123
- 使用数组初始化vector对象
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> int main() { int arr[] = { 0,1,2,3,4,5 }; std::vector<int> vec1(std::begin(arr), std::end(arr)); std::vector<int> vec2(arr + 1, arr + 4); for (auto x : vec1) std::cout << x << " "; std::cout << std::endl; for (auto x : vec2) std::cout << x << " "; std::cout << std::endl; }
- 使用范围for遍历多维数组,要使用范围for语句处理多维数组,除了最内层的循环可以不是引用类型外,其他外层所有循环的statement都是引用类型
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> int main() { constexpr int row = 3; constexpr int col = 3; int arr[row][col] = { 0 }; size_t cnt = 0; for (auto& row : arr) { for (auto& col : row) { col = cnt++; } } for (const auto& row : arr) { for (auto col : row) { std::cout << col << " "; } std::cout << std::endl; } return 0; }
- 类型别名简化多维数组:
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> int main() { constexpr int row = 3; constexpr int col = 4; int arr[row][col] = { 0 }; size_t cnt = 0; for (auto& row : arr) { for (auto& col : row) { col = cnt++; } } for (const auto& row : arr) { for (auto col : row) { std::cout << col << " "; } std::cout << std::endl; } using int_array_four = int[col]; //相当于typedef int int_array_four[col]; //注:别名用列起别名 for (int_array_four* ptr = arr; ptr != arr + row; ++ptr) { for (int* qtr = *ptr; qtr != *ptr + col; ++qtr) { std::cout << *qtr << " "; } std::cout << std::endl; } return 0; }
- CPP定义了一组异常类,分别被放在4个头文件中,其中exception头文件定义类最通用的异常类exception,只报告异常的发生,不提供任何额外信息;stdexcept头文件定义了几个常用的异常类,在下面列出;new头文件定义类bad_alloc异常类型;type_info头文件定义了bad_cast异常类型:
- exception:最常见的问题
- runtime_error:只有在运行时才能检测出来的问题。
- rang_error:运行时错误:生成结果超出有意义的值域范围。
- overflow_error:运行时错误:计算上溢
- underflow_error:运行时错误:计算下溢。
- logic_error:程序逻辑错误。
- domain_error:逻辑错误:参数对应的结果值不存在。
- invalid_argument:逻辑错误:无效参数。
- length_error:逻辑错误,试图创建一个超出该类型最大长度的对象。
- out_of_range:逻辑错误:使用一个超出有效范围的值。
cpp-primer笔记 020-字符串,向量和数组
于 2023-10-06 15:24:18 首次发布