1 / 18

走向 C++ 之路

走向 C++ 之路. WindyWinter windy@ream.at. #include <stdio.h> main(t ,_,a) char*a;{return t<1?main(*a,a[-t],"=a-1kj3gnm:q ebh_cf*<r.d>i^+?,()[?qzyrjuvcdefg h,!kbpolwxs'.t main(")&&a[-t]&&main (t-1,_,a):t/2?_==*a?putchar(32[a]) :_%115<36||main(t,_,a+1):main(

Download Presentation

走向 C++ 之路

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 走向C++之路 WindyWinter windy@ream.at #include <stdio.h> main(t ,_,a) char*a;{return t<1?main(*a,a[-t],"=a-1kj3gnm:q\ ebh_cf*<r.d>i^+?,()[?qzyrjuvcdefg\ h,!kbpolwxs'.t main(")&&a[-t]&&main (t-1,_,a):t/2?_==*a?putchar(32[a]) :_%115<36||main(t,_,a+1):main( 0,t,"+b:s?#mw{ty}t(x1{|~?\ y<#q?(*#{k)}rsh?vts){\ ?w*#yk<y,}w}z!w)v\ ~>u:!zym^t|x|\ |xtutu!uz\ |#}t") ;}

  2. 模板与泛型编程 I First things first

  3. 模板 Actually, there is only one template. • 实践中需要不同的min函数int min(int a, int b) { return a>b ? b : a; }double min(double a, double b) { return a>b ? b : a; } • 如果还需要其他类型的min怎么办?char short long long-long float long-double … • 类型抽象——T类型template <typename T>T min(T a, T b) { return a>b ? b : a; } • min(1.2, 1.5); • min(1, 2.0); • template <typename T, typename P, typename R>R min(T a, P b) { return a>b ? b : a; } • min<int>(1, 2.0);

  4. 模板 Sorry, there does exist the 2nd one. • 类也需要多样性——平面点类:Z×Z R×R C×C • template <typename T>struct Point{ T x, y;}; • Z×Z -> Point<int>R×R -> Point<double> • C×C -> Point<complex<double> >

  5. 模板 The template is really complex. • 模板参数不限于类型。 • 除类型以外还可以是整数——常用于定义数组类型: • template<typename T, int N>struct Array{ T mData[N]; T & operator[](const int i) { return mData[i]; } const T & operator[](const int i) { return mData[i]; }}; • 模板参数还可以是任何可以转化为整数的东西:char bool 枚举类型

  6. 标准模板库 Hallowed are the C++

  7. 标准模板库 STL • C语言有一个小巧精干的标准库; • C++在引入泛型编程的思想后,标准库得到了广泛的、革命性的变革——标准模板库。 • string获得大量类的特性; • vector、list、deque、queue、stack、priority_queue、set/map纷纷进入STL; • STL中的函数不再限定参数的类型,而只做出基本要求:min、max、sort要求此种类型定义过”<“;binary_search要求传递进来的序列可以随机访问;……

  8. string • #include<string> • 成员函数:[]:返回指定位置的字符;size()、length():返回长度,O(1)时间;clear():清空;+=、insert():插入;erase():删除;find():查找;substr():取子串;+:连接。

  9. string • <、>、<=、>=、==、!= • getline(cin, str):从标准输入流cin读取一行,放入str。 • cout<<str:从标准输出流cout输出str。 • cin>>str:从标准输入流读取一个字符串(以非空白字符开头、空白字符结尾),放入str。 • 它们的详细用法、函数原型,以及更多的成员函数和用法,请去http://www.cplusplus.com/reference/string/string查找。

  10. vector • #include<vector> • vector<T> a; • [], at():取指定位置的元素,[0]是第一个元素; • size():vector的长度; • push_back():将一个元素插入到vector的最后面; • insert()、clear()、erase() • http://www.cplusplus.com/reference/stl/vector/

  11. 迭代器 iterators • 最简单的迭代器相当于指向容器中元素的指针; • vector<T>::iterator i; • i=a.begin(); *i 取得一个元素的引用;i->push_back()。 • a.end() 表示“超出末端的位置”:for (i=a.begin(); i<a.end(); ++i) • vector的迭代器可以随机访问:--i;i += 2;i -= 6; • 与iterator相仿的是reverse_iterator,对应有rbegin()、rend()。 • 其他类型的迭代器比较复杂。http://www.cplusplus.com/reference/std/iterator/

  12. list • #include<list> • list<T> b; • size():list的长度,O(1)时间; • push_back()/push_front():将一个元素插入到list的最后面/最前面; • insert()、clear()、erase() • splice()、merge() • list<T>::iterator 只能++/--,不能随机访问。 • http://www.cplusplus.com/reference/stl/list/

  13. 迭代器 iterators • 迭代器的失效——如果一个迭代器指向的元素已经被删除,那么该迭代器失效,访问该迭代器后果不可预料。 • 迭代器的分类: • 随机访问迭代器 • 前向迭代器 • 双向迭代器 • 输入迭代器 • 输出迭代器 • 正向迭代器 • 反向迭代器 • 常量迭代器 • 流迭代器

  14. sort • #include<algorithm> • vector<int> a;sort(a.begin(), a.end()); • vector<person> c;sort(c.rbegin(), c.rend()); • int b[100];sort(b, b+100); • list<double> d;sort(d.begin(), d.end()); Xd.sort()O • http://www.cplusplus.com/reference/algorithm/sort/

  15. 其他的小工具 • #include<algorithm> • min(a, b) • max(a, b) • swap(a, b) • http://www.cplusplus.com/reference/algorithm/

  16. 流输入输出 • 严格来说,这不是STL的一部分。 • iostream——scanf/pringf • fstream——fscanf/fprintf • stringstream——sscanf/sprintf • cin>>XXX; cout<<XXX; • ifstream fin(“input.txt”); ofstream fout(“output.txt”);fin>>XXX; fout<<XXX; • stringstream s;s<<XXX; s>>XXX; • http://www.cplusplus.com/reference/iostream/

  17. Tags Cloud Keywords • string vector list • 迭代器 • sort • iostream fstream stringstream Soli Deo gloria.

  18. 勘误和补充 • 友元可以是定义和声明在一起 • 直接调用构造函数的例子:Vector(1.0, 1.0),生成一个临时的Vector对象。 • explicit关键字:一个类的带参构造函数,可以定义一种把某种类型转换为该类的隐式类型转换。explicit关键字加在某个构造函数的声明前,禁止这种隐式类型转换。 • 模板与泛型编程 II:C++ Lite Memo溯源授人以渔

More Related