ref:

Lec 1 Intro

Lec 2 Streams

String Stream

讲了sstream库的istringstream, ostringstreamstringstream,类似于Csscanfsprintf

  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. int main() {
  5. ostringstream oss("3.14", stringstream::ate);
  6. /* start at last
  7. * remove 'stringstream::ate' will start at front
  8. * */
  9. cout << oss.str() << endl;
  10. oss << 15;
  11. cout << oss.str() << endl;
  12. istringstream iss("19.6 dollars", stringstream::binary);
  13. string s;
  14. double amount;
  15. iss >> amount;
  16. //convert to double until meet white space, if cannot conevrt, will get '0'
  17. iss >> s;
  18. cout << s << endl << amount <<endl;
  19. return 0;
  20. }
  21. //输出如下
  22. /*
  23. 3.14
  24. 3.1415
  25. dollars
  26. 19.6
  27. */

iss划分token的方式是以空格、制表符为标志,如
image.png
iss读入的数据和类型有关,会在第一个不满足类型的字符处停止
image.png

State bits

  • good:正常
    • iss.good()
  • fail:失败,如文件无法打开,类型匹配错误
    • iss.fail()
  • eof:读至文件末尾
    • iss.eof()
  • bad:一些坏的错误,如正在读的文件突然被删掉了
    • iss.bad()

Manipulators | 控制符

一部分控制符需要<iomanip>头文件

  1. 这部分似乎有些太琐碎了,跳过了。比如`cout << setpercison(3) << 13.1769 <<endl;`会输出`13.177`,类似于`C``%3lf`

Lec 2.5 Types

Pair

将两个数据合成一组数据,是二元结构的简化。

用法

  1. pair<T1, T2> p1;
  2. //创建一个空的pair对象(使用默认构造),它的两个元素分别是T1和T2类型,采用值初始化
  3. pair<T1, T2> p1(v1, v2);
  4. //创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,second成员初始化为v2
  5. make_pair(v1, v2);
  6. // 以v1和v2的值创建一个新的pair对象,其元素类型分别是v1和v2的类型
  7. p1 < p2;
  8. // 两个pair对象间的小于运算,其定义遵循字典次序:如 p1.first < p2.first 或者 !(p2.first < p1.first) && (p1.second < p2.second) 则返回true
  9. p1 == p2
  10. // 如果两个对象的first和second依次相等,则这两个对象相等;该运算使用元素的==操作符
  11. p1.first; // 返回对象p1中名为first的公有数据成员
  12. p1.second; // 返回对象p1中名为second的公有数据成员
  13. pair<T1, T2> func();
  14. //创建返回值为pair的函数,返回时可以使用make_pair()

Struct

  1. `C`中的结构体相似。

Uniform Initialization | 统一初始化

C++ 11的新特性

这里是语法

  1. int arr[] {1, 2, 3};
  2. vector<string> {"oneko", "hello"};

Lec 3 Sequence Containers

Sequence Containers就是C++库中定义的一系列模板,用于以sequence形式储存的元素进行一系列操作。

  • std::array<T>:实现一个不可调整大小的数组
  • std::vector<T>大小可变的数组
  • std::list<T>:实现一个双向链表
  • std::forward_list<T>:实现一个单向链表
  • std::deque<T>:实现一个双端队列

    • 输入限制双端队列是一种可以从两端进行删除,但只能在一端进行插入的队列
    • 输出限制双端队列是一种可以在两端进行插入,但只能从一端进行删除的队列
    • 一般意义上的queuestack可以看做特殊的双端队列

      vector & array

      vector

      1. //usage
      2. std::vector<int> intArr;
      3. std::vector<string> strArr;
      4. std::vector<myStruct> structArr;
      5. std::vector<std::vector<string>> vecArr;
      6. //vector of vector<string>,二维数组
      一些基本操作,以vector<int>arr为例
  • arr.pushback(3):在末尾加入整数3

  • arr.popback():删除末尾元素
  • arr.size():数组大小
  • arr.empty():判断是否为空
  • arr.clear():清除所有元素
  • arr.at(2):位置为2处的元素的引用

全面的操作看这里

a point
一个值得注意的点,一般的访问在下标越界时不会报出错误,而使用var_name.at(pos)时,在越界时会报错并终止程序。

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. int main() {
  6. vector<string>name{"czl", "oneko"};
  7. cout << name[100] << endl;
  8. cout << "Hey, It's out of range actually" << endl;
  9. cout << name.at(100) << endl;
  10. return 0;
  11. }
  12. //输出
  13. /*
  14. Hey, It's out of range actually
  15. terminate called after throwing an instance of 'std::out_of_range'
  16. what(): vector::_M_range_check: __n (which is 100) >= this->size() (which is 2)
  17. */

array

  1. //usage
  2. std::array<int, 3> arr = {1, 2, 3}; //初始化了一个大小为3的数组
  3. std::array<std::array<string,3>,4>; //4*3的string数组
  1. //访问,以二维数组为例
  2. #include <iostream>
  3. #include <string>
  4. #include <array>
  5. using namespace std;
  6. int main() {
  7. std::array<std::array<string, 2>, 2> strArr{"a", "b", "c", "d"};
  8. for (int i = 0; i < strArr.size(); i++) {
  9. for (int j = 0; j < strArr.at(i).size(); j++) {
  10. cout << strArr.at(i).at(j) << " ";
  11. }
  12. cout << endl;
  13. }
  14. return 0;
  15. }
  16. //输出
  17. /*
  18. a b
  19. c d
  20. */

类似地,对vector的访问不再赘述。

deque

实际上,deque支持vector的所有操作,并且还支持快速的push_front()操作,但是实践中vector的应用更为广泛,因为一般的操作vector快于deque

“vector is the type of sequene that should be used by default… deque is the data structure of choice when most insertions and deletions take place at the beginning ar at the end of the sequence.”

list & forward_list

Overview of STL

image.png

Lec 4 Associative Containers & Iterators

Container Adaptors

stackqueue被叫做 container adaptors。
image.png

Associative Containers

Associative Containers 也是C++中定义的用来储存有序/无序关联数组的一系列模板,有一些不同的地方是数据是以的形式来访问的

  • std::map<T1, T2>:键-值对,键唯一,值可以不唯一,字典序
  • std::set<T>:值本身也发挥键的作用,值唯一,字典序
  • std::unordered_map<T1, T2>:顾名思义,没有顺序的map,Hash表实现
  • std::unordered_set<T>:没有顺序的set,Hash表实现

image.png

map

  1. //usage
  2. #include <iostream>
  3. #include <map>
  4. using namespace std;
  5. int main() {
  6. map<string,int> GradeMap;
  7. //two ways to insert
  8. GradeMap.insert(pair<string,int>("czl", 60));
  9. GradeMap.insert(map<string,int>::value_type("oneko",59));
  10. //traverse
  11. map<string,int>::iterator iter;
  12. //forward
  13. for(iter=GradeMap.begin();iter!=GradeMap.end();iter++){
  14. cout << '(' << iter->first << ',' << iter->second << ')' << ' ';
  15. }
  16. cout << endl;
  17. //backward
  18. map<string,int>::reverse_iterator re_iter;
  19. for(re_iter=GradeMap.rbegin();re_iter!=GradeMap.rend();re_iter++){
  20. cout << '(' << re_iter->first << ',' << re_iter->second << ')' << ' ';
  21. }
  22. cout << endl;
  23. //find
  24. iter = GradeMap.find("czl");
  25. if(iter != GradeMap.end()){
  26. cout << "Found! " << iter->first << "'s grade is " << iter->second;
  27. }
  28. //delete
  29. GradeMap.erase("oneko");
  30. //after traverse
  31. //(czl, 60)
  32. return 0;
  33. }
  34. //输出
  35. /*
  36. (czl,60) (oneko,59)
  37. (oneko,59) (czl,60)
  38. Found! czl's grade is 60
  39. */

Lec 5 Advanced Containers

  • 主要讲了迭代器的一些用法,比较基础。

image.png

  • Input: Read Only something = *itr;
  • Output: Write Only *itr = something;
  • Forward: Read and Write
  • Bidirectional: 可自减 --itr;
  • Random Access: itr += ofs;
  • 以上种种在日常使用时不显式指明时体会不明显

    Lec 6 & 7 Templates & Functions

    模板是泛型编程的重要组成部分。