C++ 学习笔记

2019/04/10 10:10
阅读数 14

<div class="file_content markdown-body"> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"># CPP 学习笔记</span></pre></div></div> <blockquote> <p>可以直接下载pdf <a href="/elliot1995/CPP_learn/blob/master/CPP%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.pdf">CPP 学习笔记</a></p> </blockquote>

<ul> <li> <a href="#01-%E5%A4%9A%E7%BA%BF%E7%A8%8B">0.1. 多线程</a> <ul> <li> <a href="#011-%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C%E5%B7%B2%E7%BB%8F%E5%AD%A6%E5%AE%8C%E4%BB%8A%E5%A4%A9%E7%9C%8B%E4%B8%8B%E5%A4%9A%E7%BA%BF%E7%A8%8B">0.1.1. 文件操作已经学完,今天看下多线程</a> <ul> <li><a href="#0111-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%91">0.1.1.1. 知识点1</a></li> <li><a href="#0112-%E4%BB%A3%E7%A0%811">0.1.1.2. 代码1</a></li> <li><a href="#0113-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%92">0.1.1.3. 知识点2</a></li> </ul> </li> <li> <a href="#012-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%93">0.1.2. 知识点3</a> <ul> <li><a href="#0121-%E4%BB%A3%E7%A0%81%EF%BC%93">0.1.2.1. 代码3</a></li> </ul> </li> </ul> </li> <li> <a href="#02-stl">0.2. STL</a> <ul> <li> <a href="#021-stl-%E5%AD%A6%E4%B9%A0%E6%B2%A1%E6%9C%89%E5%B8%A6%E7%AC%94%E8%AE%B0%E6%9C%AC%E5%9B%9E%E6%9D%A5%E6%88%91%E6%97%A5-20191111-2321">0.2.1. STL 学习,没有带笔记本回来。。。我日 2019.11.11 23:21</a> <ul> <li><a href="#0211-stl%E7%9A%84%E6%A6%82%E5%BF%B5">0.2.1.1. STL的概念</a></li> <li><a href="#0212-%E4%BB%A3%E7%A0%81">0.2.1.2. 代码</a></li> <li><a href="#0213-stl%E6%8A%BD%E8%B1%A1%E7%9A%84%E6%98%AF%E4%BB%80%E4%B9%88">0.2.1.3. STL抽象的是什么</a></li> </ul> </li> <li> <a href="#022-%E5%85%AD%E5%A4%A7%E7%BB%84%E4%BB%B6">0.2.2. 六大组件</a> <ul> <li><a href="#0221-vector-%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84-%E5%A0%86%E4%B8%8A">0.2.2.1. vector 动态数组 堆上</a></li> <li><a href="#0222-tuple-%E5%8F%AF%E4%BB%A5%E5%AD%98%E5%82%A8%E4%B8%8D%E5%90%8C%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B">0.2.2.2. tuple 可以存储不同的数据类型</a></li> <li><a href="#0223-list-%E9%80%82%E5%90%88%E7%BB%8F%E5%B8%B8%E6%8F%92%E5%85%A5%E7%BB%8F%E5%B8%B8%E5%88%A0%E9%99%A4%E7%9A%84%E6%83%85%E5%86%B5">0.2.2.3. list 适合经常插入,经常删除的情况</a></li> <li><a href="#0224-list-%E5%AE%B9%E5%99%A8">0.2.2.4. list 容器</a></li> <li><a href="#0225-set-%E5%AE%B9%E5%99%A8-%E7%BA%A2%E9%BB%91%E6%A0%91%E5%AE%B9%E5%99%A8">0.2.2.5. set 容器 (红黑树容器)</a></li> </ul> </li> <li><a href="#023-algorithm-%E7%AE%97%E6%B3%95">0.2.3. algorithm 算法</a></li> <li> <a href="#024-iterator">0.2.4. Iterator</a> <ul> <li><a href="#0241-%E4%BB%BF%E5%87%BD%E6%95%B0">0.2.4.1. 仿函数</a></li> </ul> </li> <li><a href="#025-wmv-stl-bug-%E8%BF%AD%E4%BB%A3%E5%99%A8">0.2.5. wmv STL Bug 迭代器</a></li> <li> <a href="#026-wmv-%E6%A0%88%E9%98%9F%E5%88%97%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97%E4%BC%98%E5%85%88%E9%98%9F%E5%88%97">0.2.6. wmv 栈队列双端队列优先队列</a> <ul> <li><a href="#0261-stack-%E5%85%B3%E9%94%AE%E5%AD%97%E5%8F%AF%E4%BB%A5%E6%9E%84%E5%BB%BA%E6%A0%88">0.2.6.1. stack 关键字可以构建栈</a></li> <li><a href="#0262-queue--%E9%98%9F%E5%88%97">0.2.6.2. queue 队列</a></li> <li><a href="#0263-deque-%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97">0.2.6.3. deque 双端队列</a></li> <li><a href="#0264-priority_que-%E4%BC%98%E5%85%88%E7%BA%A7%E9%98%9F%E5%88%97-%E4%B8%8D%E5%A4%AA%E7%90%86%E8%A7%A3">0.2.6.4. priority_que 优先级队列 (不太理解)</a></li> </ul> </li> <li> <a href="#027-%E7%BA%A2%E9%BB%91%E6%A0%91%E5%AE%B9%E5%99%A8">0.2.7. 红黑树容器</a> <ul> <li><a href="#0271-set-%E6%98%AF%E4%B8%80%E4%B8%AA%E7%BA%A2%E9%BB%91%E6%A0%91">0.2.7.1. set 是一个红黑树</a></li> </ul> </li> <li> <a href="#028-set-map-multi-map-%E7%BA%A2%E9%BB%91%E6%A0%91">0.2.8. set map multi-map 红黑树</a> <ul> <li><a href="#0281-set%E7%9A%84%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95">0.2.8.1. set的高级用法</a></li> <li><a href="#0282-multiset">0.2.8.2. multiset</a></li> <li><a href="#0283-map--%E6%98%A0%E5%B0%84-multimap-%E5%A4%9A%E5%B1%82%E6%98%A0%E5%B0%84-nbsp9wmv-5341">0.2.8.3. map 映射 multimap 多层映射 &nbsp;9.wmv 53:41</a></li> <li><a href="#0284-pair-%E5%85%B3%E9%94%AE%E5%AD%97-nbsp9wmv-012315">0.2.8.4. pair 关键字 &nbsp;9.wmv 01:23:15</a></li> <li><a href="#0285-hash_set-%E5%92%8Chash_map-nbsp9wmv-013310">0.2.8.5. hash_set 和hash_map &nbsp;9.wmv 01:33:10</a></li> <li><a href="#0286-equal_range">0.2.8.6. equal_range()</a></li> </ul> </li> <li> <a href="#029-string-%E6%9C%AC%E8%B4%A8%E6%98%AF%E4%B8%80%E4%B8%AA%E5%AE%B9%E5%99%A8">0.2.9. string 本质是一个容器</a> - <a href="#02901-%E7%BD%91%E4%B8%8A%E6%89%92%E6%9D%A5%E7%9A%84-string-%E4%BB%8B%E7%BB%8D">0.2.9.0.1. 网上扒来的 string 介绍</a> </li> <li> <a href="#0210-gpu%E7%BC%96%E7%A8%8B">0.2.10. GPU编程</a> <ul> <li><a href="#02101-lamda-%E8%A1%A8%E8%BE%BE%E5%BC%8F-c-amp-%E7%BC%96%E7%A8%8B">0.2.10.1. Lamda 表达式 C++ AMP 编程</a></li> </ul> </li> </ul> </li> <li> <a href="#03-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F">0.3. 设计模式</a> <ul> <li><a href="#031-%E5%A4%87%E5%BF%98%E5%BD%95%E6%A8%A1%E5%BC%8F">0.3.1. 备忘录模式</a></li> <li><a href="#032-%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F">0.3.2. 策略模式</a></li> <li><a href="#033-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F">0.3.3. 设计模式 抽象工厂模式</a></li> <li><a href="#034-%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F">0.3.4. 简单工厂模式</a></li> <li><a href="#035-%E6%96%B9%E6%B3%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F">0.3.5. 方法工厂模式</a></li> <li><a href="#%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F">抽象工厂模式</a></li> <li><a href="#%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F">单例模式</a></li> <li><a href="#%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F">代理模式</a></li> <li><a href="#%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%A8%A1%E5%BC%8F">迭代器模式</a></li> <li><a href="#%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%A8%A1%E5%BC%8F">命令行模式</a></li> <li><a href="#%E8%B4%A3%E4%BB%BB%E9%93%BE%E6%A8%A1%E5%BC%8F">责任链模式:</a></li> </ul> </li> <li> <a href="#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95">数据结构与算法</a> <ul> <li><a href="#%E6%80%BB%E8%A7%88">总览</a></li> <li><a href="#%E6%A6%82%E8%AE%BA">概论</a></li> <li><a href="#%E7%AE%97%E6%B3%95%E7%9A%84%E7%89%B9%E6%80%A7">算法的特性</a></li> <li><a href="#%E7%AE%97%E6%B3%95%E7%9A%84%E8%A1%A1%E9%87%8F%E6%A0%87%E5%87%86">算法的衡量标准</a></li> <li> <a href="#boost%E5%92%8C%E7%AE%97%E6%B3%95">Boost和算法</a> <ul> <li><a href="#boost-array-%E7%AC%AC%E4%B8%80%E5%8F%AAboost-%E7%A8%8B%E5%BA%8F">boost Array 第一只boost 程序</a></li> <li><a href="#boost-%E5%BA%93%E7%9A%84%E5%AD%A6%E4%B9%A0-boost_array_bind_fun_ref">boost 库的学习 boost_array_bind_fun_ref</a></li> <li> <a href="#std-%E6%96%B9%E5%BC%8F">std 方式</a> <ul> <li><a href="#boost%E6%96%B9%E5%BC%8F">boost方式</a></li> </ul> </li> <li><a href="#boostfunction-%E5%BA%93">boost::function 库</a></li> <li><a href="#boostref">boost::ref</a></li> <li><a href="#raii">RAII</a></li> <li><a href="#%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-smartpointers-%E5%BA%93">智能指针 smartpointers 库</a></li> </ul> </li> <li><a href="#%E7%B1%BB%E7%9A%84%E8%99%9A%E5%87%BD%E6%95%B0%E8%A1%A8">类的虚函数表</a></li> <li><a href="#%E4%B8%80%E4%B8%AA%E8%87%AA%E5%B7%B1%E5%AE%9E%E7%8E%B0%E7%9A%84vector%E5%8F%AA%E8%83%BD%E5%A4%84%E7%90%86%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE">一个自己实现的Vector(只能处理基本类型数据)</a></li> <li><a href="#%E7%BA%A2%E9%BB%91%E6%A0%91">红黑树</a></li> </ul> </li> <li> <a href="#boost-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0">Boost 学习笔记</a> <ul> <li><a href="#%E6%9C%89%E7%94%A8%E7%9A%84">有用的</a></li> <li> <a href="#smart-pointer">Smart Pointer</a> <ul> <li> <a href="#%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-shared_ptr-weak_ptr-scoped_ptr--scoped_array--intrusive_ptr">智能指针 :shared_ptr weak_ptr scoped_ptr scoped_array intrusive_ptr,</a> <ul> <li><a href="#smart_ptr-%E5%BA%93%E5%A6%82%E4%BD%95%E6%94%B9%E8%BF%9B%E4%BD%A0%E7%9A%84%E7%A8%8B%E5%BA%8F">Smart_ptr 库如何改进你的程序?</a></li> <li><a href="#%E6%9C%89%E4%B8%89%E7%A7%8D%E5%85%B8%E5%9E%8B%E7%9A%84%E6%83%85%E5%86%B5%E9%80%82%E5%90%88%E4%BD%BF%E7%94%A8%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88">有三种典型的情况适合使用智能指针:</a></li> <li><a href="#boost%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E4%BC%98%E5%8A%BF">boost智能指针的优势</a></li> </ul> </li> <li> <a href="#scoped_ptr">scoped_ptr</a> <ul> <li><a href="#scoped_ptr-%E7%9A%84%E7%94%A8%E6%B3%95">scoped_ptr 的用法</a></li> <li><a href="#scoped_ptr-%E5%92%8C-pimpl-%E7%94%A8%E6%B3%95">scoped_ptr 和 Pimpl 用法</a></li> <li><a href="#scoped_ptr-%E4%B8%8E-const-auto_ptr-%E7%9A%84%E5%8C%BA%E5%88%AB">scoped_ptr 与 const auto_ptr 的区别</a></li> <li><a href="#%E6%80%BB%E7%BB%93">总结</a></li> </ul> </li> <li><a href="#scoped_array">scoped_array</a></li> <li> <a href="#shared_ptr">shared_ptr</a> <ul> <li><a href="#%E4%BD%9C%E7%94%A8">作用</a></li> </ul> </li> <li><a href="#shared_ptr-%E5%AE%9A%E4%B9%89">Shared_ptr 定义</a></li> <li><a href="#shared_ptr-%E5%86%85%E9%83%A8%E6%9E%84%E9%80%A0">Shared_ptr 内部构造</a></li> </ul> </li> <li><a href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B">代码示例</a></li> </ul> </li> <li> <a href="#qt-%E5%AD%A6%E4%B9%A0">QT 学习</a> <ul> <li><a href="#011-%E5%9D%90%E6%A0%87%E7%B3%BB%E7%BB%9F">0.1.1. 坐标系统</a></li> <li><a href="#012-layouts">0.1.2. layouts</a></li> <li><a href="#013-%E6%8E%A7%E4%BB%B6">0.1.3. 控件</a></li> <li><a href="#014-%E6%B6%88%E6%81%AF%E5%9F%BA%E7%A1%80">0.1.4. 消息基础</a></li> <li> <a href="#015-qt-%E6%B6%88%E6%81%AF%E4%B8%AD%E7%BA%A7">0.1.5. QT 消息中级</a> <ul> <li><a href="#0151-eventfilter-%E6%88%AA%E5%8F%96%E6%B6%88%E6%81%AF">0.1.5.1. eventFilter 截取消息</a></li> <li><a href="#1052-notify-%E9%80%9A%E7%9F%A5%E6%B6%88%E6%81%AF">1.0.5.2. notify 通知消息</a></li> <li><a href="#1053-postevent-%E5%92%8C-sendevent">1.0.5.3. postEvent 和 sendEvent</a></li> </ul> </li> <li> <a href="#106-qpainter-%E5%92%8C%E9%87%8D%E5%86%99%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B6">1.0.6. QPainter 和重写自定义控件</a> <ul> <li><a href="#1061-qpainter-%E7%9A%84%E7%BB%98%E5%9B%BE%E6%95%88%E7%8E%87%E5%BC%95%E7%94%A8">1.0.6.1. QPainter 的绘图效率(引用)</a></li> <li><a href="#1062-qpainter-%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%B7%B1%E7%9A%84%E6%8E%A7%E4%BB%B6">1.0.6.2. QPainter 实现自己的控件</a></li> </ul> </li> <li> <a href="#107-%E4%BF%A1%E5%8F%B7%E5%92%8C%E6%A7%BD">1.0.7. 信号和槽</a> <ul> <li><a href="#1071-%E5%AE%9A%E4%B9%89">1.0.7.1. 定义</a></li> <li><a href="#1072-%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84%E4%BA%8B%E9%A1%B9">1.0.7.2. 需要注意的事项</a></li> <li><a href="#1073-%E6%80%BB%E7%BB%93">1.0.7.3. 总结:</a></li> </ul> </li> <li><a href="#108-%E9%AB%98%E7%BA%A7painter">1.0.8. 高级Painter</a></li> <li> <a href="#109-qdialog">1.0.9. QDialog</a> <ul> <li><a href="#1091-%E4%BB%A3%E7%A0%81%E5%B1%95%E7%A4%BA">1.0.9.1. 代码展示</a></li> </ul> </li> <li><a href="#1010-mainwindow">1.0.10. MainWindow</a></li> <li> <a href="#1011-qfile-qbuffer-qxxxxstream-mapping">1.0.11. QFile-QBuffer-QXXXXStream-Mapping</a> <ul> <li><a href="#10111-qfile">1.0.11.1. QFile</a></li> <li><a href="#10112-qbuffer-%E5%86%85%E5%AD%98-%E6%96%87%E4%BB%B6">1.0.11.2. QBuffer //内存 文件</a></li> <li><a href="#10113-qdatastream-qtextstream">1.0.11.3. QDataStream QTextStream</a></li> </ul> </li> <li><a href="#1012-socket-tcp-udp-boardcast-muli-cast">1.0.12. Socket-TCP-UDP-BOARDCAST-MULI-CAST</a></li> <li> <a href="#1013-%E5%85%B3%E4%BA%8E%E5%9C%A8new-%E7%94%9F%E6%88%90%E6%8E%A7%E4%BB%B6%E7%9A%84%E6%97%B6%E5%80%99this-%E7%9A%84%E8%A7%A3%E9%87%8A-%E5%92%8C-deletelater">1.0.13. 关于在new 生成控件的时候this 的解释 和 deleteLater</a> <ul> <li><a href="#10131-%E5%85%B3%E4%BA%8E%E5%9C%A8new-%E7%94%9F%E6%88%90%E6%8E%A7%E4%BB%B6%E7%9A%84%E6%97%B6%E5%80%99this">1.0.13.1. 关于在new 生成控件的时候this</a></li> <li><a href="#10132-deletelater">1.0.13.2. deleteLater</a></li> </ul> </li> <li><a href="#1014-qt%E5%8A%A8%E6%80%81%E5%BA%93">1.0.14. QT动态库</a></li> <li><a href="#1015-qt%E9%9D%99%E6%80%81%E5%BA%93">1.0.15. QT静态库</a></li> <li><a href="#1016-qt-json">1.0.16. QT JSON</a></li> <li><a href="#1017-qt-%E5%8A%A0%E5%AF%86">1.0.17. QT 加密</a></li> <li> <a href="#1018-qsqldatabase-%E7%9A%84%E4%BD%BF%E7%94%A8">1.0.18. QSQLDatabase 的使用</a> <ul> <li><a href="#10181-%E7%BC%96%E8%AF%91mysql-%E9%A9%B1%E5%8A%A8">1.0.18.1. 编译Mysql 驱动</a></li> </ul> </li> <li> <a href="#1019-qsqldatabase-%E7%9A%84%E4%BD%BF%E7%94%A8">1.0.19. QSQLDatabase 的使用</a> <ul> <li><a href="#10191-%E5%8E%9F%E7%94%9F%E6%89%A7%E8%A1%8Csql%E8%AF%AD%E5%8F%A5">1.0.19.1. 原生执行SQL语句</a></li> <li><a href="#10192-qsqlmodel-%E6%96%B9%E5%BC%8F-%E6%89%A7%E8%A1%8C">1.0.19.2. QSQLModel 方式 执行</a></li> <li><a href="#10193-qtableview-%E7%89%B9%E5%88%AB%E6%96%B9%E4%BE%BF">1.0.19.3. QTableView 特别方便</a></li> </ul> </li> <li><a href="#1020-qprocess-%E7%9A%84%E4%BD%BF%E7%94%A8">1.0.20. Qprocess 的使用</a></li> </ul> </li> </ul>

<p><a name="01-%E5%A4%9A%E7%BA%BF%E7%A8%8B"></a></p> <h2> <a id="01-多线程" class="anchor" href="#01-%E5%A4%9A%E7%BA%BF%E7%A8%8B"></a>0.1. 多线程</h2> <p><a name="011-%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C%E5%B7%B2%E7%BB%8F%E5%AD%A6%E5%AE%8C%E4%BB%8A%E5%A4%A9%E7%9C%8B%E4%B8%8B%E5%A4%9A%E7%BA%BF%E7%A8%8B"></a></p> <h3> <a id="011-文件操作已经学完今天看下多线程" class="anchor" href="#011-%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C%E5%B7%B2%E7%BB%8F%E5%AD%A6%E5%AE%8C%E4%BB%8A%E5%A4%A9%E7%9C%8B%E4%B8%8B%E5%A4%9A%E7%BA%BF%E7%A8%8B"></a>0.1.1. 文件操作已经学完,今天看下多线程</h3> <blockquote> <blockquote> <p>2019.11.11 15:06 <a name="0111-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%91"></a></p> </blockquote> </blockquote> <h4> <a id="0111-知识点1" class="anchor" href="#0111-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%91"></a>0.1.1.1. 知识点1</h4> Linux 下编译 带&amp;ltthread&amp;gt 的CPP需要加上 -pthread 编译选项。例如: ``` g++ -std=c++11 -pthread a.cpp <div class="white"><div class="highlight"><pre><span id="LC1" class="line">&lt;li&gt;QT Creator 编译带&amp;ltthread&amp;gt的CPP有BUG,暂时不知道怎么去除!</span> <span id="LC2" class="line"></span> <span id="LC3" class="line">&lt;a id="markdown-0112-代码1" name="0112-代码1"&gt;&lt;/a&gt;</span> <span id="LC4" class="line">#### 0.1.1.2. 代码1</span> <span id="LC5" class="line"></span> <span id="LC6" class="line">```#include &lt;iostream&gt;</span> <span id="LC7" class="line">#include &lt;stdlib.h&gt;v</span> <span id="LC8" class="line">#include &lt;thread&gt;</span> <span id="LC9" class="line"></span> <span id="LC10" class="line">using namespace std;</span> <span id="LC11" class="line">void run(char* p)</span> <span id="LC12" class="line">{</span> <span id="LC13" class="line"> int i=0;</span> <span id="LC14" class="line"> i=system(p);</span> <span id="LC15" class="line">}</span> <span id="LC16" class="line"></span> <span id="LC17" class="line">int main()</span> <span id="LC18" class="line">{</span> <span id="LC19" class="line"> char p[5][20]={</span> <span id="LC20" class="line"> "ls",</span> <span id="LC21" class="line"> "echo nihao",</span> <span id="LC22" class="line"> "gnome-terminal",</span> <span id="LC23" class="line"> "terminator",</span> <span id="LC24" class="line"> "ps -aux"</span> <span id="LC25" class="line"></span> <span id="LC26" class="line"> };</span> <span id="LC27" class="line"></span> <span id="LC28" class="line"> while("nimei")</span> <span id="LC29" class="line"> {</span> <span id="LC30" class="line"> static int i(0);</span> <span id="LC31" class="line"> if(i&lt;5){</span> <span id="LC32" class="line"> thread *pt=new thread(run,p[i]);</span> <span id="LC33" class="line"> i+=1;</span> <span id="LC34" class="line"> cout&lt;&lt;"I now is :\t"&lt;&lt;i&lt;&lt;endl;</span> <span id="LC35" class="line"> }</span> <span id="LC36" class="line"> else{</span> <span id="LC37" class="line"> break;</span> <span id="LC38" class="line"> }</span> <span id="LC39" class="line"></span> <span id="LC40" class="line"> cout&lt;&lt;"Breaking...."&lt;&lt;endl;</span> <span id="LC41" class="line"> }</span> <span id="LC42" class="line"></span> <span id="LC43" class="line"></span> <span id="LC44" class="line"></span> <span id="LC45" class="line"></span> <span id="LC46" class="line"> cin.get();</span> <span id="LC47" class="line"> return 0;</span> <span id="LC48" class="line">}</span></pre></div></div> <p><a name="0113-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%92"></a></p> <h4> <a id="0113-知识点2" class="anchor" href="#0113-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%92"></a>0.1.1.3. 知识点2</h4> 关于thread类的内部成 <table> <thead> <tr> <th>关键字</th> <th>详细解释</th> </tr> </thead> <tbody> <tr> <td>id</td> <td>Thread的id</td> </tr> <tr> <td>native_handle_type</td> <td>native_handle_type</td> </tr> <tr> <td>operator=</td> <td>Move massive Thread</td> </tr> <tr> <td>get_id</td> <td>get Thread ID</td> </tr> <tr> <td>joinable</td> <td>get if joinable</td> </tr> <tr> <td>join</td> <td>join thread</td> </tr> <tr> <td>detach</td> <td>detach thread</td> </tr> <tr> <td>swap</td> <td>swap thread</td> </tr> <tr> <td>native_handle</td> <td>get native handle</td> </tr> <tr> <td>hardware_concurrency[static]</td> <td>Detect hardware concurrency (public static function)</td> </tr> </tbody> </table> <p><img src="/elliot1995/CPP_learn/raw/master/1.jpg" alt=""></p> 线程 detach 脱离主线程的绑定,主线程挂了,子线程不报错,子线程执行完自动退出。 线程 detach以后,子线程会成为孤儿线程,线程之间将无法通信。 <p><a name="012-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%93"></a></p> <h3> <a id="012-知识点3" class="anchor" href="#012-%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%93"></a>0.1.2. 知识点3</h3> 线程中变量的竞争控制是通过 mutex automic 来实现的 mutex : 互斥量。需要包含头文件 &amp;ltmutex&gt; 来使用 --&gt;速度慢 atomic 原子变量。需要包含头文件&amp;ltatomic&gt;来实现 --&gt;速度快,线程安全。 <p><a name="0121-%E4%BB%A3%E7%A0%81%EF%BC%93"></a></p> <h4> <a id="0121-代码3" class="anchor" href="#0121-%E4%BB%A3%E7%A0%81%EF%BC%93"></a>0.1.2.1. 代码3</h4> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;iostream&gt;</span> <span id="LC2" class="line">#include &lt;stdlib.h&gt;</span> <span id="LC3" class="line">#include &lt;thread&gt;</span> <span id="LC4" class="line">#include &lt;atomic&gt;</span> <span id="LC5" class="line"></span> <span id="LC6" class="line">using namespace std;</span> <span id="LC7" class="line">int count(0);</span> <span id="LC8" class="line"></span> <span id="LC9" class="line">void run()</span> <span id="LC10" class="line">{</span> <span id="LC11" class="line"> for(int i(0);i&lt;1000000;i++)</span> <span id="LC12" class="line"> {</span> <span id="LC13" class="line"> count++;</span> <span id="LC14" class="line"> cout&lt;&lt;"\t"&lt;&lt;i&lt;&lt;"\t"&lt;&lt;count&lt;&lt;"\t";</span> <span id="LC15" class="line"> }</span> <span id="LC16" class="line">}</span> <span id="LC17" class="line"></span> <span id="LC18" class="line">int main()</span> <span id="LC19" class="line">{</span> <span id="LC20" class="line"> auto n=thread::hardware_concurrency();</span> <span id="LC21" class="line"></span> <span id="LC22" class="line"> thread* pt[n];</span> <span id="LC23" class="line"> for(int z=0;z&lt;n;z++)</span> <span id="LC24" class="line"> {</span> <span id="LC25" class="line"> pt[z]=new thread(run);</span> <span id="LC26" class="line"> pt[z]-&gt;detach();</span> <span id="LC27" class="line"></span> <span id="LC28" class="line"> }</span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> cout&lt;&lt;"Finally count is \t"&lt;&lt;count&lt;&lt;endl;</span> <span id="LC31" class="line"> cout&lt;&lt;"Used "&lt;&lt;n &lt;&lt;"threads"&lt;&lt;endl;</span> <span id="LC32" class="line"></span> <span id="LC33" class="line"></span> <span id="LC34" class="line"></span> <span id="LC35" class="line"></span> <span id="LC36" class="line"> cin.get();</span> <span id="LC37" class="line"> return 0;</span> <span id="LC38" class="line">}</span></pre></div></div> <p><em>运行结果不是1000000×2.</em></p> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;stdlib.h&gt;</span> <span id="LC2" class="line">#include &lt;thread&gt;</span> <span id="LC3" class="line">#include &lt;atomic&gt;</span> <span id="LC4" class="line"></span> <span id="LC5" class="line">using namespace std;</span> <span id="LC6" class="line">int count(0);</span> <span id="LC7" class="line"></span> <span id="LC8" class="line">void run()</span> <span id="LC9" class="line">{</span> <span id="LC10" class="line"> for(int i(0);i&lt;1000000;i++)</span> <span id="LC11" class="line"> {</span> <span id="LC12" class="line"> count++;</span> <span id="LC13" class="line"> cout&lt;&lt;"\t"&lt;&lt;i&lt;&lt;"\t"&lt;&lt;count&lt;&lt;"\t";</span> <span id="LC14" class="line"> }</span> <span id="LC15" class="line">}</span> <span id="LC16" class="line"></span> <span id="LC17" class="line">int main()</span> <span id="LC18" class="line">{</span> <span id="LC19" class="line"> auto n=thread::hardware_concurrency();</span> <span id="LC20" class="line"></span> <span id="LC21" class="line"> thread* pt[n];</span> <span id="LC22" class="line"> for(int z=0;z&lt;n;z++)</span> <span id="LC23" class="line"> {</span> <span id="LC24" class="line"> pt[z]=new thread(run);</span> <span id="LC25" class="line"> pt[z]-&gt;detach();</span> <span id="LC26" class="line"></span> <span id="LC27" class="line"> }</span> <span id="LC28" class="line"></span> <span id="LC29" class="line"> cout&lt;&lt;"Finally count is \t"&lt;&lt;count&lt;&lt;endl;</span> <span id="LC30" class="line"> cout&lt;&lt;"Used "&lt;&lt;n &lt;&lt;"threads"&lt;&lt;endl;</span> <span id="LC31" class="line"></span> <span id="LC32" class="line"></span> <span id="LC33" class="line"></span> <span id="LC34" class="line"></span> <span id="LC35" class="line"> cin.get();</span> <span id="LC36" class="line"> return 0;</span> <span id="LC37" class="line">}</span></pre></div></div> <p><em>运行结果是1000000×2.正确</em></p> <em>atomic 声明方式为 atomic&amp;ltint&gt; a(100); 等号初始化会报错 <em>vim 按CTRL+S 后假死按 CTRL+q 退出 <p><a name="02-stl"></a></p> <h2> <a id="02-stl" class="anchor" href="#02-stl"></a>0.2. STL</h2> <p><a name="021-stl-%E5%AD%A6%E4%B9%A0%E6%B2%A1%E6%9C%89%E5%B8%A6%E7%AC%94%E8%AE%B0%E6%9C%AC%E5%9B%9E%E6%9D%A5%E6%88%91%E6%97%A5-20191111-2321"></a></p> <h3> <a id="021-stl-学习没有带笔记本回来我日-20191111-2321" class="anchor" href="#021-stl-%E5%AD%A6%E4%B9%A0%E6%B2%A1%E6%9C%89%E5%B8%A6%E7%AC%94%E8%AE%B0%E6%9C%AC%E5%9B%9E%E6%9D%A5%E6%88%91%E6%97%A5-20191111-2321"></a>0.2.1. STL 学习,没有带笔记本回来。。。我日 2019.11.11 23:21</h3> <p><a name="0211-stl%E7%9A%84%E6%A6%82%E5%BF%B5"></a></p> <h4> <a id="0211-stl的概念" class="anchor" href="#0211-stl%E7%9A%84%E6%A6%82%E5%BF%B5"></a>0.2.1.1. STL的概念</h4> Iterator (迭代器) Container (容器) array Alogrithm (算法) Adapters (配接器) 用来实现容器之间的转接<br> 面向过程--》面向对象-》基于对象-》泛型 <p><a name="0212-%E4%BB%A3%E7%A0%81"></a></p> <h4> <a id="0212-代码" class="anchor" href="#0212-%E4%BB%A3%E7%A0%81"></a>0.2.1.2. 代码</h4> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;iostream&gt;</span> <span id="LC2" class="line">#include &lt;vector&gt; //容器</span> <span id="LC3" class="line">#include &lt;algorithm&gt; //算法</span> <span id="LC4" class="line"></span> <span id="LC5" class="line">using namespace std;</span> <span id="LC6" class="line"></span> <span id="LC7" class="line">//专么实现一个类模板,实现打印</span> <span id="LC8" class="line">//类模板实现了方法</span> <span id="LC9" class="line">template &lt;class T&gt;</span> <span id="LC10" class="line">class vector_s {</span> <span id="LC11" class="line">public:</span> <span id="LC12" class="line">void operator()(const T &amp;t1) //重载了小括号 实现访问小括号就直接打印</span> <span id="LC13" class="line"> {</span> <span id="LC14" class="line"> cout &lt;&lt; t1 &lt;&lt; endl;</span> <span id="LC15" class="line"> }</span> <span id="LC16" class="line">};</span> <span id="LC17" class="line"></span> <span id="LC18" class="line">int main(void)</span> <span id="LC19" class="line">{</span> <span id="LC20" class="line"> vector&lt;int&gt; myv;</span> <span id="LC21" class="line"> myv.push_back(13);</span> <span id="LC22" class="line"> myv.push_back(23);</span> <span id="LC23" class="line"> myv.push_back(33);</span> <span id="LC24" class="line"> myv.push_back(113);</span> <span id="LC25" class="line"> myv.push_back(1995);</span> <span id="LC26" class="line"> myv.push_back(1996);</span> <span id="LC27" class="line"></span> <span id="LC28" class="line"> vector_s&lt;int&gt; print; // 对打印实现实例化</span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> //myv.begin(), myv.end() 是迭代器 本质是指针</span> <span id="LC31" class="line"> // for_each 本质是一个算法</span> <span id="LC32" class="line"> for_each(myv.begin(), myv.end(), print);</span> <span id="LC33" class="line"></span> <span id="LC34" class="line"> cin.get();</span> <span id="LC35" class="line"></span> <span id="LC36" class="line"></span> <span id="LC37" class="line"></span> <span id="LC38" class="line"> return 0;</span> <span id="LC39" class="line">}</span></pre></div></div> <br> 注意:算法需要包含头文件 &amp;ltalgorithm&amp;gt <br> 这个遍历的函数为 for_each 不是foreach<br> 算法可以适用于任何容器<br> <p><a name="0213-stl%E6%8A%BD%E8%B1%A1%E7%9A%84%E6%98%AF%E4%BB%80%E4%B9%88"></a></p> <h4> <a id="0213-stl抽象的是什么" class="anchor" href="#0213-stl%E6%8A%BD%E8%B1%A1%E7%9A%84%E6%98%AF%E4%BB%80%E4%B9%88"></a>0.2.1.3. STL抽象的是什么</h4> <p>-有些算法并不依赖于数据结构的特定实现,而只依赖于该结构的几个基本语义属性</p> <p>-STL 抽象出的这些基本属性(concept)成功的将算法和数据结构相分离,在没有效率损失的前提下,获得了极大的弹性!</p> <p><a name="022-%E5%85%AD%E5%A4%A7%E7%BB%84%E4%BB%B6"></a></p> <h3> <a id="022-六大组件" class="anchor" href="#022-%E5%85%AD%E5%A4%A7%E7%BB%84%E4%BB%B6"></a>0.2.2. 六大组件</h3> <p>-容器 (container)</p> <p>-算法Algorithm</p> <p>-迭代器 (iterator)</p> <p>-仿函数 (Function Object)</p> <p>-适配器 (Adaptor)</p> <p>-空间制配器 (allocator)</p> <p>获取远程代码修改后,想要push到远端与原来不同的新分支,可以使用下面的命令实现: git push origin 本地分支:远端希望创建的分支</p> <p>例如git下来的分支为master</p> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"></span> <span id="LC2" class="line">git branch master</span> <span id="LC3" class="line">git push origin master:my_remote_new_branch</span> <span id="LC4" class="line">#远端即可创建新的分支my_remote_new_branch,提交本地修改</span> <span id="LC5" class="line"></span></pre></div></div>

<p><code> std::for_each(arr.begin(), arr.end(), func);</code></p> <p>比较有用的for_each 用法</p> <p><code>for (auto n:Original)</code></p> <p>C++ 11 新的for</p>

<h4> <a id="typename" class="anchor" href="#typename"></a>typename</h4> <ul> <li>成员函数也可以是模板-</li> <li>typename 作为类型前的标识符号-</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">class MyClass</span> <span id="LC2" class="line"> typename T::SubType * ptr; //typename 直接指示 T::SubType * 为一个类型 没有typename 会被解析为</span> <span id="LC3" class="line"> // T域中的 SubType 静态成员 乘于(*) ptr.</span> <span id="LC4" class="line"> ...</span> <span id="LC5" class="line"> };</span></pre></div></div> <p>-typename 的第二个作用:在模板声明中替换class -</p> <p><code>template &lt;typename T &gt; class Myclass;</code></p> <h4> <a id="static_cast-只有当类型转换有所定义转换才会成功明确转换类型" class="anchor" href="#static_cast-%E5%8F%AA%E6%9C%89%E5%BD%93%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E6%9C%89%E6%89%80%E5%AE%9A%E4%B9%89%E8%BD%AC%E6%8D%A2%E6%89%8D%E4%BC%9A%E6%88%90%E5%8A%9F%E6%98%8E%E7%A1%AE%E8%BD%AC%E6%8D%A2%E7%B1%BB%E5%9E%8B"></a>static_cast 只有当类型转换有所定义,转换才会成功。(明确转换类型)</h4> <h4> <a id="dynamic_cast-将多态类型向下转换downcast-被转换的类必须有虚函数否则失败" class="anchor" href="#dynamic_cast-%E5%B0%86%E5%A4%9A%E6%80%81%E7%B1%BB%E5%9E%8B%E5%90%91%E4%B8%8B%E8%BD%AC%E6%8D%A2downcast-%E8%A2%AB%E8%BD%AC%E6%8D%A2%E7%9A%84%E7%B1%BB%E5%BF%85%E9%A1%BB%E6%9C%89%E8%99%9A%E5%87%BD%E6%95%B0%E5%90%A6%E5%88%99%E5%A4%B1%E8%B4%A5"></a>dynamic_cast 将多态类型向下转换&lt;downcast&gt; 被转换的类必须有虚函数。否则失败</h4> <h4> <a id="const_cast-用于转换为const-值" class="anchor" href="#const_cast-%E7%94%A8%E4%BA%8E%E8%BD%AC%E6%8D%A2%E4%B8%BAconst-%E5%80%BC"></a>const_cast 用于转换为const 值</h4> <h4> <a id="reinterpret_cast-多用于指针个的转换" class="anchor" href="#reinterpret_cast-%E5%A4%9A%E7%94%A8%E4%BA%8E%E6%8C%87%E9%92%88%E4%B8%AA%E7%9A%84%E8%BD%AC%E6%8D%A2"></a>reinterpret_cast 多用于指针个的转换</h4> <h4> <a id="c语言的小括号可以替换除dymanic_cast-外的所有静态转换函数无法明确显示使用他们的切确理由" class="anchor" href="#c%E8%AF%AD%E8%A8%80%E7%9A%84%E5%B0%8F%E6%8B%AC%E5%8F%B7%E5%8F%AF%E4%BB%A5%E6%9B%BF%E6%8D%A2%E9%99%A4dymanic_cast-%E5%A4%96%E7%9A%84%E6%89%80%E6%9C%89%E9%9D%99%E6%80%81%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E6%97%A0%E6%B3%95%E6%98%8E%E7%A1%AE%E6%98%BE%E7%A4%BA%E4%BD%BF%E7%94%A8%E4%BB%96%E4%BB%AC%E7%9A%84%E5%88%87%E7%A1%AE%E7%90%86%E7%94%B1"></a>C语言的小括号()可以替换除dymanic_cast 外的所有静态转换函数,无法明确显示使用他们的切确理由</h4> <h3> <a id="容器的概念" class="anchor" href="#%E5%AE%B9%E5%99%A8%E7%9A%84%E6%A6%82%E5%BF%B5"></a>容器的概念</h3> <p>-用于管理一组元素 <img src="/elliot1995/CPP_learn/raw/master/container.jpg" alt=""></p> <h4> <a id="容器的分类" class="anchor" href="#%E5%AE%B9%E5%99%A8%E7%9A%84%E5%88%86%E7%B1%BB"></a>容器的分类</h4> <h5> <a id="-序列式容器sequence-containers" class="anchor" href="#-%E5%BA%8F%E5%88%97%E5%BC%8F%E5%AE%B9%E5%99%A8sequence-containers"></a>-序列式容器(Sequence containers)</h5> <p>-每个元素都有固定位置--去结余插入时机和地点,和元素值无关 -vector list deque</p> <h5> <a id="关联式容器-associated-containers" class="anchor" href="#%E5%85%B3%E8%81%94%E5%BC%8F%E5%AE%B9%E5%99%A8-associated-containers"></a>关联式容器 (Associated containers)</h5> <p>-元素取决于特定的排序准则,和插入顺序无关 -set multiset map multimap <img src="/elliot1995/CPP_learn/raw/master/container1.jpg" alt=""></p> <h4> <a id="array" class="anchor" href="#array"></a>array</h4> <p><code> array &lt;int ,5&gt;={1,2,3,4,5};</code> -静态数组,栈上 <a name="0221-vector-%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84-%E5%A0%86%E4%B8%8A"></a></p> <h4> <a id="0221-vector-动态数组-堆上" class="anchor" href="#0221-vector-%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84-%E5%A0%86%E4%B8%8A"></a>0.2.2.1. vector 动态数组 堆上</h4> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> mv.push_back()</span></pre></div></div> <p>-不需要变长,容量较小,array 需要变长,容量较大,用vector</p> <p><a name="0222-tuple-%E5%8F%AF%E4%BB%A5%E5%AD%98%E5%82%A8%E4%B8%8D%E5%90%8C%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"></a></p> <h4> <a id="0222-tuple-可以存储不同的数据类型" class="anchor" href="#0222-tuple-%E5%8F%AF%E4%BB%A5%E5%AD%98%E5%82%A8%E4%B8%8D%E5%90%8C%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"></a>0.2.2.2. tuple 可以存储不同的数据类型</h4> <p><a name="0223-list-%E9%80%82%E5%90%88%E7%BB%8F%E5%B8%B8%E6%8F%92%E5%85%A5%E7%BB%8F%E5%B8%B8%E5%88%A0%E9%99%A4%E7%9A%84%E6%83%85%E5%86%B5"></a></p> <h4> <a id="0223-list-适合经常插入经常删除的情况" class="anchor" href="#0223-list-%E9%80%82%E5%90%88%E7%BB%8F%E5%B8%B8%E6%8F%92%E5%85%A5%E7%BB%8F%E5%B8%B8%E5%88%A0%E9%99%A4%E7%9A%84%E6%83%85%E5%86%B5"></a>0.2.2.3. list 适合经常插入,经常删除的情况</h4> <p><a name="0224-list-%E5%AE%B9%E5%99%A8"></a></p> <h4> <a id="0224-list-容器" class="anchor" href="#0224-list-%E5%AE%B9%E5%99%A8"></a>0.2.2.4. list 容器</h4> <p>-list容器是无序容器</p> <p>-list 容器只能通过迭代器访问。通过++ -- 遍历</p> <p>-list容器可以使用remove()方法删除元素,</p> <p>-可以同时正向和反向查找</p> <p>-可以使用sort()函数排序</p> <p>-可以使用merge 合并,但是合并之前必须排序</p> <p>-可以使用unique()函数删除重复项,删除前必须排序。</p> <p>--merge (使)合并,结合,并入;相融;融入;渐渐消失在某物中</p> <p><a name="0225-set-%E5%AE%B9%E5%99%A8-%E7%BA%A2%E9%BB%91%E6%A0%91%E5%AE%B9%E5%99%A8"></a></p> <h4> <a id="0225-set-容器-红黑树容器" class="anchor" href="#0225-set-%E5%AE%B9%E5%99%A8-%E7%BA%A2%E9%BB%91%E6%A0%91%E5%AE%B9%E5%99%A8"></a>0.2.2.5. set 容器 (红黑树容器)</h4> <p>-set容器不能有重复项,重复的会被舍弃 -set容器会自动排序 -set 用insert插入元素 -set可以用find()函数查找位置</p> <p>循环加栈</p> <p><a name="023-algorithm-%E7%AE%97%E6%B3%95"></a></p> <h3> <a id="023-algorithm-算法" class="anchor" href="#023-algorithm-%E7%AE%97%E6%B3%95"></a>0.2.3. algorithm 算法</h3> <p>find()函数可以查找数组、容器中的元素。</p> <p>for_each(); 遍历每一个元素。</p> <p>multiset和set差不多,但是允许重复元素。</p> <p>迭代器本质是智能指针。</p> <p><a name="024-iterator"></a></p> <h3> <a id="024-iterator" class="anchor" href="#024-iterator"></a>0.2.4. Iterator</h3> <p><img src="/elliot1995/CPP_learn/raw/master/iterator.jpg" alt=""></p> <p><a name="0241-%E4%BB%BF%E5%87%BD%E6%95%B0"></a></p> <h4> <a id="0241-仿函数" class="anchor" href="#0241-%E4%BB%BF%E5%87%BD%E6%95%B0"></a>0.2.4.1. 仿函数</h4> <p><code>auto ifind=find_if(mylist.bengin(),mylisy.end(),bindlst(greater&lt;int&gt;(),3));</code></p> <p>bindlst 需要头文件 funtional <code>#include &lt;functional&gt;</code></p> <p><code>bindlst(greater&lt;int&gt;(),3);</code> 绑定一个函数。 <code>greater&lt;int&gt;() </code> 是一个仿函数(functional) 是一个重载了()的类/结构 体 ,可以用来实现一定的算法策略。</p> <p>仿函数例子:</p> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;list&gt;</span> <span id="LC2" class="line">#include &lt;functional&gt;</span> <span id="LC3" class="line">#include &lt;array&gt;</span> <span id="LC4" class="line">#include &lt;algorithm&gt;</span> <span id="LC5" class="line"></span> <span id="LC6" class="line">using namespace std;</span> <span id="LC7" class="line"></span> <span id="LC8" class="line"></span> <span id="LC9" class="line">class shuchu {</span> <span id="LC10" class="line"> public:</span> <span id="LC11" class="line"></span> <span id="LC12" class="line"> void operator()(int x)</span> <span id="LC13" class="line"> {</span> <span id="LC14" class="line"> std::cout&lt;&lt;x&lt;&lt;endl;</span> <span id="LC15" class="line"> }</span> <span id="LC16" class="line">};</span> <span id="LC17" class="line"></span> <span id="LC18" class="line"></span> <span id="LC19" class="line"></span> <span id="LC20" class="line"> int main(int argc, char const *argv[]) {</span> <span id="LC21" class="line"> /* code */</span> <span id="LC22" class="line"></span> <span id="LC23" class="line"> // array&lt;int,5&gt;array1({1,2,3,4,5});</span> <span id="LC24" class="line"> list &lt;int&gt;ls1;</span> <span id="LC25" class="line"> ls1.push_back(1);</span> <span id="LC26" class="line"> ls1.push_back(3);</span> <span id="LC27" class="line"> ls1.push_back(5);</span> <span id="LC28" class="line"> ls1.push_back(7);</span> <span id="LC29" class="line"> ls1.push_back(9);</span> <span id="LC30" class="line"> auto ib=ls1.begin();</span> <span id="LC31" class="line"> auto ie=ls1.end();</span> <span id="LC32" class="line"> for_each(ib,ie,shuchu());</span> <span id="LC33" class="line"> cin.get();</span> <span id="LC34" class="line"> return 0;</span> <span id="LC35" class="line">}</span> <span id="LC36" class="line"></span></pre></div></div> <p><a name="025-wmv-stl-bug-%E8%BF%AD%E4%BB%A3%E5%99%A8"></a></p> <h3> <a id="025-wmv-stl-bug-迭代器" class="anchor" href="#025-wmv-stl-bug-%E8%BF%AD%E4%BB%A3%E5%99%A8"></a>0.2.5. wmv STL Bug 迭代器</h3> <blockquote> <blockquote> <p>2019.11.15</p> </blockquote> </blockquote> <p>-智能指针有一个_Ptr属性,可以打印里面的指针。</p> <p>-STL 有bug 先访问迭代器的_Ptr属性,再访问迭代器指针正常,但是反过来会出错。</p> <p>-分行打印就没有问题。</p> <p><a name="026-wmv-%E6%A0%88%E9%98%9F%E5%88%97%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97%E4%BC%98%E5%85%88%E9%98%9F%E5%88%97"></a></p> <h3> <a id="026-wmv-栈队列双端队列优先队列" class="anchor" href="#026-wmv-%E6%A0%88%E9%98%9F%E5%88%97%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97%E4%BC%98%E5%85%88%E9%98%9F%E5%88%97"></a>0.2.6. wmv 栈队列双端队列优先队列</h3> <blockquote> <blockquote> <p>2019.11.15</p> </blockquote> </blockquote> <p><a name="0261-stack-%E5%85%B3%E9%94%AE%E5%AD%97%E5%8F%AF%E4%BB%A5%E6%9E%84%E5%BB%BA%E6%A0%88"></a></p> <h4> <a id="0261-stack-关键字可以构建栈" class="anchor" href="#0261-stack-%E5%85%B3%E9%94%AE%E5%AD%97%E5%8F%AF%E4%BB%A5%E6%9E%84%E5%BB%BA%E6%A0%88"></a>0.2.6.1. stack 关键字可以构建栈</h4> <p>-用法</p> <p><code>stack &lt;mystack&gt;; //声明一个栈</code> <code>mystack.push(num); //压栈</code> <code>mystack.pop(); //出栈</code> <code>mystack.top(); //获取第一个元素</code></p> <p>stack成员函数示例 -size( ) :返回栈中元素个数 -top( ) :返回栈顶的元素 -pop( ) :从栈中取出并删除元素 -push(e) :向栈中添加元素e -empty( ) :栈为空时返回true</p> <p><a name="0262-queue--%E9%98%9F%E5%88%97"></a></p> <h4> <a id="0262-queue--队列" class="anchor" href="#0262-queue--%E9%98%9F%E5%88%97"></a>0.2.6.2. queue 队列</h4> <p>-queue 英 [kjuː] 美 [kjuː]</p> <p>n.(人、汽车等的)队,行列;(储存的数据)队列 v.(人、车等)排队等候;(使)排队;列队等待</p> <p>-queue 需要头文件 &lt;queue&gt;</p> <p>-从网上拔来的Queue</p> <p><a href="/elliot1995/CPP_learn/blob/master/queue.md">queue详解</a></p> <h2> <a id="queue-操作" class="anchor" href="#queue-%E6%93%8D%E4%BD%9C"></a>queue 操作</h2> queue 和 stack 有一些成员函数相似,但在一些情况下,工作方式有些不同:<br> <ul> <li> front():返回 queue 中第一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。</li> <li> back():返回 queue 中最后一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。</li> <li> push(const T&amp; obj):在 queue 的尾部添加一个元素的副本。这是通过调用底层容器的成员函数 push_back() 来完成的。</li> <li> push(T&amp;&amp; obj):以移动的方式在 queue 的尾部添加元素。这是通过调用底层容器的具有右值引用参数的成员函数 push_back() 来完成的。</li> <li> pop():删除 queue 中的第一个元素。</li> <li> size():返回 queue 中元素的个数。</li> <li> empty():如果 queue 中没有元素的话,返回 true。</li> <li> emplace():用传给 emplace() 的参数调用 T 的构造函数,在 queue 的尾部生成对象。</li> <li> swap(queue&lt;T&gt; &amp;other_q):将当前 queue 中的元素和参数 queue 中的元素交换。它们需要包含相同类型的元素。也可以调用全局函数模板&nbsp;swap() 来完成同样的操作。</li> </ul> <br> queue&lt;T&gt; 模板定义了拷贝和移动版的 operator=(),对于所保存元素类型相同的 queue 对象,它们有一整套的比较运算符,这些运算符的工作方式和 stack 容器相同。<br> <p><a name="0263-deque-%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97"></a></p> <h4> <a id="0263-deque-双端队列" class="anchor" href="#0263-deque-%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97"></a>0.2.6.3. deque 双端队列</h4> <ul> <li>deque 可以从头部push_front()和尾部push_back()插入</li> <li>deque 可以使用迭代器 可以使用 迭代器+n 访问删除</li> <li>deque 可以pop_front() pop_back() 从头部和尾部 删除元素。</li> </ul> <p>(1)&nbsp;&nbsp;&nbsp;&nbsp;构造函数</p> <p>deque():创建一个空deque</p> <p>deque(int nSize):创建一个deque,元素个数为nSize</p> <p>deque(int nSize,const T&amp; t):创建一个deque,元素个数为nSize,且值均为t</p> <p>deque(const deque &amp;):复制构造函数</p> <p>(2)&nbsp;&nbsp;&nbsp;&nbsp;增加函数</p> <p>void push_front(const T&amp; x):双端队列头部增加一个元素X</p> <p>void push_back(const T&amp; x):双端队列尾部增加一个元素x</p> <p>iterator insert(iterator it,const T&amp; x):双端队列中某一元素前增加一个元素x</p> <p>void insert(iterator it,int n,const T&amp; x):双端队列中某一元素前增加n个相同的元素x</p> <p>void insert(iterator it,const_iterator first,const_iteratorlast):双端队列中某一元素前插入另一个相同类型向量的[forst,last)间的数据</p> <p>(3)&nbsp;&nbsp;&nbsp;&nbsp;删除函数</p> <p>Iterator erase(iterator it):删除双端队列中的某一个元素</p> <p>Iterator erase(iterator first,iterator last):删除双端队列中[first,last)中的元素</p> <p>void pop_front():删除双端队列中最前一个元素</p> <p>void pop_back():删除双端队列中最后一个元素</p> <p>void clear():清空双端队列中最后一个元素</p> <p>(4)&nbsp;&nbsp;&nbsp;&nbsp;遍历函数</p> <p>reference at(int pos):返回pos位置元素的引用</p> <p>reference front():返回手元素的引用</p> <p>reference back():返回尾元素的引用</p> <p>iterator begin():返回向量头指针,指向第一个元素</p> <p>iterator end():返回指向向量中最后一个元素下一个元素的指针(不包含在向量中)</p> <p>reverse_iterator rbegin():反向迭代器,指向最后一个元素</p> <p>reverse_iterator rend():反向迭代器,指向第一个元素的前一个元素</p> <p>(5)&nbsp;&nbsp;&nbsp;&nbsp;判断函数</p> <p>bool empty() const:向量是否为空,若true,则向量中无元素</p> <p>(6)&nbsp;&nbsp;&nbsp;&nbsp;大小函数</p> <p>Int size() const:返回向量中元素的个数</p> <p>int max_size() const:返回最大可允许的双端对了元素数量值</p> <p>(7)&nbsp;&nbsp;&nbsp;&nbsp;其他函数</p> <p>void swap(deque&amp;):交换两个同类型向量的数据</p> <p>void assign(int n,const T&amp; x):向量中第n个元素的值设置为x &nbsp; <a href="/elliot1995/CPP_learn/blob/master/deque.md">deque网上扒来的</a></p> <p><a name="0264-priority_que-%E4%BC%98%E5%85%88%E7%BA%A7%E9%98%9F%E5%88%97-%E4%B8%8D%E5%A4%AA%E7%90%86%E8%A7%A3"></a></p> <h4> <a id="0264-priority_que-优先级队列-不太理解" class="anchor" href="#0264-priority_que-%E4%BC%98%E5%85%88%E7%BA%A7%E9%98%9F%E5%88%97-%E4%B8%8D%E5%A4%AA%E7%90%86%E8%A7%A3"></a>0.2.6.4. priority_que 优先级队列 (不太理解)</h4> <ul> <li>优先队列是按照堆来实现的</li> </ul> <p><a name="027-%E7%BA%A2%E9%BB%91%E6%A0%91%E5%AE%B9%E5%99%A8"></a></p> <h3> <a id="027-红黑树容器" class="anchor" href="#027-%E7%BA%A2%E9%BB%91%E6%A0%91%E5%AE%B9%E5%99%A8"></a>0.2.7. 红黑树容器</h3> <p><a name="0271-set-%E6%98%AF%E4%B8%80%E4%B8%AA%E7%BA%A2%E9%BB%91%E6%A0%91"></a></p> <h4> <a id="0271-set-是一个红黑树" class="anchor" href="#0271-set-%E6%98%AF%E4%B8%80%E4%B8%AA%E7%BA%A2%E9%BB%91%E6%A0%91"></a>0.2.7.1. set 是一个红黑树</h4> <p><a name="028-set-map-multi-map-%E7%BA%A2%E9%BB%91%E6%A0%91"></a></p> <h3> <a id="028-set-map-multi-map-红黑树" class="anchor" href="#028-set-map-multi-map-%E7%BA%A2%E9%BB%91%E6%A0%91"></a>0.2.8. set map multi-map 红黑树</h3> <p><a name="0281-set%E7%9A%84%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95"></a></p> <h4> <a id="0281-set的高级用法" class="anchor" href="#0281-set%E7%9A%84%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95"></a>0.2.8.1. set的高级用法</h4> <ul> <li>红黑树,处理纯字符串比较少,处理字符串及对象比较多。</li> <li>pair 复合集合</li> <li>二叉树查找依赖于有序。 字符串可以实现有序。</li> <li>pair 也是个类模板!起到获取插入返回值(boolean)的作用。两个参数,第一个类型,第二个是比大小的方法</li> <li>set的每一个节点就是一个节点</li> <li>二叉树的实现语法一直在在在变 。VS2013 能过的 VS2017 就不能过了!~!!mingw 32又能过,代码如下:</li> <li>set 不能包含同样的值</li> <li>0-35:40</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;set&gt;</span> <span id="LC2" class="line">#include &lt;string&gt;</span> <span id="LC3" class="line">#include &lt;stdio.h&gt;</span> <span id="LC4" class="line">#include &lt;string.h&gt;</span> <span id="LC5" class="line"></span> <span id="LC6" class="line">using namespace std;</span> <span id="LC7" class="line"></span> <span id="LC8" class="line">class strless</span> <span id="LC9" class="line">{</span> <span id="LC10" class="line">public:</span> <span id="LC11" class="line"></span> <span id="LC12" class="line"> bool operator () (const char* p,const char* p1)</span> <span id="LC13" class="line"> {</span> <span id="LC14" class="line"> return strcmp(p, p1) &lt; 0;</span> <span id="LC15" class="line"> }</span> <span id="LC16" class="line"></span> <span id="LC17" class="line">private:</span> <span id="LC18" class="line"></span> <span id="LC19" class="line">};</span> <span id="LC20" class="line"></span> <span id="LC21" class="line"></span> <span id="LC22" class="line"></span> <span id="LC23" class="line">int main(void)</span> <span id="LC24" class="line">{</span> <span id="LC25" class="line"> const char* cmd[] = { "nihao","spectrc","calc","good" };</span> <span id="LC26" class="line"> set&lt;const char *, strless&gt;myset(cmd,cmd+4,strless());</span> <span id="LC27" class="line"> auto ib = myset.begin();</span> <span id="LC28" class="line"> auto ie = myset.end();</span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> for (auto i : myset)</span> <span id="LC31" class="line"> {</span> <span id="LC32" class="line"> cout &lt;&lt; i &lt;&lt; endl;</span> <span id="LC33" class="line"> }</span> <span id="LC34" class="line"></span> <span id="LC35" class="line"></span> <span id="LC36" class="line"> return 0;</span> <span id="LC37" class="line"></span> <span id="LC38" class="line">}</span></pre></div></div> <p><a name="0282-multiset"></a></p> <h4> <a id="0282-multiset" class="anchor" href="#0282-multiset"></a>0.2.8.2. multiset</h4> <ul> <li>可以插入相同的值</li> <li>multiset 的每一个节点是一个链表</li> <li>练习代码如下(mingw 32编译):</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;set&gt;</span> <span id="LC2" class="line">#include &lt;string.h&gt;</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">using namespace std;</span> <span id="LC5" class="line"></span> <span id="LC6" class="line">class stu{</span> <span id="LC7" class="line">public:</span> <span id="LC8" class="line"> int id;</span> <span id="LC9" class="line"> char p[100];</span> <span id="LC10" class="line">};</span> <span id="LC11" class="line"></span> <span id="LC12" class="line">class stuless</span> <span id="LC13" class="line">{</span> <span id="LC14" class="line">public:</span> <span id="LC15" class="line"></span> <span id="LC16" class="line"> bool operator ()(const stu &amp;st1,const stu &amp;st2)</span> <span id="LC17" class="line"> {</span> <span id="LC18" class="line"> return st1.id&lt;st2.id;</span> <span id="LC19" class="line"> }</span> <span id="LC20" class="line"></span> <span id="LC21" class="line">};</span> <span id="LC22" class="line"></span> <span id="LC23" class="line"></span> <span id="LC24" class="line"></span> <span id="LC25" class="line">int main()</span> <span id="LC26" class="line">{</span> <span id="LC27" class="line"> stu student[3]={</span> <span id="LC28" class="line"> {99,"zhuang"},</span> <span id="LC29" class="line"> {15,"li"},</span> <span id="LC30" class="line"> {3,"ooooo"}</span> <span id="LC31" class="line"> };</span> <span id="LC32" class="line"></span> <span id="LC33" class="line"> stu newstu;</span> <span id="LC34" class="line"> newstu.id=100782;</span> <span id="LC35" class="line"> strcpy(newstu.p,"nimeide");</span> <span id="LC36" class="line"> multiset&lt;stu,stuless&gt; stu1 (student,student+3,stuless());</span> <span id="LC37" class="line"> stu1.insert(newstu);</span> <span id="LC38" class="line"> strcpy(newstu.p,"SBSBSBSB");</span> <span id="LC39" class="line"> stu1.insert(newstu);</span> <span id="LC40" class="line"> strcpy(newstu.p,"luo liuo");</span> <span id="LC41" class="line"> stu1.insert(newstu);</span> <span id="LC42" class="line"></span> <span id="LC43" class="line"> for(auto i:stu1)</span> <span id="LC44" class="line"> {</span> <span id="LC45" class="line"> cout&lt;&lt; "\t"&lt;&lt;i.id&lt;&lt;"\t"&lt;&lt;i.p&lt;&lt;endl;</span> <span id="LC46" class="line"> }</span> <span id="LC47" class="line"></span> <span id="LC48" class="line"> return 0;</span> <span id="LC49" class="line"></span> <span id="LC50" class="line">}</span></pre></div></div> <ul> <li> <p>输出</p> </li> <li> </li> </ul> <p><img src="/elliot1995/CPP_learn/raw/master/cout1.jpg" alt="输出结果"></p> <p><a name="0283-map--%E6%98%A0%E5%B0%84-multimap-%E5%A4%9A%E5%B1%82%E6%98%A0%E5%B0%84-nbsp9wmv-5341"></a></p> <h4> <a id="0283-map--映射-multimap-多层映射-9wmv-5341" class="anchor" href="#0283-map--%E6%98%A0%E5%B0%84-multimap-%E5%A4%9A%E5%B1%82%E6%98%A0%E5%B0%84-9wmv-5341"></a>0.2.8.3. map 映射 multimap 多层映射 &nbsp;9.wmv 53:41</h4> <ul> <li>map 也是 红黑树,但是能同时映射多个数据</li> <li>map 需要头文件 &lt;map&gt;</li> <li>映射都是左边映射到右边。访问时用右边访问左边。可以通过对等的映射查找</li> <li>示例代码如下:</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;string&gt;</span> <span id="LC2" class="line">#include &lt;map&gt;</span> <span id="LC3" class="line">#include &lt;stdlib.h&gt;</span> <span id="LC4" class="line"></span> <span id="LC5" class="line">using namespace std;</span> <span id="LC6" class="line"></span> <span id="LC7" class="line">class worker{</span> <span id="LC8" class="line"></span> <span id="LC9" class="line">public:</span> <span id="LC10" class="line"> int id;</span> <span id="LC11" class="line"> string name;</span> <span id="LC12" class="line"> string work;</span> <span id="LC13" class="line">};</span> <span id="LC14" class="line"></span> <span id="LC15" class="line"></span> <span id="LC16" class="line">class winfo{</span> <span id="LC17" class="line">public:</span> <span id="LC18" class="line"> int randid;</span> <span id="LC19" class="line"> worker w1;</span> <span id="LC20" class="line"></span> <span id="LC21" class="line">};</span> <span id="LC22" class="line"></span> <span id="LC23" class="line"></span> <span id="LC24" class="line">int main()</span> <span id="LC25" class="line">{</span> <span id="LC26" class="line"> system("chcp 65001");</span> <span id="LC27" class="line"></span> <span id="LC28" class="line"> winfo warr[]={</span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> {1,{10,"李四","mugong"}},</span> <span id="LC31" class="line"> {2,{9,"张三","mugong"}},</span> <span id="LC32" class="line"> {8,{5,"wangermazi","mugong"}},</span> <span id="LC33" class="line"> {20,{3,"gg","mugong"}},</span> <span id="LC34" class="line"> {30,{1,"ww张三","mugong"}}</span> <span id="LC35" class="line"> };</span> <span id="LC36" class="line"></span> <span id="LC37" class="line"> map &lt;int,worker&gt; m;</span> <span id="LC38" class="line"> for(auto i : warr)</span> <span id="LC39" class="line"> {</span> <span id="LC40" class="line"> static int n=0;</span> <span id="LC41" class="line"> m[warr[n].randid]=warr[n].w1;</span> <span id="LC42" class="line"> n++;</span> <span id="LC43" class="line"> }</span> <span id="LC44" class="line"></span> <span id="LC45" class="line"> auto ib=m.begin();</span> <span id="LC46" class="line"> auto ie=m.end();</span> <span id="LC47" class="line"></span> <span id="LC48" class="line"> for(;ib!=ie;ib++)</span> <span id="LC49" class="line"> {</span> <span id="LC50" class="line"> cout&lt;&lt;"\t winfo.randid is \t"&lt;&lt;(*ib).first&lt;&lt;" worker info is \t"&lt;&lt;(*ib).second.id&lt;&lt;"\t"&lt;&lt;(*ib).second.name</span> <span id="LC51" class="line"> &lt;&lt;"\t"&lt;&lt;(*ib).second.work&lt;&lt;"\t"&lt;&lt;endl;</span> <span id="LC52" class="line"> }</span> <span id="LC53" class="line"></span> <span id="LC54" class="line"> return 0;</span> <span id="LC55" class="line">}</span></pre></div></div> <ul> <li> <p>输出</p> <p><img src="/elliot1995/CPP_learn/raw/master/cout2.jpg" alt="输出结果"></p> </li> </ul> <p><a name="0284-pair-%E5%85%B3%E9%94%AE%E5%AD%97-nbsp9wmv-012315"></a></p> <h4> <a id="0284-pair-关键字-9wmv-012315" class="anchor" href="#0284-pair-%E5%85%B3%E9%94%AE%E5%AD%97-9wmv-012315"></a>0.2.8.4. pair 关键字 &nbsp;9.wmv 01:23:15</h4> <ul> <li>用于插入 复杂映射类型</li> <li>multimap set map 每一个节点就是一个 pair</li> <li>用法:</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">m.insert(pair&lt;const char*,int&gt; ("第一个"),1);</span> <span id="LC2" class="line">m.insert(pair&lt;const char*,int&gt; ("第二个"),2);</span> <span id="LC3" class="line">m.insert(pair&lt;const char*,int&gt; ("第三个"),5);</span> <span id="LC4" class="line">m.insert(pair&lt;const char*,int&gt; ("第四个"),8);</span> <span id="LC5" class="line"></span></pre></div></div> <p><a name="0285-hash_set-%E5%92%8Chash_map-nbsp9wmv-013310"></a></p> <h4> <a id="0285-hash_set-和hash_map-9wmv-013310" class="anchor" href="#0285-hash_set-%E5%92%8Chash_map-9wmv-013310"></a>0.2.8.5. hash_set 和hash_map &nbsp;9.wmv 01:33:10</h4> <ul> <li>作用:把比较大的数据抽象得比较小</li> <li>hash_set 不会自动排序 查找时一次就足够了。而set需要 log2n 次</li> <li>适用于精确查找,一次就能找到。比二分查找要快</li> <li>hash_set 的经典用法 判断数据是否相等</li> <li>hash_set、hash_map 不会自动排序</li> <li>hash算法依赖于hash_table</li> </ul> <p><a name="0286-equal_range"></a></p> <h4> <a id="0286-equal_range" class="anchor" href="#0286-equal_range"></a>0.2.8.6. equal_range()</h4> <ul> <li>找到红黑树的链表节点,遍历所有节点</li> <li>first为链表的首节点,second 为最后一个空节点</li> </ul> <p><a name="029-string-%E6%9C%AC%E8%B4%A8%E6%98%AF%E4%B8%80%E4%B8%AA%E5%AE%B9%E5%99%A8"></a></p> <h3> <a id="029-string-本质是一个容器" class="anchor" href="#029-string-%E6%9C%AC%E8%B4%A8%E6%98%AF%E4%B8%80%E4%B8%AA%E5%AE%B9%E5%99%A8"></a>0.2.9. string 本质是一个容器</h3> <ul> <li>C语言中 下列代码是错误的:</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">char str[54];</span> <span id="LC2" class="line">str="123456";</span></pre></div></div> <ul> <li> <p>erase() 函数可删除字符串。 str1.erase(3,4); erase(str1.begin(),str.begin()+2);</p> </li> <li> <p>replace() 替换函数 str1.replace(3,3,"China");str1.replace(3,"China"); str1.replace(3,"China"); replace (位置,长度,字符串);</p> </li> <li> <p>str.find() 查找字符串,找到第一个匹配的字符的位置,找不到返回-1;</p> </li> <li> <p>rfind() 反向查找,返回找的的第一个的匹配的字符串的位置。找不到返回-1;</p> </li> <li> <p>find_first_of () 找到第一个并返回所在位置。</p> </li> <li> <p>find_first_not_of () 找到第一个不匹配的并返回所在位置。</p> </li> <li> <p>find_last_of () 找到最后一个匹配的并返回所在位置。</p> </li> <li> <p>find_last_not_of () 找到最后一个不匹配的并返回所在位置。</p> </li> </ul> <p><a name="02901-%E7%BD%91%E4%B8%8A%E6%89%92%E6%9D%A5%E7%9A%84-string-%E4%BB%8B%E7%BB%8D"></a></p> <h5> <a id="02901-网上扒来的-string-介绍" class="anchor" href="#02901-%E7%BD%91%E4%B8%8A%E6%89%92%E6%9D%A5%E7%9A%84-string-%E4%BB%8B%E7%BB%8D"></a>0.2.9.0.1. 网上扒来的 string 介绍</h5> <ul> <li><a href="/elliot1995/CPP_learn/blob/master/string.md">String</a></li> </ul> <p><a name="0210-gpu%E7%BC%96%E7%A8%8B"></a></p> <h3> <a id="0210-gpu编程" class="anchor" href="#0210-gpu%E7%BC%96%E7%A8%8B"></a>0.2.10. GPU编程</h3> <ul> <li>可以利用模板类实现对容器的操作(利用重载括号的方式) <a name="02101-lamda-%E8%A1%A8%E8%BE%BE%E5%BC%8F-c-amp-%E7%BC%96%E7%A8%8B"></a> </li> </ul> <h4> <a id="02101-lamda-表达式-c-amp-编程" class="anchor" href="#02101-lamda-%E8%A1%A8%E8%BE%BE%E5%BC%8F-c-amp-%E7%BC%96%E7%A8%8B"></a>0.2.10.1. Lamda 表达式 C++ AMP 编程</h4> <ul> <li>函数包装器(LAMDA 表达式,类似JS里面的匿名函数) : 需要头文件 </li> <li>GPU 强项在于并行计算,CPU强项在于单点计算</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">auto func=[](int a ,int b){ return a+b;};</span> <span id="LC2" class="line"></span> <span id="LC3" class="line">for_each(myvector.begin(),myvector.end(),[](int a){return a*=2;cout&lt;&lt;a&lt;&lt;endl;});</span></pre></div></div> <ul> <li>C++ AMP 计算演示 (VS 2017中编译失败)</li> <li>VS2017 中错误信息:</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"></span></pre></div></div> <p>严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C3861 “_Access”: 找不到标识符 GPU_hello c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.15.26726\include\amp.h 2616</p> <p>严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C3588 在 amp 限制代码中不支持从“unknown-type”转换为“void *” GPU_hello c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.15.26726\include\amp.h 2616</p> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;amp.h&gt;</span> <span id="LC2" class="line">#include &lt;iostream&gt;</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">using namespace std;</span> <span id="LC5" class="line">using namespace concurrency;</span> <span id="LC6" class="line"></span> <span id="LC7" class="line">int main(void)</span> <span id="LC8" class="line">{</span> <span id="LC9" class="line"> int a[10] = { 1,2,3,4,5,6,7,8,9,10 };</span> <span id="LC10" class="line"> array_view&lt;int&gt; av(10, a); //GPU 计算结构 AV 存储到GPU的显存</span> <span id="LC11" class="line"> /*</span> <span id="LC12" class="line"> *[=](index&lt;1&gt;idx) restrict (amp) {av[idx] *= 2; }</span> <span id="LC13" class="line"> * [=] 标识直接操作这个数据 </span> <span id="LC14" class="line"> * restrict (amp) 标识定位到 GPU进行运算</span> <span id="LC15" class="line"> *这个表达式为Lambda表达式</span> <span id="LC16" class="line"> */</span> <span id="LC17" class="line"></span> <span id="LC18" class="line"> parallel_for_each(av.extent, [=](index&lt;1&gt; idx) restrict (amp) {av[idx] *= 2; });</span> <span id="LC19" class="line"> </span> <span id="LC20" class="line"> for (int i = 0; i &lt; 10; i++)</span> <span id="LC21" class="line"> {</span> <span id="LC22" class="line"></span> <span id="LC23" class="line"> cout &lt;&lt; "\t" &lt;&lt; av[i] &lt;&lt; endl;</span> <span id="LC24" class="line"> }</span> <span id="LC25" class="line"> </span> <span id="LC26" class="line"></span> <span id="LC27" class="line"></span> <span id="LC28" class="line"> cin.get();</span> <span id="LC29" class="line">}</span></pre></div></div> <p><a name="03-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F"></a></p> <h2> <a id="03-设计模式" class="anchor" href="#03-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F"></a>0.3. 设计模式</h2> <p><a name="031-%E5%A4%87%E5%BF%98%E5%BD%95%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="031-备忘录模式" class="anchor" href="#031-%E5%A4%87%E5%BF%98%E5%BD%95%E6%A8%A1%E5%BC%8F"></a>0.3.1. 备忘录模式</h3> <ul> <li> <p>在一个类内部记录另一个类的快照状态的模式。可以再合适的时候跳回复用</p> </li> <li> <p>设计备忘录的三大步骤:</p> </li> </ul> <ol> <li>设计记录的节点,存储记录 2.设计记录的存储: vector list map set 可以使 链表 图 数组 树 3.操作记录的类,记录节点状态,设置节点状态,显示节点状态</li> </ol> <p><a name="032-%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="032-策略模式" class="anchor" href="#032-%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F"></a>0.3.2. 策略模式</h3> <ul> <li> <p>策略模式针对一组算法,将每一个算法封装到具有共同接口的独立类中。</p> </li> <li> <p>从而使得他们可以相互转换,策略模式可以在不影响客户端的情况下使算法发生改变。策略模式把行为和环境分离开来,环境类负责维持和查询行为类。</p> </li> <li> <p>策略模式依赖多态,策略模式的抽象类,接口,抽象类的指针可以访问所有子类对象(纯虚函数)</p> </li> <li> <p>各种策略的实现类。都必须集成抽象类。</p> </li> <li> <p>策略的设置接口类。设置不同的策略 ======= <a name="033-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a></p> </li> </ul> <h3> <a id="033-设计模式-抽象工厂模式" class="anchor" href="#033-%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a>0.3.3. 设计模式 抽象工厂模式</h3> <ul> <li>工厂模式: 客户类和工厂类分开。</li> <li>消费者需要任何产品,只需要和工厂请求就可以了。</li> <li>消费者无需修改就可以接纳新产品,缺点是当产品修改时,工厂类也要做相应的修改</li> </ul> <p>=======</p> <p><a name="034-%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="034-简单工厂模式" class="anchor" href="#034-%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a>0.3.4. 简单工厂模式</h3> <ul> <li>基类存放数据 派生类存放操作</li> <li>再实现一个调用各个操作的静态类,调用时返回派生类指针</li> </ul> <p>代码: <a href="/elliot1995/CPP_learn/blob/master/simple_factory.cpp">simple_factory</a></p> <p><a name="035-%E6%96%B9%E6%B3%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="035-方法工厂模式" class="anchor" href="#035-%E6%96%B9%E6%B3%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a>0.3.5. 方法工厂模式</h3> <ul> <li>把操作和实例化工厂的类分别抽象出来</li> <li>通过继承抽象类实现不同的操作</li> <li>方法工厂模式就是简单工厂模式把工厂进行抽象并且进行封装后得到的</li> </ul> <p>代码: <a href="/elliot1995/CPP_learn/blob/master/factory_method.cpp">factory_method</a></p> <blockquote> <blockquote> <blockquote> <p>不能在linux 下编译!!! 报错 only nonstatic member functions may be virtual</p> </blockquote> </blockquote> </blockquote> <p><a name="%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="抽象工厂模式" class="anchor" href="#%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"></a>抽象工厂模式</h3> <ul> <li>工厂模式: 客户类和工厂类分开。</li> <li>消费者需要任何产品,只需要和工厂请求就可以了。</li> <li>消费者无需修改就可以接纳新产品,缺点是当产品修改时,工厂类也要做相应的修改</li> <li>消费者 工厂 商品都有自己的抽象类并且通过继承 实例化 抽象接口 ,提供不同的服务</li> </ul>

<p><a name="%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="单例模式" class="anchor" href="#%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F"></a>单例模式</h3> <ul> <li>单例模式确认某个类只有只有一个实例</li> <li>有两种实现模式: 1.匿名类的声明 &nbsp; 2.通过内部的静态类指针来实现</li> <li>单例模式:单例模式确保某一个类只有一个实例,</li> <li>而且自行实例化并向整个系统提供这个实例单例模式</li> <li>单例模式只应在有真正的“单一实例”的需求时才可使用。</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">class {</span> <span id="LC2" class="line"> public:</span> <span id="LC3" class="line"> int a=100;</span> <span id="LC4" class="line"> }aa;</span></pre></div></div> <p><a href="/elliot1995/CPP_learn/blob/master/SingleInstance.cpp">示例</a></p> <p><a name="%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="代理模式" class="anchor" href="#%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F"></a>代理模式</h3> <ul> <li>代理模式:代理模式给某一个对象提供一个代理对象,</li> <li>并由代理对象控制对源对象的引用。</li> <li>代理就是一个人或一个机构代表另一个人或者一个机构采取行动。</li> <li>某些情况下,客户不想或者不能够直接引用一个对象,</li> <li>代理对象可以在客户和目标对象直接起到中介的作用。</li> <li>客户端分辨不出代理主题对象与真实主题对象。</li> <li>代理模式可以并不知道真正的被代理对象,</li> <li>而仅仅持有一个被代理对象的接口,这时候代理对象不能够创建被代理对象,</li> <li>被代理对象必须有系统的其他角色代为创建并传入。</li> </ul> <p><a href="/elliot1995/CPP_learn/blob/master/proxy.cpp">代理模式示例</a></p> <p><a name="%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="迭代器模式" class="anchor" href="#%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%A8%A1%E5%BC%8F"></a>迭代器模式</h3> <ul> <li>迭代子模式:迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。</li> <li>多个对象聚在一起形成的总体称之为聚集,聚集对象是能够包容一组对象的容器对象。</li> </ul> <p><a name="%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="命令行模式" class="anchor" href="#%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%A8%A1%E5%BC%8F"></a>命令行模式</h3> <ul> <li> <p>把执行命令单独建一个类。专职做命令的执行工作</p> </li> <li> <p>命令的执行者专么建一个基类,存放执行不同命令的类继承自这个基类。通过执行不同命令划分。</p> </li> <li> <p>再建一个类统筹这些执行命令的类,调用执行命令的类。</p> </li> <li> <p>代码: <a href="/elliot1995/CPP_learn/blob/master/Command_mode.cpp">命令模式示例代码</a></p> </li> </ul> <p><a name="%E8%B4%A3%E4%BB%BB%E9%93%BE%E6%A8%A1%E5%BC%8F"></a></p> <h3> <a id="责任链模式" class="anchor" href="#%E8%B4%A3%E4%BB%BB%E9%93%BE%E6%A8%A1%E5%BC%8F"></a>责任链模式:</h3> <ul> <li>一级传达另一级,知道没有上级,直到最高级</li> <li><a href="/elliot1995/CPP_learn/blob/master/duty_mode.cpp">责任链模式示例</a></li> </ul> <p><a name="%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95"></a></p> <h2> <a id="数据结构与算法" class="anchor" href="#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95"></a>数据结构与算法</h2> <blockquote> <p>20140903</p> </blockquote> <p><a name="%E6%80%BB%E8%A7%88"></a></p> <h3> <a id="总览" class="anchor" href="#%E6%80%BB%E8%A7%88"></a>总览</h3> <p><img src="/elliot1995/CPP_learn/raw/master/sjjg.jpg" alt=""></p> <p><a name="%E6%A6%82%E8%AE%BA"></a></p> <h3> <a id="概论" class="anchor" href="#%E6%A6%82%E8%AE%BA"></a>概论</h3> <p><img src="/elliot1995/CPP_learn/raw/master/gl.png" alt=""></p> <p><a name="%E7%AE%97%E6%B3%95%E7%9A%84%E7%89%B9%E6%80%A7"></a></p> <h3> <a id="算法的特性" class="anchor" href="#%E7%AE%97%E6%B3%95%E7%9A%84%E7%89%B9%E6%80%A7"></a>算法的特性</h3> <p><img src="/elliot1995/CPP_learn/raw/master/sf.jpg" alt=""></p> <p><a name="%E7%AE%97%E6%B3%95%E7%9A%84%E8%A1%A1%E9%87%8F%E6%A0%87%E5%87%86"></a></p> <h3> <a id="算法的衡量标准" class="anchor" href="#%E7%AE%97%E6%B3%95%E7%9A%84%E8%A1%A1%E9%87%8F%E6%A0%87%E5%87%86"></a>算法的衡量标准</h3> <p><img src="/elliot1995/CPP_learn/raw/master/sfbz.jpg" alt=""></p> <p><a name="boost%E5%92%8C%E7%AE%97%E6%B3%95"></a></p> <h3> <a id="boost和算法" class="anchor" href="#boost%E5%92%8C%E7%AE%97%E6%B3%95"></a>Boost和算法</h3> <p><a name="boost-array-%E7%AC%AC%E4%B8%80%E5%8F%AAboost-%E7%A8%8B%E5%BA%8F"></a></p> <h4> <a id="boost-array-第一只boost-程序" class="anchor" href="#boost-array-%E7%AC%AC%E4%B8%80%E5%8F%AAboost-%E7%A8%8B%E5%BA%8F"></a>boost Array 第一只boost 程序</h4> <ul> <li>使用boost 必须下载安装适当的编译好的包。</li> <li>各个VS 使用的版本不一样</li> <li>Linux只能自己编译</li> <li>boost 的命名空间为boost</li> </ul> <p><a href="/elliot1995/CPP_learn/blob/master/boost_CPP.cpp">boost第一个程序(array)</a></p> <p><a name="boost-%E5%BA%93%E7%9A%84%E5%AD%A6%E4%B9%A0-boost_array_bind_fun_ref"></a></p> <h4> <a id="boost-库的学习-boost_array_bind_fun_ref" class="anchor" href="#boost-%E5%BA%93%E7%9A%84%E5%AD%A6%E4%B9%A0-boost_array_bind_fun_ref"></a>boost 库的学习 boost_array_bind_fun_ref</h4> <p><a name="std-%E6%96%B9%E5%BC%8F"></a></p> <h4> <a id="std-方式" class="anchor" href="#std-%E6%96%B9%E5%BC%8F"></a>std 方式</h4> <ul> <li>绑定已有函数增加新的参数但是不改变原来的函数(std 方式)</li> <li>使用: <ul> <li>1执行操作的类继承自 std::binary_function&lt;&gt;</li> <li>2.使用bind1st()</li> </ul> </li> <li>代码: <ul> <li><a href="/elliot1995/CPP_learn/blob/master/bind1st.cpp">bind1st示例代码</a></li> </ul> </li> </ul> <p><a name="boost%E6%96%B9%E5%BC%8F"></a></p> <h5> <a id="boost方式" class="anchor" href="#boost%E6%96%B9%E5%BC%8F"></a>boost方式</h5> <ul> <li><a href="/elliot1995/CPP_learn/blob/master/boostbind.cpp">boost::bind示例代码</a></li> <li><a href="/elliot1995/CPP_learn/blob/master/boostbind.md">boost详解</a></li> </ul> <p><a name="boostfunction-%E5%BA%93"></a></p> <h4> <a id="boostfunction-库" class="anchor" href="#boostfunction-%E5%BA%93"></a>boost::function 库</h4> <ul> <li>boost::function 库提供了一个类模板 boost::function。它是一个仿函数类,用于封装各种函数指针通常用来和bind结合起来使用。当仿函数没有绑定任何指针时,会抛出 boost::bad_function_call异常。</li> </ul> <p><a name="boostref"></a></p> <h4> <a id="boostref" class="anchor" href="#boostref"></a>boost::ref</h4> <ul> <li>不能拷贝对象用boost::ref()</li> </ul> <p><a name="raii"></a></p> <h4> <a id="raii" class="anchor" href="#raii"></a>RAII</h4> <ul> <li>避免内存泄漏,把堆上的内存当做栈使用</li> </ul> <p><a name="%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-smartpointers-%E5%BA%93"></a></p> <h4> <a id="智能指针-smartpointers-库" class="anchor" href="#%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-smartpointers-%E5%BA%93"></a>智能指针 smartpointers 库</h4> <p><a name="%E7%B1%BB%E7%9A%84%E8%99%9A%E5%87%BD%E6%95%B0%E8%A1%A8"></a></p> <h3> <a id="类的虚函数表" class="anchor" href="#%E7%B1%BB%E7%9A%84%E8%99%9A%E5%87%BD%E6%95%B0%E8%A1%A8"></a>类的虚函数表</h3> <ul> <li>类有一个虚函数表,存储着所有虚函数的地址。</li> <li>类总是把虚函数表放在最前面</li> <li>一种访问类的虚函数的方法(代码如下:)</li> <li>不管基类中是公有,私有,都不影响子类集成虚函数</li> <li>虚函数顺序:基类-》子类</li> <li>多重继承子类会有多个虚函数表,每个虚函数表继承自每个基类的虚函数表</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"></span> <span id="LC2" class="line"> using namespace std;</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">class A</span> <span id="LC5" class="line">{</span> <span id="LC6" class="line">public:</span> <span id="LC7" class="line"></span> <span id="LC8" class="line"></span> <span id="LC9" class="line"> void virtual a() {</span> <span id="LC10" class="line"> </span> <span id="LC11" class="line"> std::cout &lt;&lt; "A --&gt; a" &lt;&lt; endl;</span> <span id="LC12" class="line"> }</span> <span id="LC13" class="line"></span> <span id="LC14" class="line"> void virtual b() {</span> <span id="LC15" class="line"></span> <span id="LC16" class="line"> std::cout &lt;&lt; "A--&gt; b" &lt;&lt; endl;</span> <span id="LC17" class="line"> }</span> <span id="LC18" class="line"></span> <span id="LC19" class="line"> void virtual c() {</span> <span id="LC20" class="line"></span> <span id="LC21" class="line"> std::cout &lt;&lt; "A--&gt; c" &lt;&lt; endl;</span> <span id="LC22" class="line"> }</span> <span id="LC23" class="line">private:</span> <span id="LC24" class="line"></span> <span id="LC25" class="line">};</span> <span id="LC26" class="line"></span> <span id="LC27" class="line"></span> <span id="LC28" class="line">class B :public A</span> <span id="LC29" class="line">{</span> <span id="LC30" class="line">public:</span> <span id="LC31" class="line"> void virtual a()</span> <span id="LC32" class="line"> {</span> <span id="LC33" class="line"> std::cout &lt;&lt; "B--&gt; a" &lt;&lt; endl;</span> <span id="LC34" class="line"> }</span> <span id="LC35" class="line"></span> <span id="LC36" class="line"> void virtual b() {</span> <span id="LC37" class="line"></span> <span id="LC38" class="line"> std::cout &lt;&lt; "B--&gt; b" &lt;&lt; endl;</span> <span id="LC39" class="line"> }</span> <span id="LC40" class="line">};</span> <span id="LC41" class="line"></span> <span id="LC42" class="line"></span> <span id="LC43" class="line">typedef void (*Pfunc) (void);</span> <span id="LC44" class="line"></span> <span id="LC45" class="line">int main(void)</span> <span id="LC46" class="line">{</span> <span id="LC47" class="line"> B b;</span> <span id="LC48" class="line"> cout &lt;&lt; "B is " &lt;&lt;sizeof(b) &lt;&lt; endl;</span> <span id="LC49" class="line"> </span> <span id="LC50" class="line"> Pfunc pfc;</span> <span id="LC51" class="line"> for (int i = 0; i &lt; 3; i++)</span> <span id="LC52" class="line"> {</span> <span id="LC53" class="line"></span> <span id="LC54" class="line"> /* (&amp;b) 取出B的地址 </span> <span id="LC55" class="line"> (int *)(&amp;b) 转换b的地址为int 类型,方便后期访问紧跟着它的内存</span> <span id="LC56" class="line"> *(int *)(&amp;b) 取出B里面的内容(虚函数表的地址)</span> <span id="LC57" class="line"> (int *) *(int *)(&amp;b) 把虚函数表的地址转换为int类型,方便后期访问紧跟着它的内存</span> <span id="LC58" class="line"> (int *) *(int *)(&amp;b) + i 利用地址存储的机制,+1 自动跳过4(8)个字节,访问下一个内存内容,访问存储在虚函数表里面的函数地址</span> <span id="LC59" class="line"> (Pfunc)* 将虚函数表李的函数指针地址转换为 pFunc 类型的函数指针地址,方便调用</span> <span id="LC60" class="line"> pfc(); 调用</span> <span id="LC61" class="line"> </span> <span id="LC62" class="line"></span> <span id="LC63" class="line"> */</span> <span id="LC64" class="line"> pfc = (Pfunc)*((int *) *(int *)(&amp;b) + i);</span> <span id="LC65" class="line"> pfc();</span> <span id="LC66" class="line"> }</span> <span id="LC67" class="line"></span> <span id="LC68" class="line"></span> <span id="LC69" class="line"></span> <span id="LC70" class="line"></span> <span id="LC71" class="line"> cin.get();</span> <span id="LC72" class="line"></span> <span id="LC73" class="line"> return 0;</span> <span id="LC74" class="line">}</span></pre></div></div> <p><a name="%E4%B8%80%E4%B8%AA%E8%87%AA%E5%B7%B1%E5%AE%9E%E7%8E%B0%E7%9A%84vector%E5%8F%AA%E8%83%BD%E5%A4%84%E7%90%86%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE"></a></p> <h3> <a id="一个自己实现的vector只能处理基本类型数据" class="anchor" href="#%E4%B8%80%E4%B8%AA%E8%87%AA%E5%B7%B1%E5%AE%9E%E7%8E%B0%E7%9A%84vector%E5%8F%AA%E8%83%BD%E5%A4%84%E7%90%86%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE"></a>一个自己实现的Vector(只能处理基本类型数据)</h3> <ul> <li>string 类型不行</li> <li>bool char* int double float long long 等基本类型可用</li> <li>使用模板类实现。底层为数组实现。</li> <li>dvector.h</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #define DVECTOR_H</span> <span id="LC2" class="line"> #pragma once</span> <span id="LC3" class="line"> #include &lt;iostream&gt;</span> <span id="LC4" class="line">template &lt;class T&gt;</span> <span id="LC5" class="line">class dvector</span> <span id="LC6" class="line">{</span> <span id="LC7" class="line">public:</span> <span id="LC8" class="line"> dvector();</span> <span id="LC9" class="line"> ~dvector();</span> <span id="LC10" class="line"> bool push_back(T);</span> <span id="LC11" class="line"> void show();</span> <span id="LC12" class="line"></span> <span id="LC13" class="line">public:</span> <span id="LC14" class="line"> T* p;</span> <span id="LC15" class="line"> int len;</span> <span id="LC16" class="line"> int real_len;</span> <span id="LC17" class="line"></span> <span id="LC18" class="line">};</span> <span id="LC19" class="line"></span> <span id="LC20" class="line"> #endif // DVECTOR_H</span></pre></div></div> <ul> <li>dvector.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"></span> <span id="LC2" class="line">template &lt;class T&gt;</span> <span id="LC3" class="line">dvector&lt;T&gt;::dvector()</span> <span id="LC4" class="line">{</span> <span id="LC5" class="line"> len=real_len =0;</span> <span id="LC6" class="line"> p=nullptr;</span> <span id="LC7" class="line">}</span> <span id="LC8" class="line"></span> <span id="LC9" class="line"></span> <span id="LC10" class="line">template&lt;class T&gt;</span> <span id="LC11" class="line">dvector&lt;T&gt;::~dvector()</span> <span id="LC12" class="line">{</span> <span id="LC13" class="line"> if(p!=nullptr)</span> <span id="LC14" class="line"> {</span> <span id="LC15" class="line"> delete []p;</span> <span id="LC16" class="line"> p=nullptr;</span> <span id="LC17" class="line"> }</span> <span id="LC18" class="line">}</span> <span id="LC19" class="line"></span> <span id="LC20" class="line">template &lt;class T&gt;</span> <span id="LC21" class="line">void dvector&lt;T&gt;::show()</span> <span id="LC22" class="line">{</span> <span id="LC23" class="line"> if(p!=nullptr)</span> <span id="LC24" class="line"> {</span> <span id="LC25" class="line"> for(int i=0;i&lt;real_len;i++){</span> <span id="LC26" class="line"> std::cout&lt;&lt;i&lt;&lt;"\t"&lt;&lt;*(p+i)&lt;&lt;std::endl;</span> <span id="LC27" class="line"> }</span> <span id="LC28" class="line"></span> <span id="LC29" class="line"> std::cout&lt;&lt;"length="&lt;&lt;real_len&lt;&lt;std::endl;</span> <span id="LC30" class="line"> }else {</span> <span id="LC31" class="line"></span> <span id="LC32" class="line"> std::cout&lt;&lt;"NNNNNNNNNNNNNNNNNNNNNNNNNNo thing"&lt;&lt;std::endl;</span> <span id="LC33" class="line">}</span> <span id="LC34" class="line">}</span> <span id="LC35" class="line"></span> <span id="LC36" class="line"></span> <span id="LC37" class="line">template&lt;class T&gt;</span> <span id="LC38" class="line">bool dvector&lt;T&gt;::push_back(T t){</span> <span id="LC39" class="line"></span> <span id="LC40" class="line"> if(p==nullptr)</span> <span id="LC41" class="line"> {</span> <span id="LC42" class="line"> //第一个为空说明只有一个元素</span> <span id="LC43" class="line"> p=new T;</span> <span id="LC44" class="line"> *p=t;</span> <span id="LC45" class="line"> real_len=len=1;</span> <span id="LC46" class="line"> }else {</span> <span id="LC47" class="line"></span> <span id="LC48" class="line"> //第一个不为空说明有多个元素,这时候链表就必须重新分配内存</span> <span id="LC49" class="line"> //分配为数组形式</span> <span id="LC50" class="line"> T *ptemp = new T[real_len+1];</span> <span id="LC51" class="line"> for(int i=0;i&lt;real_len;i++)</span> <span id="LC52" class="line"> {</span> <span id="LC53" class="line"> *(ptemp+i)=*(p+i);</span> <span id="LC54" class="line"></span> <span id="LC55" class="line"> }</span> <span id="LC56" class="line"> *(ptemp+real_len)=t;</span> <span id="LC57" class="line"> delete []p;</span> <span id="LC58" class="line"> p=ptemp;</span> <span id="LC59" class="line"> real_len+=1;</span> <span id="LC60" class="line"> len+=1;</span> <span id="LC61" class="line"> }</span> <span id="LC62" class="line"></span> <span id="LC63" class="line"> return true;</span> <span id="LC64" class="line"></span> <span id="LC65" class="line">}</span> <span id="LC66" class="line"></span> <span id="LC67" class="line"></span> <span id="LC68" class="line">int main()</span> <span id="LC69" class="line">{</span> <span id="LC70" class="line"> //测试基本类型</span> <span id="LC71" class="line"> //string 会出错,因为string 不是基本类型,是一种类似 vector 的类模板,其内部的内存操作与基本类型不一样。</span> <span id="LC72" class="line"> //1. int</span> <span id="LC73" class="line"> dvector&lt;int&gt;* dv1=new dvector&lt;int&gt;;</span> <span id="LC74" class="line"> dv1-&gt;push_back(12);</span> <span id="LC75" class="line"> dv1-&gt;push_back(15);</span> <span id="LC76" class="line"> dv1-&gt;push_back(1995);</span> <span id="LC77" class="line"> dv1-&gt;push_back(200);</span> <span id="LC78" class="line"> dv1-&gt;push_back(2);</span> <span id="LC79" class="line"> dv1-&gt;push_back(1);</span> <span id="LC80" class="line"></span> <span id="LC81" class="line"> dv1-&gt;show();</span> <span id="LC82" class="line"></span> <span id="LC83" class="line"> //2. double</span> <span id="LC84" class="line"> dvector&lt;double&gt;* dv2=new dvector&lt;double&gt;;</span> <span id="LC85" class="line"> dv2-&gt;push_back(12.2);</span> <span id="LC86" class="line"> dv2-&gt;push_back(15.3);</span> <span id="LC87" class="line"> dv2-&gt;push_back(1995.0220);</span> <span id="LC88" class="line"> dv2-&gt;push_back(200.1);</span> <span id="LC89" class="line"> dv2-&gt;push_back(2.3);</span> <span id="LC90" class="line"> dv2-&gt;push_back(1.9);</span> <span id="LC91" class="line"></span> <span id="LC92" class="line"> dv2-&gt;show();</span> <span id="LC93" class="line"></span> <span id="LC94" class="line"> //3. char*</span> <span id="LC95" class="line"> dvector&lt;char*&gt;* dv3=new dvector&lt;char*&gt;;</span> <span id="LC96" class="line"> dv3-&gt;push_back("nimei");</span> <span id="LC97" class="line"> dv3-&gt;push_back("de");</span> <span id="LC98" class="line"> dv3-&gt;push_back("垃圾");</span> <span id="LC99" class="line"> dv3-&gt;push_back("95");</span> <span id="LC100" class="line"> dv3-&gt;push_back("lalala");</span> <span id="LC101" class="line"> dv3-&gt;push_back("45");</span> <span id="LC102" class="line"></span> <span id="LC103" class="line">dv3-&gt;show();</span> <span id="LC104" class="line"> return 0;</span> <span id="LC105" class="line">}</span></pre></div></div> <ul> <li>晚上完善 增加了 删 改 查</li> </ul> <blockquote> <p>和尹成老师视频里面写的不一样。。自己写的。反正就是不规范就是了</p> </blockquote> <ul> <li>dvector.h</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #define DVECTOR_H</span> <span id="LC2" class="line"> #pragma once</span> <span id="LC3" class="line"> #include &lt;iostream&gt;</span> <span id="LC4" class="line">template &lt;class T&gt;</span> <span id="LC5" class="line">class dvector</span> <span id="LC6" class="line">{</span> <span id="LC7" class="line">public:</span> <span id="LC8" class="line"> dvector();</span> <span id="LC9" class="line"> ~dvector();</span> <span id="LC10" class="line"> bool push_back(T);</span> <span id="LC11" class="line"> bool del(T);</span> <span id="LC12" class="line"> bool modify(T, T);</span> <span id="LC13" class="line"> int search(T t);</span> <span id="LC14" class="line"> void show();</span> <span id="LC15" class="line"></span> <span id="LC16" class="line">public:</span> <span id="LC17" class="line"> T* p;</span> <span id="LC18" class="line"> int len;</span> <span id="LC19" class="line"> int real_len;</span> <span id="LC20" class="line"></span> <span id="LC21" class="line">};</span> <span id="LC22" class="line"></span> <span id="LC23" class="line"> #endif // DVECTOR_H</span></pre></div></div> <ul> <li>dvector.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"></span> <span id="LC2" class="line">using namespace std;</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">template &lt;class T&gt;</span> <span id="LC5" class="line">dvector&lt;T&gt;::dvector()</span> <span id="LC6" class="line">{</span> <span id="LC7" class="line"> len=real_len =0;</span> <span id="LC8" class="line"> p=nullptr;</span> <span id="LC9" class="line">}</span> <span id="LC10" class="line"></span> <span id="LC11" class="line"></span> <span id="LC12" class="line">template&lt;class T&gt;</span> <span id="LC13" class="line">dvector&lt;T&gt;::~dvector()</span> <span id="LC14" class="line">{</span> <span id="LC15" class="line"> if(p!=nullptr)</span> <span id="LC16" class="line"> {</span> <span id="LC17" class="line"> delete []p;</span> <span id="LC18" class="line"> p=nullptr;</span> <span id="LC19" class="line"> }</span> <span id="LC20" class="line">}</span> <span id="LC21" class="line"></span> <span id="LC22" class="line">//遍历输出</span> <span id="LC23" class="line">template &lt;class T&gt;</span> <span id="LC24" class="line">void dvector&lt;T&gt;::show()</span> <span id="LC25" class="line">{</span> <span id="LC26" class="line"> if(p!=nullptr)</span> <span id="LC27" class="line"> {</span> <span id="LC28" class="line"> for(int i=0;i&lt;real_len;i++){</span> <span id="LC29" class="line"> std::cout&lt;&lt;i&lt;&lt;"\t"&lt;&lt;*(p+i)&lt;&lt;std::endl;</span> <span id="LC30" class="line"> }</span> <span id="LC31" class="line"></span> <span id="LC32" class="line"> std::cout&lt;&lt;"length="&lt;&lt;real_len&lt;&lt;std::endl;</span> <span id="LC33" class="line"> }else {</span> <span id="LC34" class="line"></span> <span id="LC35" class="line"> std::cout&lt;&lt;"NNNNNNNNNNNNNNNNNNNNNNNNNNo thing"&lt;&lt;std::endl;</span> <span id="LC36" class="line">}</span> <span id="LC37" class="line">}</span> <span id="LC38" class="line"></span> <span id="LC39" class="line">//增</span> <span id="LC40" class="line">template&lt;class T&gt;</span> <span id="LC41" class="line">bool dvector&lt;T&gt;::push_back(T t){</span> <span id="LC42" class="line"></span> <span id="LC43" class="line"> if(p==nullptr)</span> <span id="LC44" class="line"> {</span> <span id="LC45" class="line"> //第一个为空说明只有一个元素</span> <span id="LC46" class="line"> p=new T;</span> <span id="LC47" class="line"> *p=t;</span> <span id="LC48" class="line"> real_len=len=1;</span> <span id="LC49" class="line"> }else {</span> <span id="LC50" class="line"></span> <span id="LC51" class="line"> //第一个不为空说明有多个元素,这时候链表就必须重新分配内存</span> <span id="LC52" class="line"> //分配为数组形式</span> <span id="LC53" class="line"> T *ptemp = new T[real_len+1];</span> <span id="LC54" class="line"> for(int i=0;i&lt;real_len;i++)</span> <span id="LC55" class="line"> {</span> <span id="LC56" class="line"> *(ptemp+i)=*(p+i);</span> <span id="LC57" class="line"></span> <span id="LC58" class="line"> }</span> <span id="LC59" class="line"> *(ptemp+real_len)=t;</span> <span id="LC60" class="line"> delete []p;</span> <span id="LC61" class="line"> p=ptemp;</span> <span id="LC62" class="line"> real_len+=1;</span> <span id="LC63" class="line"> len+=1;</span> <span id="LC64" class="line"> }</span> <span id="LC65" class="line"></span> <span id="LC66" class="line"> return true;</span> <span id="LC67" class="line"></span> <span id="LC68" class="line">}</span> <span id="LC69" class="line">//查 返回查找到的序号</span> <span id="LC70" class="line">template &lt;class T&gt;</span> <span id="LC71" class="line">int dvector&lt;T&gt;::search(T t)</span> <span id="LC72" class="line">{</span> <span id="LC73" class="line"> if (p ==nullptr)</span> <span id="LC74" class="line"> {</span> <span id="LC75" class="line"> std::cout &lt;&lt; "The dvector is empty ,abort!" &lt;&lt; std::endl;</span> <span id="LC76" class="line"> return false;</span> <span id="LC77" class="line"> }</span> <span id="LC78" class="line"> else</span> <span id="LC79" class="line"> {</span> <span id="LC80" class="line"> for (size_t i = 0; i &lt; real_len; i++)</span> <span id="LC81" class="line"> {</span> <span id="LC82" class="line"> if (*(p+i) == t)</span> <span id="LC83" class="line"> {</span> <span id="LC84" class="line"> cout &lt;&lt; "FIND " &lt;&lt; t &lt;&lt; "at the position of " &lt;&lt; i&lt;&lt;" " &lt;&lt; endl;</span> <span id="LC85" class="line"> return i;</span> <span id="LC86" class="line"> </span> <span id="LC87" class="line"> }</span> <span id="LC88" class="line"> }</span> <span id="LC89" class="line"></span> <span id="LC90" class="line"> cout &lt;&lt; "no such a thing" &lt;&lt; endl; </span> <span id="LC91" class="line"> return -1;</span> <span id="LC92" class="line"></span> <span id="LC93" class="line"> }</span> <span id="LC94" class="line"></span> <span id="LC95" class="line"></span> <span id="LC96" class="line"> return -1;</span> <span id="LC97" class="line"></span> <span id="LC98" class="line"></span> <span id="LC99" class="line">}</span> <span id="LC100" class="line"></span> <span id="LC101" class="line"> //删</span> <span id="LC102" class="line"> template &lt;class T&gt;</span> <span id="LC103" class="line"> bool dvector&lt;T&gt;::del(T t)</span> <span id="LC104" class="line"> {</span> <span id="LC105" class="line"></span> <span id="LC106" class="line"> if (p == nullptr)</span> <span id="LC107" class="line"> {</span> <span id="LC108" class="line"> std::cout &lt;&lt; "The dvector is alreafy empty ,abort!" &lt;&lt; std::endl;</span> <span id="LC109" class="line"> return false;</span> <span id="LC110" class="line"> }</span> <span id="LC111" class="line"> else</span> <span id="LC112" class="line"> {</span> <span id="LC113" class="line"> for (size_t i = 0; i &lt; real_len; i++)</span> <span id="LC114" class="line"> {</span> <span id="LC115" class="line"> if (*(p + i) == t)</span> <span id="LC116" class="line"> {</span> <span id="LC117" class="line"> cout &lt;&lt; "Deleting" &lt;&lt; endl;</span> <span id="LC118" class="line"> cout &lt;&lt; "FIND " &lt;&lt; t &lt;&lt; "at the position of " &lt;&lt; i &lt;&lt; " " &lt;&lt; endl;</span> <span id="LC119" class="line"> </span> <span id="LC120" class="line"> //如果是在第一个的情况</span> <span id="LC121" class="line"> if (i == 0)</span> <span id="LC122" class="line"> {</span> <span id="LC123" class="line"> for (size_t i = 0; i &lt; real_len; i++)</span> <span id="LC124" class="line"> {</span> <span id="LC125" class="line"> if (real_len == 1) //如果是只剩一个而且刚好第一个是要删除的</span> <span id="LC126" class="line"> { </span> <span id="LC127" class="line"> delete p;</span> <span id="LC128" class="line"> p == nullptr;</span> <span id="LC129" class="line"> real_len -= 1;</span> <span id="LC130" class="line"> return true;</span> <span id="LC131" class="line"> </span> <span id="LC132" class="line"> }</span> <span id="LC133" class="line"> else</span> <span id="LC134" class="line"> { //还有多个且第一个是要删除的</span> <span id="LC135" class="line"> T* tmp = new T[real_len - 1];</span> <span id="LC136" class="line"> for (size_t k = 0; k &lt; real_len-1; k++)</span> <span id="LC137" class="line"> {</span> <span id="LC138" class="line"> *(tmp + k) = *(p + k + 1);</span> <span id="LC139" class="line"> }</span> <span id="LC140" class="line"></span> <span id="LC141" class="line"> </span> <span id="LC142" class="line"> delete[]p;</span> <span id="LC143" class="line"> p = tmp;</span> <span id="LC144" class="line"> real_len -= 1;</span> <span id="LC145" class="line"> return true;</span> <span id="LC146" class="line"> }</span> <span id="LC147" class="line"> </span> <span id="LC148" class="line"> }</span> <span id="LC149" class="line"></span> <span id="LC150" class="line"> }</span> <span id="LC151" class="line"> else if (i== real_len-1) //最后一个匹配要删除的时候</span> <span id="LC152" class="line"> {</span> <span id="LC153" class="line"> p + i == nullptr;</span> <span id="LC154" class="line"> real_len -= 1;</span> <span id="LC155" class="line"> }</span> <span id="LC156" class="line"> else //在中间的情况</span> <span id="LC157" class="line"> {</span> <span id="LC158" class="line"></span> <span id="LC159" class="line"> T* tmp = new T[real_len - 1];</span> <span id="LC160" class="line"> for (size_t k = 0; k &lt; i ; k++)</span> <span id="LC161" class="line"> {</span> <span id="LC162" class="line"> *(tmp + k) = *(p + k);</span> <span id="LC163" class="line"> }</span> <span id="LC164" class="line"> // 利用两个循环刚好跳过下标为i 的值 k 为原来的p 中的值,tmp+k-i 为新数组中的下标</span> <span id="LC165" class="line"> for (size_t k = i+1; k &lt; real_len; k++)</span> <span id="LC166" class="line"> {</span> <span id="LC167" class="line"> *(tmp + k - 1) = *(p + k);</span> <span id="LC168" class="line"></span> <span id="LC169" class="line"> }</span> <span id="LC170" class="line"></span> <span id="LC171" class="line"> </span> <span id="LC172" class="line"> delete[]p;</span> <span id="LC173" class="line"> p = tmp;</span> <span id="LC174" class="line"> real_len -= 1;</span> <span id="LC175" class="line"> return true;</span> <span id="LC176" class="line"></span> <span id="LC177" class="line"> }</span> <span id="LC178" class="line"></span> <span id="LC179" class="line"></span> <span id="LC180" class="line"> }</span> <span id="LC181" class="line"> }</span> <span id="LC182" class="line"></span> <span id="LC183" class="line"> cout &lt;&lt; "no such a thing,Delete failed!" &lt;&lt; endl;</span> <span id="LC184" class="line"> return false;</span> <span id="LC185" class="line"></span> <span id="LC186" class="line"> } </span> <span id="LC187" class="line"></span> <span id="LC188" class="line"> }</span> <span id="LC189" class="line"></span> <span id="LC190" class="line"> //改</span> <span id="LC191" class="line"> template &lt;class T&gt;</span> <span id="LC192" class="line"> bool dvector&lt;T&gt;::modify(T origin, T mo)</span> <span id="LC193" class="line"> {</span> <span id="LC194" class="line"></span> <span id="LC195" class="line"> if (p == nullptr)</span> <span id="LC196" class="line"> {</span> <span id="LC197" class="line"> std::cout &lt;&lt; "The dvector is empty ,abort!" &lt;&lt; std::endl;</span> <span id="LC198" class="line"> return false;</span> <span id="LC199" class="line"> }</span> <span id="LC200" class="line"> else</span> <span id="LC201" class="line"> {</span> <span id="LC202" class="line"> for (size_t i = 0; i &lt; real_len; i++)</span> <span id="LC203" class="line"> {</span> <span id="LC204" class="line"> if (*(p + i) == origin)</span> <span id="LC205" class="line"> {</span> <span id="LC206" class="line"> cout &lt;&lt; "Modifying" &lt;&lt; endl;</span> <span id="LC207" class="line"> cout &lt;&lt; "FIND " &lt;&lt; origin &lt;&lt; "at the position of " &lt;&lt; i &lt;&lt; " " &lt;&lt; endl;</span> <span id="LC208" class="line"> </span> <span id="LC209" class="line"> *(p + i) = mo;</span> <span id="LC210" class="line"></span> <span id="LC211" class="line"> }</span> <span id="LC212" class="line"> }</span> <span id="LC213" class="line"></span> <span id="LC214" class="line"> cout &lt;&lt; "no such a thing ,Modify failed!" &lt;&lt; endl;</span> <span id="LC215" class="line"> return false;</span> <span id="LC216" class="line"></span> <span id="LC217" class="line"> }</span> <span id="LC218" class="line"></span> <span id="LC219" class="line"></span> <span id="LC220" class="line"> return -1;</span> <span id="LC221" class="line"></span> <span id="LC222" class="line"> }</span> <span id="LC223" class="line"></span> <span id="LC224" class="line"></span> <span id="LC225" class="line"></span> <span id="LC226" class="line">int main()</span> <span id="LC227" class="line">{</span> <span id="LC228" class="line"> //测试基本类型</span> <span id="LC229" class="line"> //string 会出错,因为string 不是基本类型,是一种类似 vector 的类模板,其内部的内存操作与基本类型不一样。</span> <span id="LC230" class="line"> //1. int</span> <span id="LC231" class="line"> dvector&lt;int&gt;* dv1=new dvector&lt;int&gt;;</span> <span id="LC232" class="line"> dv1-&gt;push_back(12);</span> <span id="LC233" class="line"> dv1-&gt;push_back(15);</span> <span id="LC234" class="line"> dv1-&gt;push_back(1995);</span> <span id="LC235" class="line"> dv1-&gt;push_back(200);</span> <span id="LC236" class="line"> dv1-&gt;push_back(2);</span> <span id="LC237" class="line"> dv1-&gt;push_back(1);</span> <span id="LC238" class="line"></span> <span id="LC239" class="line"> dv1-&gt;show();</span> <span id="LC240" class="line"></span> <span id="LC241" class="line"> //dv1-&gt;search(200);</span> <span id="LC242" class="line"> //dv1-&gt;search(1000);</span> <span id="LC243" class="line"> dv1-&gt;del(12);</span> <span id="LC244" class="line"> dv1-&gt;show();</span> <span id="LC245" class="line"> dv1-&gt;del(1995);</span> <span id="LC246" class="line"> dv1-&gt;show();</span> <span id="LC247" class="line"> dv1-&gt;del(1);</span> <span id="LC248" class="line"> dv1-&gt;show();</span> <span id="LC249" class="line"></span> <span id="LC250" class="line"> dv1-&gt;modify(15, 33);</span> <span id="LC251" class="line"> dv1-&gt;modify(200, 1);</span> <span id="LC252" class="line"> dv1-&gt;modify(100, 1);</span> <span id="LC253" class="line"> dv1-&gt;show();</span> <span id="LC254" class="line"></span> <span id="LC255" class="line">// cout&lt;&lt;"next............"&lt;&lt;endl&lt;&lt;endl;</span> <span id="LC256" class="line">// //2. double</span> <span id="LC257" class="line">// dvector&lt;double&gt;* dv2=new dvector&lt;double&gt;;</span> <span id="LC258" class="line">// dv2-&gt;push_back(12.2);</span> <span id="LC259" class="line">// dv2-&gt;push_back(15.3);</span> <span id="LC260" class="line">// dv2-&gt;push_back(1995.0220);</span> <span id="LC261" class="line">// dv2-&gt;push_back(200.1);</span> <span id="LC262" class="line">// dv2-&gt;push_back(2.3);</span> <span id="LC263" class="line">// dv2-&gt;push_back(1.9);</span> <span id="LC264" class="line">//</span> <span id="LC265" class="line">// dv2-&gt;show();</span> <span id="LC266" class="line">//</span> <span id="LC267" class="line">// dv2-&gt;search(1.9);</span> <span id="LC268" class="line">//</span> <span id="LC269" class="line">// cout &lt;&lt; "next............" &lt;&lt; endl &lt;&lt; endl;</span> <span id="LC270" class="line">//</span> <span id="LC271" class="line">// //3. char*</span> <span id="LC272" class="line">// dvector&lt;const char*&gt;* dv3=new dvector&lt;const char*&gt;;</span> <span id="LC273" class="line">// dv3-&gt;push_back("nimei");</span> <span id="LC274" class="line">// dv3-&gt;push_back("de");</span> <span id="LC275" class="line">// dv3-&gt;push_back("垃圾");</span> <span id="LC276" class="line">// dv3-&gt;push_back("95");</span> <span id="LC277" class="line">// dv3-&gt;push_back("lalala");</span> <span id="LC278" class="line">// dv3-&gt;push_back("45");</span> <span id="LC279" class="line">//</span> <span id="LC280" class="line">//dv3-&gt;show();</span> <span id="LC281" class="line">//dv3-&gt;search("nimei");</span> <span id="LC282" class="line">//cout &lt;&lt; "next............" &lt;&lt; endl &lt;&lt; endl;</span> <span id="LC283" class="line"></span> <span id="LC284" class="line">std::cin.get();</span> <span id="LC285" class="line"> return 0;</span> <span id="LC286" class="line">}</span></pre></div></div> <p><a name="%E7%BA%A2%E9%BB%91%E6%A0%91"></a></p> <h3> <a id="红黑树" class="anchor" href="#%E7%BA%A2%E9%BB%91%E6%A0%91"></a>红黑树</h3> <ul> <li><a href="/elliot1995/CPP_learn/blob/master/RBTree.md">见红黑树</a></li> </ul> <p><a name="boost-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0"></a></p> <h2> <a id="boost-学习笔记" class="anchor" href="#boost-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0"></a>Boost 学习笔记</h2> <blockquote> <p>书籍:Beyond the C++ Standard Library : An Introduction to Boost &gt; &gt; 注记(笔记)</p> </blockquote> <p><a name="%E6%9C%89%E7%94%A8%E7%9A%84"></a></p> <h3> <a id="有用的" class="anchor" href="#%E6%9C%89%E7%94%A8%E7%9A%84"></a>有用的</h3> <h4> <a id="boost-string_algo库" class="anchor" href="#boost-string_algo%E5%BA%93"></a>boost string_algo库</h4> <ul> <li>头文件 boost/algorithm/string.hpp</li> <li>这是一组与字符串相关的算法。包括很多有用的算法,用于大小写转换,空格清除,字符串分割,查找及替换,等 等。这组算法是目前C++标准库里已有功能的扩展。</li> </ul> <p><a name="smart-pointer"></a></p> <h3> <a id="smart-pointer" class="anchor" href="#smart-pointer"></a>Smart Pointer</h3> <p><a name="%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-shared_ptr-weak_ptr-scoped_ptr--scoped_array--intrusive_ptr"></a></p> <h4> <a id="智能指针-shared_ptr-weak_ptr-scoped_ptr--scoped_array--intrusive_ptr" class="anchor" href="#%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88-shared_ptr-weak_ptr-scoped_ptr--scoped_array--intrusive_ptr"></a>智能指针 :shared_ptr weak_ptr scoped_ptr scoped_array intrusive_ptr,</h4> <p><a name="smart_ptr-%E5%BA%93%E5%A6%82%E4%BD%95%E6%94%B9%E8%BF%9B%E4%BD%A0%E7%9A%84%E7%A8%8B%E5%BA%8F"></a></p> <h5> <a id="smart_ptr-库如何改进你的程序" class="anchor" href="#smart_ptr-%E5%BA%93%E5%A6%82%E4%BD%95%E6%94%B9%E8%BF%9B%E4%BD%A0%E7%9A%84%E7%A8%8B%E5%BA%8F"></a>Smart_ptr 库如何改进你的程序?</h5> <ul> <li>使用 shared_ptr 进行对象的生存期自动管理,使得分享资源所有权变得有效且安全。</li> <li>使用 weak_ptr 可以安全地观测共享资源,避免了悬挂的指针。</li> <li>使用 scoped_ptr 和 scoped_array 限制资源的使用范围,使得代码更易于编写和维护,并有助于写出异常安全的代码。</li> </ul> <p><a name="%E6%9C%89%E4%B8%89%E7%A7%8D%E5%85%B8%E5%9E%8B%E7%9A%84%E6%83%85%E5%86%B5%E9%80%82%E5%90%88%E4%BD%BF%E7%94%A8%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88"></a></p> <h5> <a id="有三种典型的情况适合使用智能指针" class="anchor" href="#%E6%9C%89%E4%B8%89%E7%A7%8D%E5%85%B8%E5%9E%8B%E7%9A%84%E6%83%85%E5%86%B5%E9%80%82%E5%90%88%E4%BD%BF%E7%94%A8%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88"></a>有三种典型的情况适合使用智能指针:</h5> <ul> <li>资源所有权的共享</li> <li>要写异常安全的代码时</li> <li>避免常见的错误,如资源泄漏</li> </ul> <p><a name="boost%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E4%BC%98%E5%8A%BF"></a></p> <h5> <a id="boost智能指针的优势" class="anchor" href="#boost%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E4%BC%98%E5%8A%BF"></a>boost智能指针的优势</h5> <p>虽然 C++ 标准库中提供了 std::auto_ptr, 但是它不能 完全满足我们对智能指针的需求。例如,auto_ptr 不能用作 STL 容器的元素。Boost 的智能指针类填充了标准所留下来的缺口。</p> <p><a name="scoped_ptr"></a></p> <h4> <a id="scoped_ptr" class="anchor" href="#scoped_ptr"></a>scoped_ptr</h4> <ul> <li> <p>它不能转让所有权,scoped_ptr 永远不能被复制或被赋值,scoped_ptr 拥有它所指向的资源的所有权,并永远不会放弃这个所有权</p> </li> <li> <p>成员函数:</p> <ul> <li>explicit scoped_ptr(T* p=0) 构造函数,存储 p的一份拷贝。</li> <li>~scoped_ptr() 删除被指物。</li> <li>void reset(T* p=0); 重置一个 scoped_ptr 就是删除它已保存的指针,如果它有的话,并重新保存 p</li> <li>T* get() const; 返回保存的指针。应该小心地使用get,因为它可以直接操作裸指针。 get通常用于调用那些需要裸指针的函数。</li> <li>operator unspecified_bool_type() const 返回 scoped_ptr 是否为非空。返回值的类型是未指明的,但这个类型可被用于 Boolean 的上下文中。在 if 语 句中最好使用这个类型转换函数,而不要用 get 去测试 scoped_ptr 的有效性 <ul> <li>void swap(scoped_ptr&amp; b) 交换两个 scoped_ptr 的内容。这个函数不会抛出异常。</li> <li>template void swap(scoped_ptr&amp; a,scoped_ptr&amp; b)<br>这个函数提供了交换两个scoped pointer的内容的更好的方法。之所以说它更好,是因为 swap(scoped1, scoped2) 可以更广泛地用于很多指针类型,包括裸指针和第三方的智能指针。[2] scoped1.swap(scoped2) 则 只能用于它的定义所在的智能指针,而不能用于裸指针</li> </ul> </li> </ul> </li> </ul> <p><a name="scoped_ptr-%E7%9A%84%E7%94%A8%E6%B3%95"></a></p> <h5> <a id="scoped_ptr-的用法" class="anchor" href="#scoped_ptr-%E7%9A%84%E7%94%A8%E6%B3%95"></a>scoped_ptr 的用法</h5> <ul> <li>scoped_ptr 的用法与普通的指针没什么区别;最大的差别在于你不必再记得在指针上调用 delete,还有复制是不允许的。典型的指针操作(operator* 和 operator-&gt;)都被重载了,并提供了和裸指针一样的语法。用scoped_ptr 和用裸指针一样快,也没有大小上的增加,因此它们可以广泛使用。使用 boost::scoped_ptr时,包含头文件 "boost/scoped_ptr.hpp". 在声明一个scoped_ptr 时,用被指物的类型来指定类模板的参数。</li> <li>代码展示:</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;iostream&gt;</span> <span id="LC2" class="line">#include "boost/scoped_ptr.hpp"</span> <span id="LC3" class="line">#include &lt;string&gt;</span> <span id="LC4" class="line"></span> <span id="LC5" class="line"></span> <span id="LC6" class="line">using namespace std;</span> <span id="LC7" class="line">using namespace boost;</span> <span id="LC8" class="line"></span> <span id="LC9" class="line">using namespace std;</span> <span id="LC10" class="line"></span> <span id="LC11" class="line">int main(int argc, char ** argv)</span> <span id="LC12" class="line">{</span> <span id="LC13" class="line"></span> <span id="LC14" class="line"> scoped_ptr&lt;string&gt; st(new string("Scoped ptr Inner Text"));</span> <span id="LC15" class="line"></span> <span id="LC16" class="line"> //错误的做法,scoped_ptr 不能复制</span> <span id="LC17" class="line"> //st = new scoped_ptr&lt;string&gt;; </span> <span id="LC18" class="line"></span> <span id="LC19" class="line"></span> <span id="LC20" class="line"> cout &lt;&lt; *st &lt;&lt;endl&lt;&lt;"Is "&lt;&lt;st-&gt;size()&lt;&lt;" length"&lt;&lt;endl;</span> <span id="LC21" class="line"></span> <span id="LC22" class="line"> *st = "文本已经改变!"; //但是scoped指针指向的内容是可以改变的,scoped_ptr 使用与普通指针没有区别!</span> <span id="LC23" class="line"> </span> <span id="LC24" class="line"> cout &lt;&lt; endl &lt;&lt; *st;</span> <span id="LC25" class="line"></span> <span id="LC26" class="line"></span> <span id="LC27" class="line"></span> <span id="LC28" class="line"></span> <span id="LC29" class="line"> cin.get();</span> <span id="LC30" class="line"></span> <span id="LC31" class="line">// 此处 st 指针自动删除!不需要delete</span> <span id="LC32" class="line"> return 0;</span> <span id="LC33" class="line">}</span></pre></div></div> <ul> <li>Tips: <ul> <li>永远不要把 auto_ptr 放入标准库的容器里。如果你试一下,通常你会得到一个编译错误;如 果你没有得到错误,你就麻烦了。 <ul> <li>auto_ptr 转让所有权后(= 赋值)会变成一个空指针。</li> <li>由于 scoped_ptr::get 会返回一个裸指针,所以就有可能对 scoped_ptr 做一些有害的事情,其中有两件是你 尤其要避免的。 <ul> <li>不要删除这个裸指针。因为它会在 scoped_ptr 被销毁时再一次被删除。</li> <li>不要把这个裸指针保存到另一个 scoped_ptr (或其它任何的智能指针)里。因为这样也会两次删除这个指针,每个scoped_ptr 一次。简单地说,尽量少用 get, 除非你要使用那些要求你传送裸指针的遗留代码!</li> <li>如果你使用 scoped_ptr 作为一个类的成员,你就必须手工定义这个类的复制构造函数和赋值操作符。原因是 scoped_ptr 是不能复制的,因此聚集了它的类也变得不能复制了。</li> </ul> </li> </ul> </li> </ul> </li> </ul> <p><a name="scoped_ptr-%E5%92%8C-pimpl-%E7%94%A8%E6%B3%95"></a></p> <h5> <a id="scoped_ptr-和-pimpl-用法" class="anchor" href="#scoped_ptr-%E5%92%8C-pimpl-%E7%94%A8%E6%B3%95"></a>scoped_ptr 和 Pimpl 用法</h5> <ul> <li>Tips: <ul> <li>如果 pimpl 实例可以安全地被多个封装类(在这里是 pimpl_sample)的实例所共享,那 么用 boost::shared_ptr 来管理 pimpl 的生存期才是正确的选择。用 shared_ptr 比用 scoped_ptr 的优势 Beyond the C++ Standard Library : An Introduction to Boost 40 在于,不需要手工去定义复制构造函数和赋值操作符,而且可以定义空的析构函数,shared_ptr 被设计为可以正 确地用于未完成的类</li> </ul> </li> </ul> <p><a name="scoped_ptr-%E4%B8%8E-const-auto_ptr-%E7%9A%84%E5%8C%BA%E5%88%AB"></a></p> <h5> <a id="scoped_ptr-与-const-auto_ptr-的区别" class="anchor" href="#scoped_ptr-%E4%B8%8E-const-auto_ptr-%E7%9A%84%E5%8C%BA%E5%88%AB"></a>scoped_ptr 与 const auto_ptr 的区别</h5> <ul> <li> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">scoped_ptr 可以被 reset, 在需要时可以删除并替换被指物。</span></pre></div></div> </li> <li> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">而对于 const auto_ptr 这是不可能的</span></pre></div></div> </li> </ul> <p><a name="%E6%80%BB%E7%BB%93"></a></p> <h5> <a id="总结" class="anchor" href="#%E6%80%BB%E7%BB%93"></a>总结</h5> <ul> <li>使用裸指针来写异常安全和无错误的代码是很复杂的。使用智能指针来自动地把动态分配对象的生存期限制在一个 明确的范围之内,是解决这种问题的一个有效的方法,并且提高了代码的可读性、可维护性和质量。scoped_ptr 明确地表示被指物不能被共享和转移。正如你所看到的,std::auto_ptr 可以从另一个 auto_ptr 那里窃取被指 物,那怕是无意的,这被认为是 auto_ptr 的最大的缺点。正是这个缺点使得 scoped_ptr 成为 auto_ptr 最 好的补充。当一个动态分配的对象被传送给 scoped_ptr, 它就成为了这个对象的唯一的拥有者。因为 scoped_ptr 几乎总是以自动变量或数据成员来分配的,因此它可以在离开作用域时正确地销毁,从而在执行流由 于返回语句或异常抛出而离开作用域时,总能释放它所管理的内存。</li> <li>在以下情况时使用 scoped_ptr : <ul> <li>在可能有异常抛出的作用域里使用指针</li> <li>函数里有几条控制路径</li> <li>动态分配对象的生存期应被限制于特定的作用域内</li> <li>异常安全非常重要时(始终如此!)</li> </ul> </li> </ul> <p><a name="scoped_array"></a></p> <h4> <a id="scoped_array" class="anchor" href="#scoped_array"></a>scoped_array</h4> <ul> <li>头文件: "boost/scoped_array.hpp" -作用: <ul> <li>防止错误调用 delete 操作符而不是 delete[] 操作符。</li> <li>scoped_array 为数组做了 scoped_ptr 为单个对象的指针所做的事情:它负责释放内存。区 别只在于 scoped_array 是用 delete[] 操作符来做这件事的。</li> </ul> </li> </ul> <p><a name="shared_ptr"></a></p> <h4> <a id="shared_ptr" class="anchor" href="#shared_ptr"></a>shared_ptr</h4> <ul> <li>头文件: "boost/shared_ptr.hpp"</li> <li>分类 <ul> <li>插入式(intrusive)</li> <li>非插入式(non-intrusive)</li> </ul> </li> </ul> <p><a name="%E4%BD%9C%E7%94%A8"></a></p> <h5> <a id="作用" class="anchor" href="#%E4%BD%9C%E7%94%A8"></a>作用</h5> <ul> <li>被管理的类可能拥有一些特性使得它更应该与引用计数智能指针一起使用。例如,它的复制操作很昂贵,或 者它所 代表的有些东西必须被多个实例共享,这些特性都值得去共享所有权。还有一种情形是共享的资源没有一个明确的拥 有者。使用引用计数智能指针可以在需要 访问共享资源的对象之间共享资源的所有权。引用计数智能指针还让你可 以把对象指针存入标准库的容器中而不会有泄漏的风险,特别是在面对异常或要从容器中删 除元素的时候。如果你 把指针放入容器,你就可以获得多态的好处,可以提高性能(如果复制的代价很高的话),还可以通过把相同的对象放 入多个辅助容器来进行 特定的查找。</li> </ul> <p><a name="shared_ptr-%E5%AE%9A%E4%B9%89"></a></p> <h4> <a id="shared_ptr-定义" class="anchor" href="#shared_ptr-%E5%AE%9A%E4%B9%89"></a>Shared_ptr 定义</h4> <ul> <li>可以从一个裸指针、另一个 shared_ptr、一个 std::auto_ptr、或者一个 boost::weak_ptr 构 造。</li> <li>还可以传递第二个参数给 shared_ptr 的构造函数,它被称为删除器(deleter)。删除器稍后会被调用,来处理 共享资源的释放。这对于管理那些不是用 new 分配也不是用 delete 释放的资源时非常有用</li> <li>shared_ptr 被创建后,它就可象普通指针一样使用了,除了一点,它不能被显式地删除。</li> </ul> <p><a name="shared_ptr-%E5%86%85%E9%83%A8%E6%9E%84%E9%80%A0"></a></p> <h4> <a id="shared_ptr-内部构造" class="anchor" href="#shared_ptr-%E5%86%85%E9%83%A8%E6%9E%84%E9%80%A0"></a>Shared_ptr 内部构造</h4> <blockquote> <blockquote> <blockquote> <p>定义</p> </blockquote> </blockquote> </blockquote> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">template&lt;typename T&gt; class shared_ptr {</span> <span id="LC2" class="line">public:</span> <span id="LC3" class="line">template &lt;class Y&gt; explicit shared_ptr(Y* p);</span> <span id="LC4" class="line">template &lt;class Y,class D&gt; shared_ptr(Y* p, D d);</span> <span id="LC5" class="line">shared_ptr(const shared_ptr &amp; r);</span> <span id="LC6" class="line">template &lt;class Y&gt; explicit</span> <span id="LC7" class="line">shared_ptr(const weak_ptr&lt;Y&gt;&amp; r);</span> <span id="LC8" class="line">template &lt;class Y&gt; explicit shared_ptr(std::auto_ptr&lt;Y&gt;&amp; r);</span> <span id="LC9" class="line">shared_ptr&amp; operator=(const shared_ptr&amp; r);</span> <span id="LC10" class="line">void reset();</span> <span id="LC11" class="line">T&amp; operator*() const;</span> <span id="LC12" class="line">T* operator-&gt;() const;</span> <span id="LC13" class="line">T* get() const;</span> <span id="LC14" class="line">bool unique() const;</span> <span id="LC15" class="line">long use_count() const;</span> <span id="LC16" class="line">operator unspecified_bool_type() const; //译注:原文是unspecified-bool-type(),有误</span> <span id="LC17" class="line">void swap(shared_ptr&lt;T&gt;&amp; b);</span> <span id="LC18" class="line">};</span> <span id="LC19" class="line">template &lt;class T,class U&gt;</span> <span id="LC20" class="line">shared_ptr&lt;T&gt; static_pointer_cast(const shared_ptr&lt;U&gt;&amp; r);</span> <span id="LC21" class="line">}</span></pre></div></div> <h5> <a id="成员函数" class="anchor" href="#%E6%88%90%E5%91%98%E5%87%BD%E6%95%B0"></a>成员函数</h5> <ul> <li> <p><code>template &lt;class Y&gt; explicit shared_ptr(Y* p);</code></p> <ul> <li>这个构造函数获得给定指针 p 的所有权。参数 p 必须是指向 Y 的有效指针。构造后引用计数设为1。唯一从这个构 造函数抛出的异常是 std::bad_alloc (仅在一种很罕见的情况下发生,即不能获得引用计数器所需的自由空间)。</li> </ul> </li> <li> <p><code>template &lt;class Y, class D&gt; shared_ptr(Y* p, D d); </code></p> <ul> <li>这个构造函数带有两个参数。第一个是 shared_ptr 将要获得所有权的那个资源,第二个是 shared_ptr 被销毁时 负责释放资源的一个对象,被保存的资源将以 d(p) 的形式传给那个对象。因此 p 的值是否有效取决于 d。如果引 用计数器不能分配成功, shared_ptr 抛出一个类型为 std::bad_alloc 的异常。</li> </ul> </li> <li> <p><code>shared_ptr(const shared_ptr&amp; r);</code></p> <ul> <li>r 中保存的资源被新构造的 shared_ptr 所共享,引用计数加一。这个构造函数不会抛出异常。</li> </ul> </li> <li> <p><code>template &lt;class Y&gt; explicit shared_ptr(const weak_ptr&lt;Y&gt;&amp; r);</code></p> <ul> <li>从一个 weak_ptr (本章稍后会介绍)构造 shared_ptr。这使得 weak_ptr 的使用具有线程安全性,因为指向 weak_ptr 参数的共享资源的引用计数将会自增(weak_ptr 不影响共享资源的引用计数)。如果 weak_ptr 为空 (r. use_count()==0), shared_ptr 抛出一个类型为 bad_weak_ptr 的异常</li> </ul> </li> <li> <p><code>template &lt;typename Y&gt; shared_ptr(std::auto_ptr&lt;Y&gt;&amp; r);</code></p> <ul> <li>这个构造函数从一个 auto_ptr 获取 r 中保存的指针的所有权,方法是保存指针的一份拷贝并对 auto_ptr 调用 release 。构造后的引用计数为1。而 r 当然就变为空的。如果引用计数器不能分配成功,则抛出 std:: bad_alloc 。</li> </ul> </li> <li> <p><code>~shared_ptr();</code></p> <ul> <li>shared_ptr 析构函数对引用计数减一。如果计数为零,则保存的指针被删除。删除指针的方法是调用 operator delete 或者,如果给定了一个执行删除操作的客户化删除器对象,就把保存的指针作为唯一参数调用这个对象。析 构函数不会抛出异常。</li> </ul> </li> <li> <p><code>shared_ptr&amp; operator=(const shared_ptr&amp; r);</code></p> <ul> <li>赋值操作共享 r 中的资源,并停止对原有资源的共享。赋值操作不会抛出异常。</li> </ul> </li> <li> <p><code>void reset();</code></p> <ul> <li>reset 函数用于停止对保存指针的所有权的共享。共享资源的引用计数减一。</li> </ul> </li> <li> <p><code>T&amp; operator*() const;</code></p> <ul> <li>这个操作符返回对已存指针所指向的对象的一个引用。如果指针为空,调用 operator* 会导致未定义行为。这个操 作符不会抛出异常</li> </ul> </li> <li> <p><code>T* operator-&gt;() const;</code></p> <ul> <li>这个操作符返回保存的指针。这个操作符与 operator* 一起使得智能指针看起来象普通指针。这个操作符不会抛出 异常</li> </ul> </li> <li> <p><code>T* get() const; </code></p> <ul> <li>get 函数是当保存的指针有可能为空时(这时 operator* 和 operator-&gt; 都会导致未定义行为)获取它的最好办法。 注意,你也可以使用隐式布尔类型转换来测试 shared_ptr 是否包含有效指针。这个函数不会抛出异常。</li> </ul> </li> <li> <p><code>bool unique() const;</code></p> <ul> <li>这个函数在 shared_ptr 是它所保存指针的唯一拥有者时返回 true;否则返回 false。 unique 不会抛出异常。</li> </ul> </li> <li> <p><code>long use_count() const;</code></p> <ul> <li>use_count 函数返回指针的引用计数。它在调试的时候特别有用,因为它可以在程序执行的关键点获得引用计数的 快照。小心地使用它,因为在某些可能的 shared_ptr 实现中,计算引用计数可能是昂贵的,甚至是不行的。这个 函数不会抛出异常。</li> </ul> </li> <li> <p><code>operator unspecified_bool_type() const;</code></p> <ul> <li>这是个到 unspecified_bool_type 类型的隐式转换函数,它可以在Boolean上下文中测试一个智能指针。如果 shared_ptr 保存着一个有效的指针,返回值为 True;否则为 false。</li> </ul> </li> <li> <p><code>void swap(shared_ptr&lt;T&gt;&amp; b);</code></p> <ul> <li>这可以很方便地交换两个 shared_ptr。 swap 函数交换保存的指针(以及它们的引用计数)。这个函数不会抛出异 常</li> </ul> </li> </ul> <h4> <a id="普通函数" class="anchor" href="#%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0"></a>普通函数</h4> <ul> <li> <code>template &lt;typename T,typename U&gt; shared_ptr&lt;T&gt; static_pointer_cast(const shared_ptr&lt;U&gt;&amp; r);</code> <ul> <li>要对保存在 shared_ptr 里的指针执行 static_cast,我们可以取出指针然后强制转换它,但我们不能把它存到另 一个 shared_ptr 里;新的 shared_ptr 会认为它是第一个管理这些资源的。解决的方法是用 static_pointer_cast. 使用这个函数可以确保被指物的引用计数保持正确。 static_pointer_cast 不会抛出异 常。</li> </ul> </li> </ul> <h4> <a id="使用方法" class="anchor" href="#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95"></a>使用方法</h4> <ul> <li>详见最后的代码展示:</li> </ul> <h4> <a id="shared_ptr-与其它资源" class="anchor" href="#shared_ptr-%E4%B8%8E%E5%85%B6%E5%AE%83%E8%B5%84%E6%BA%90"></a>shared_ptr 与其它资源</h4> <ul> <li> <p>有时你会发现你要把 shared_ptr 用于某个特别的类型,它需要其它清除操作而不是简单的 delete. shared_ptr 可以通过客户化删除器来支持这种需要。那些处理象 FILE* 这样的操作系统句柄的资源通常要使用 象 fclose 这样的操作来释放。要在 shared_ptr 里使用 FILE* ,我们要定义一个类来负责释放相应的资源。</p> </li> <li> <p>注意</p> <ul> <li>在访问资源时,我们需要对 shared_ptr 使用 &amp;* 用法, get, 或 get_pointer 。 (请注意最好使用 &amp;*. 另两 个选择不太清晰)</li> </ul> </li> </ul> <h4> <a id="从this创建shared_ptr" class="anchor" href="#%E4%BB%8Ethis%E5%88%9B%E5%BB%BAshared_ptr"></a>从this创建shared_ptr</h4> <ul> <li>有时候,需要从 this 获得 shared_ptr ,即是说,你希望你的类被 shared_ptr 所管理,你需要把“自身”转换 为 shared_ptr 的方法。看起来不可能?好的,解决方案来自于我们即将讨论的另一个智能指针 boost:: weak_ptr. weak_ptr 是 shared_ptr 的一个观察者;它只是安静地坐着并看着它们,但不会影响引用计数。通过存 储一个指向 this 的 weak_ptr 作为类的成员,就可以在需要的时候获得一个指向 this 的 shared_ptr 。为了你 可以不必编写代码来保存一个指向 this 的 weak_ptr ,接着又从 weak_ptr 获 shared_ptr 得, Boost. Smart_ptr 为这个任务提供了一个助手类,称为 enable_shared_from_this. 只要简单地让你的类公有地派生自 enable_shared_from_this ,然后在需要访问管理 this 的 shared_ptr 时,使用函数 shared_from_this 就 行了。</li> </ul> <h4> <a id="总结-1" class="anchor" href="#%E6%80%BB%E7%BB%93-1"></a>总结</h4> <ul> <li>引用计数智能指针是非常重要的工具。 Boost 的 shared_ptr 提供了坚固而灵活的解决方案,它已被广泛用于多种 环境下。需要在使用者之间共享对象是常见的,而且通常没有办法通知使用者何时删除对象是安全的。 shared_ptr 让使用者无需知道也在使用共享对象的其它对象,并让它们 无需担心在没有对象引用时的资源释放。 这对于Boost的智能指针类而言是最重要的。你会看到 Boost.Smart_ptr 中还有其它的智能指针,但这 一个肯定 是你最想要的。通过使用定制删除器,几乎所有资源类型都可以存入 shared_ptr 。这使得 shared_ptr 成为处理 资源管理的通用类,而不仅仅是处理动态分配对象。与裸指针相比, shared_ptr 会有一点点额外的空间代价。我 还没有发现由于这些代价太大而需要另外寻找一个解决方案的情形。不要去创建你自己的引用计数智能指针类。没有 比使用 shared_ptr 智能指针更好的了。</li> </ul> <h4> <a id="使用shared_ptr-的时机" class="anchor" href="#%E4%BD%BF%E7%94%A8shared_ptr-%E7%9A%84%E6%97%B6%E6%9C%BA"></a>使用shared_ptr 的时机</h4> <ul> <li>当有多个使用者使用同一个对象,而没有一个明显的拥有者时</li> <li>当要把指针存入标准库容器时</li> <li>当要传送对象到库或从库获取对象,而没有明确的所有权时</li> <li>当管理一些需要特殊清除方式的资源时 <ul> <li>通过定制删除器的帮助。</li> </ul> </li> </ul> <h3> <a id="shared_array" class="anchor" href="#shared_array"></a>shared_array</h3> <ul> <li>头文件: "boost/shared_array.hpp"</li> <li> <blockquote> <blockquote> <p>ared_array 用于共享数组所有权的智能指针。它与 shared_ptr 的关系就如 scoped_array 与 scoped_ptr 的关系。 shared_array 与 shared_ptr 的不同之处主要在于它是用于数组的而不是用于单个对象 的。在我们讨论 scoped_array时,我提到过通常 std::vector 是一个更好的选择。但 shared_array 比 vector 更有价值,因为它提供了对数组所有权的共享。 shared_array 的接口与 shared_ptr 非常相似,差别 仅在于增加了一个下标操作符,以及不支持定制删除器。</p> </blockquote> </blockquote> </li> </ul> <h3> <a id="instrusive_ptr" class="anchor" href="#instrusive_ptr"></a>instrusive_ptr</h3> <ul> <li>头文件: "boost/intrusive_ptr.hpp"</li> </ul> <blockquote> <blockquote> <ul> <li>intrusive_ptr 是 shared_ptr 的插入式版本。有时我们必须使用插入式的引用计数智能指针。典型的情况是对 于那些已经写好了内部引用计数器的代码,而我们又没有时间去重写它(或者已经不能获得那些代码了)。另一种情 况是要求智能指针的大小必须与裸指针大小严格相等,或者 shared_ptr 的引用计数器分配严重影响了程序的性 能(我可以肯定这是非常罕见的情况! )。从功能的观点来看,唯一需要插入式智能指针的情况是,被指类的某个成 员函数需要返回 this ,以便它可以用于另一个智能指针(事实上,也有办法使用非插入式智能指针来解决这个问 题,正如我们在本章前面看到的)。 intrusive_ptr 不同于其它智能指针,因为它要求你来提供它所要的引用计 数器。 当 intrusive_ptr 递增或递减一个非空指针上的引用计数时,它是通过分别调用函数 intrusive_ptr_add_ref 和 intrusive_ptr_release 来完成的。这两个函数负责确保引用计数的正确性,并且负责在引用计数降为零时 删除指针。因此,你必须为你的类重载这两个函数,正如我们后面将看到的。</li> </ul> </blockquote> </blockquote> <ul> <li>部分代码</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">template&lt;class T&gt; class intrusive_ptr {</span> <span id="LC2" class="line">public:</span> <span id="LC3" class="line">intrusive_ptr(T* p,bool add_ref=true);</span> <span id="LC4" class="line">intrusive_ptr(const intrusive_ptr&amp; r);</span> <span id="LC5" class="line">~intrusive_ptr();</span> <span id="LC6" class="line">T&amp; operator*() const;</span> <span id="LC7" class="line">T* operator-&gt;() const;</span> <span id="LC8" class="line">T* get() const;</span> <span id="LC9" class="line">operator unspecified-bool-type() const;</span> <span id="LC10" class="line">};</span> <span id="LC11" class="line">template &lt;class T&gt; T* get_pointer(const intrusive_ptr&lt;T&gt;&amp; p);</span> <span id="LC12" class="line">template &lt;class T,class U&gt; intrusive_ptr&lt;T&gt;</span> <span id="LC13" class="line">static_pointer_cast(const intrusive_ptr&lt;U&gt;&amp; r);</span> <span id="LC14" class="line">}</span></pre></div></div> <p><a name="%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"></a></p> <h3> <a id="代码示例" class="anchor" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"></a>代码示例</h3> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;iostream&gt;</span> <span id="LC2" class="line">#include "boost/scoped_ptr.hpp"</span> <span id="LC3" class="line">#include &lt;string&gt;</span> <span id="LC4" class="line">#include "boost/algorithm/string.hpp" //String_algo 算法</span> <span id="LC5" class="line">#include "boost/shared_ptr.hpp"</span> <span id="LC6" class="line">//#include "boost/container/vector.hpp"</span> <span id="LC7" class="line">#include &lt;vector&gt;</span> <span id="LC8" class="line">#include &lt;fstream&gt;</span> <span id="LC9" class="line"></span> <span id="LC10" class="line">#define SCOPED_PTR 0</span> <span id="LC11" class="line">#define SHARED_SIMPLE 0</span> <span id="LC12" class="line">#define SHARED_FILE 1</span> <span id="LC13" class="line"></span> <span id="LC14" class="line">using namespace boost;</span> <span id="LC15" class="line">using std::cin;</span> <span id="LC16" class="line">using std::cout;</span> <span id="LC17" class="line">using std::endl;</span> <span id="LC18" class="line"></span> <span id="LC19" class="line"></span> <span id="LC20" class="line">class filecloser</span> <span id="LC21" class="line">{</span> <span id="LC22" class="line">public:</span> <span id="LC23" class="line"> filecloser();</span> <span id="LC24" class="line"> ~filecloser();</span> <span id="LC25" class="line"></span> <span id="LC26" class="line"> void operator()(std::fstream *fs)</span> <span id="LC27" class="line"> {</span> <span id="LC28" class="line"> cout &lt;&lt; "Now starting to close the file resourse" &lt;&lt; endl;</span> <span id="LC29" class="line"> if (fs)</span> <span id="LC30" class="line"> {</span> <span id="LC31" class="line"> fs-&gt;close();</span> <span id="LC32" class="line"> cout &lt;&lt; "File Closed" &lt;&lt; endl;</span> <span id="LC33" class="line"> }</span> <span id="LC34" class="line"> </span> <span id="LC35" class="line"> }</span> <span id="LC36" class="line"></span> <span id="LC37" class="line">private:</span> <span id="LC38" class="line"></span> <span id="LC39" class="line">};</span> <span id="LC40" class="line"></span> <span id="LC41" class="line">filecloser::filecloser()</span> <span id="LC42" class="line">{</span> <span id="LC43" class="line">}</span> <span id="LC44" class="line"></span> <span id="LC45" class="line">filecloser::~filecloser()</span> <span id="LC46" class="line">{</span> <span id="LC47" class="line">}</span> <span id="LC48" class="line"></span> <span id="LC49" class="line">//#define shared_ptr boost::shared_ptr;</span> <span id="LC50" class="line"></span> <span id="LC51" class="line">class A {</span> <span id="LC52" class="line"></span> <span id="LC53" class="line">public:</span> <span id="LC54" class="line"> virtual void sing() = 0;</span> <span id="LC55" class="line">protected:</span> <span id="LC56" class="line"></span> <span id="LC57" class="line"> virtual ~A()</span> <span id="LC58" class="line"> {</span> <span id="LC59" class="line"></span> <span id="LC60" class="line"> } </span> <span id="LC61" class="line">};</span> <span id="LC62" class="line"></span> <span id="LC63" class="line"></span> <span id="LC64" class="line">class B : public A</span> <span id="LC65" class="line">{</span> <span id="LC66" class="line">public:</span> <span id="LC67" class="line"> void sing()</span> <span id="LC68" class="line"> {</span> <span id="LC69" class="line"> cout &lt;&lt; "A B C D E \n\n" &lt;&lt; endl;</span> <span id="LC70" class="line"> };</span> <span id="LC71" class="line"></span> <span id="LC72" class="line"> </span> <span id="LC73" class="line"></span> <span id="LC74" class="line"></span> <span id="LC75" class="line">};</span> <span id="LC76" class="line"></span> <span id="LC77" class="line"></span> <span id="LC78" class="line">static boost::shared_ptr&lt;A&gt; createA()</span> <span id="LC79" class="line">{</span> <span id="LC80" class="line"> boost::shared_ptr&lt;B&gt; p(new B());</span> <span id="LC81" class="line"></span> <span id="LC82" class="line"> return p;</span> <span id="LC83" class="line"></span> <span id="LC84" class="line">}</span> <span id="LC85" class="line"></span> <span id="LC86" class="line">typedef std::vector&lt;boost::shared_ptr&lt;A&gt;&gt; sv;</span> <span id="LC87" class="line">typedef boost::shared_ptr&lt;B&gt; SB;</span> <span id="LC88" class="line">typedef sv::iterator sbi;</span> <span id="LC89" class="line"></span> <span id="LC90" class="line"></span> <span id="LC91" class="line">//using namespace std;</span> <span id="LC92" class="line">using namespace boost;</span> <span id="LC93" class="line"></span> <span id="LC94" class="line">using namespace std;</span> <span id="LC95" class="line"></span> <span id="LC96" class="line">int main(int argc, char ** argv)</span> <span id="LC97" class="line">{</span> <span id="LC98" class="line"></span> <span id="LC99" class="line">#if SCOPED_PTR</span> <span id="LC100" class="line"></span> <span id="LC101" class="line"></span> <span id="LC102" class="line"> scoped_ptr&lt;string&gt; st(new string("Scoped ptr Inner Text"));</span> <span id="LC103" class="line"></span> <span id="LC104" class="line"> //错误的做法,scoped_ptr 不能复制</span> <span id="LC105" class="line"> //st = new scoped_ptr&lt;string&gt;; </span> <span id="LC106" class="line"></span> <span id="LC107" class="line"></span> <span id="LC108" class="line"> cout &lt;&lt; *st &lt;&lt;endl&lt;&lt;"Is "&lt;&lt;st-&gt;size()&lt;&lt;" length"&lt;&lt;endl;</span> <span id="LC109" class="line"></span> <span id="LC110" class="line"> *st = "文本已经改变!"; //但是scoped指针指向的内容是可以改变的,scoped_ptr 使用与普通指针没有区别!</span> <span id="LC111" class="line"> </span> <span id="LC112" class="line"> cout &lt;&lt; endl &lt;&lt; *st;</span> <span id="LC113" class="line"></span> <span id="LC114" class="line">#endif // SCOPED_PTR</span> <span id="LC115" class="line"></span> <span id="LC116" class="line"></span> <span id="LC117" class="line"></span> <span id="LC118" class="line">#if SHARED_SIMPLE</span> <span id="LC119" class="line"></span> <span id="LC120" class="line"> sv shared_v;</span> <span id="LC121" class="line"></span> <span id="LC122" class="line"> for (int i = 0; i &lt; 100; i++)</span> <span id="LC123" class="line"> {</span> <span id="LC124" class="line"></span> <span id="LC125" class="line"> shared_v.push_back(createA());</span> <span id="LC126" class="line"> }</span> <span id="LC127" class="line"></span> <span id="LC128" class="line"> cout &lt;&lt; "下面取出并打印:\n"; </span> <span id="LC129" class="line"></span> <span id="LC130" class="line"> for (auto a: shared_v)</span> <span id="LC131" class="line"> {</span> <span id="LC132" class="line"></span> <span id="LC133" class="line"> a-&gt;sing();</span> <span id="LC134" class="line"> }</span> <span id="LC135" class="line"></span> <span id="LC136" class="line">#endif</span> <span id="LC137" class="line"></span> <span id="LC138" class="line"></span> <span id="LC139" class="line">#if SHARED_FILE</span> <span id="LC140" class="line"></span> <span id="LC141" class="line"> fstream fs("1.txt",ios::out|ios::out);</span> <span id="LC142" class="line"> if (!fs.bad())</span> <span id="LC143" class="line"> {</span> <span id="LC144" class="line"> boost::shared_ptr&lt;std::fstream&gt; file(&amp;fs,filecloser());</span> <span id="LC145" class="line"> file-&gt;write("文件内读写!\n",60); </span> <span id="LC146" class="line"> }</span> <span id="LC147" class="line"> </span> <span id="LC148" class="line"></span> <span id="LC149" class="line"> </span> <span id="LC150" class="line"></span> <span id="LC151" class="line">#endif</span> <span id="LC152" class="line"></span> <span id="LC153" class="line"></span> <span id="LC154" class="line"> cin.get();</span> <span id="LC155" class="line"></span> <span id="LC156" class="line"> return 0;</span> <span id="LC157" class="line">}</span></pre></div></div> <p><a name="qt-%E5%AD%A6%E4%B9%A0"></a></p> <h2> <a id="qt-学习" class="anchor" href="#qt-%E5%AD%A6%E4%B9%A0"></a>QT 学习</h2> <ul> <li> <p>.pro文件为工程文件</p> </li> <li> <p>.pro.user 特定环境的编译的工程文件</p> </li> <li> <p>hello World 代码</p> </li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;QWidget&gt;</span> <span id="LC2" class="line">//窗口类</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">#include &lt;QPushButton&gt;</span> <span id="LC5" class="line">//按钮</span> <span id="LC6" class="line"></span> <span id="LC7" class="line">int main (int argc,char** argv)</span> <span id="LC8" class="line">{</span> <span id="LC9" class="line"></span> <span id="LC10" class="line"> QApplication app(argc,argv);</span> <span id="LC11" class="line"> QWidget w;</span> <span id="LC12" class="line">// w为窗口对象</span> <span id="LC13" class="line"> QPushButton bt;</span> <span id="LC14" class="line"> bt.setText("点击我啊啊啊啊");</span> <span id="LC15" class="line">// 按钮也是个窗口 ,控件都是窗口</span> <span id="LC16" class="line"> bt.setParent(&amp;w);</span> <span id="LC17" class="line">// 窗口对象的父子关系影响着显示位置</span> <span id="LC18" class="line">// 没有父窗口的窗口称之为主窗口</span> <span id="LC19" class="line"> QObject::connect(&amp;bt,SIGNAL(clicked()),&amp;w,SLOT(close()));</span> <span id="LC20" class="line">// QT 对C++的扩展 ,和C++ std::bind 和 std::function 有相似之处</span> <span id="LC21" class="line"> w.show();</span> <span id="LC22" class="line">// bt.show();</span> <span id="LC23" class="line">// 显示窗口</span> <span id="LC24" class="line"> w.setWindowTitle("Hello ,My first QT Program!");</span> <span id="LC25" class="line"></span> <span id="LC26" class="line"> return app.exec();</span> <span id="LC27" class="line"></span> <span id="LC28" class="line">}</span></pre></div></div> <ul> <li>LineEdit 示例</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;QApplication&gt;</span> <span id="LC2" class="line">//应用程序抽象类</span> <span id="LC3" class="line">#include &lt;QWidget&gt;</span> <span id="LC4" class="line">//窗口类</span> <span id="LC5" class="line">#include &lt;QLineEdit&gt;</span> <span id="LC6" class="line">#include &lt;QPushButton&gt;</span> <span id="LC7" class="line">//按钮</span> <span id="LC8" class="line"></span> <span id="LC9" class="line">int main(int argc,char** argv)</span> <span id="LC10" class="line">{</span> <span id="LC11" class="line"></span> <span id="LC12" class="line"> QApplication app(argc,argv);</span> <span id="LC13" class="line"> QWidget w;</span> <span id="LC14" class="line"> QLineEdit ql;</span> <span id="LC15" class="line"> ql.setEchoMode(QLineEdit::Password);</span> <span id="LC16" class="line"> ql.setParent(&amp;w);</span> <span id="LC17" class="line"> ql.setPlaceholderText("请输入密码!");</span> <span id="LC18" class="line">// QCompleter cp1(QStringList()&lt;&lt;"aaa"&lt;&lt;"bbb"&lt;&lt;"2333");</span> <span id="LC19" class="line">// ql.setCompleter(&amp;cp1);</span> <span id="LC20" class="line"> w.show();</span> <span id="LC21" class="line">// 显示窗口</span> <span id="LC22" class="line"> w.setWindowTitle("Hello ,My first QT Program!");</span> <span id="LC23" class="line"></span> <span id="LC24" class="line"> return app.exec();</span> <span id="LC25" class="line">}</span> <span id="LC26" class="line"></span></pre></div></div> <p><a name="011-%E5%9D%90%E6%A0%87%E7%B3%BB%E7%BB%9F"></a></p> <h3> <a id="011-坐标系统" class="anchor" href="#011-%E5%9D%90%E6%A0%87%E7%B3%BB%E7%BB%9F"></a>0.1.1. 坐标系统</h3> <ul> <li>setGeometry(const &amp;rect) 函数 或者 setGeometry(x,y,w,h);</li> </ul> <p><a name="012-layouts"></a></p> <h3> <a id="012-layouts" class="anchor" href="#012-layouts"></a>0.1.2. layouts</h3> <ul> <li>QVBoxlayout</li> <li>QHBoxlayout</li> <li>GridLayout</li> <li>代码示例</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include &lt;QApplication&gt;</span> <span id="LC2" class="line">//应用程序抽象类</span> <span id="LC3" class="line">#include &lt;QWidget&gt;</span> <span id="LC4" class="line">//窗口类</span> <span id="LC5" class="line">#include &lt;QLineEdit&gt;</span> <span id="LC6" class="line">#include &lt;QPushButton&gt;</span> <span id="LC7" class="line">//按钮</span> <span id="LC8" class="line">//#include &lt;QBoxLayout&gt;</span> <span id="LC9" class="line">#include &lt;QVBoxLayout&gt;</span> <span id="LC10" class="line">#include &lt;QHBoxLayout&gt;</span> <span id="LC11" class="line">#include &lt;QGridLayout&gt;</span> <span id="LC12" class="line"></span> <span id="LC13" class="line">int main(int argc,char** argv)</span> <span id="LC14" class="line">{</span> <span id="LC15" class="line"></span> <span id="LC16" class="line"> QApplication app(argc,argv);</span> <span id="LC17" class="line"> QWidget w;</span> <span id="LC18" class="line"> QLineEdit ql;</span> <span id="LC19" class="line"> ql.setEchoMode(QLineEdit::Password);</span> <span id="LC20" class="line"> ql.setParent(&amp;w);</span> <span id="LC21" class="line"> ql.setPlaceholderText("请输入密码!");</span> <span id="LC22" class="line">// ql.setGeometry(100,100,100,50);</span> <span id="LC23" class="line"></span> <span id="LC24" class="line"> QPushButton qp;</span> <span id="LC25" class="line"> qp.setText("nimei");</span> <span id="LC26" class="line"> qp.setParent(&amp;w);</span> <span id="LC27" class="line"></span> <span id="LC28" class="line">#if 0</span> <span id="LC29" class="line"> QVBoxLayout qbb;</span> <span id="LC30" class="line">// QHBoxLayout qbb;</span> <span id="LC31" class="line"> qbb.stretch(1);</span> <span id="LC32" class="line"> qbb.addWidget(&amp;ql);</span> <span id="LC33" class="line"> qbb.addSpacing(50);</span> <span id="LC34" class="line"> qbb.addWidget(&amp;qp);</span> <span id="LC35" class="line"> qbb.stretch(1);</span> <span id="LC36" class="line"></span> <span id="LC37" class="line">#endif</span> <span id="LC38" class="line"></span> <span id="LC39" class="line"> QGridLayout qbb;</span> <span id="LC40" class="line"> qbb.addWidget(&amp;ql,1,1);</span> <span id="LC41" class="line"> qbb.addWidget(&amp;qp,1,2);</span> <span id="LC42" class="line"> qbb.addWidget(new QPushButton,2,1);</span> <span id="LC43" class="line"> qbb.addWidget(new QPushButton,2,2);</span> <span id="LC44" class="line"> qbb.addWidget(new QPushButton,3,1);</span> <span id="LC45" class="line"> qbb.addWidget(new QPushButton,3,2);</span> <span id="LC46" class="line"></span> <span id="LC47" class="line"> qbb.setColumnStretch(0,1);</span> <span id="LC48" class="line"> qbb.setColumnStretch(3,1);</span> <span id="LC49" class="line"> qbb.setRowStretch(0,1);</span> <span id="LC50" class="line"> qbb.setRowStretch(4,1);</span> <span id="LC51" class="line"></span> <span id="LC52" class="line">// QCompleter cp1(QStringList()&lt;&lt;"aaa"&lt;&lt;"bbb"&lt;&lt;"2333");</span> <span id="LC53" class="line">// ql.setCompleter(&amp;cp1);</span> <span id="LC54" class="line"> w.setLayout(&amp;qbb);</span> <span id="LC55" class="line">// 设置layout</span> <span id="LC56" class="line"> w.show();</span> <span id="LC57" class="line">// 显示窗口</span> <span id="LC58" class="line"> w.setWindowTitle("Hello ,My first QT Program!");</span> <span id="LC59" class="line"></span> <span id="LC60" class="line"></span> <span id="LC61" class="line"> return app.exec();</span> <span id="LC62" class="line">}</span> <span id="LC63" class="line"></span> <span id="LC64" class="line"></span></pre></div></div> <p><a name="013-%E6%8E%A7%E4%BB%B6"></a></p> <h3> <a id="013-控件" class="anchor" href="#013-%E6%8E%A7%E4%BB%B6"></a>0.1.3. 控件</h3> <p><a name="014-%E6%B6%88%E6%81%AF%E5%9F%BA%E7%A1%80"></a></p> <h3> <a id="014-消息基础" class="anchor" href="#014-%E6%B6%88%E6%81%AF%E5%9F%BA%E7%A1%80"></a>0.1.4. 消息基础</h3> <ul> <li>代码</li> <li>dwin.hpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#ifndef DWIN_H</span> <span id="LC2" class="line"> #define DWIN_H</span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QWidget&gt;</span> <span id="LC5" class="line"> #include &lt;QPushButton&gt;</span> <span id="LC6" class="line"></span> <span id="LC7" class="line">class DWin : public QWidget</span> <span id="LC8" class="line">{</span> <span id="LC9" class="line"> Q_OBJECT</span> <span id="LC10" class="line">public:</span> <span id="LC11" class="line"> explicit DWin(QWidget *parent = nullptr);</span> <span id="LC12" class="line"> bool event(QEvent *event);</span> <span id="LC13" class="line"> void mousePressEvent(QMouseEvent *event);</span> <span id="LC14" class="line"> void mouseReleaseEvent(QMouseEvent *event);</span> <span id="LC15" class="line"> void keyPressEvent(QKeyEvent *event);</span> <span id="LC16" class="line"> void keyReleaseEvent(QKeyEvent *event);</span> <span id="LC17" class="line"> void mouseMoveEvent(QMouseEvent *event);</span> <span id="LC18" class="line"> void closeEvent(QCloseEvent *event);</span> <span id="LC19" class="line"> void showEvent(QShowEvent *event);</span> <span id="LC20" class="line"> void hideEvent(QHideEvent *event);</span> <span id="LC21" class="line"> QPushButton *pb;</span> <span id="LC22" class="line">signals:</span> <span id="LC23" class="line"></span> <span id="LC24" class="line">public slots:</span> <span id="LC25" class="line">};</span> <span id="LC26" class="line"></span> <span id="LC27" class="line">#endif // DWIN_H</span></pre></div></div> <ul> <li>dwin.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #include &lt;iostream&gt;</span> <span id="LC2" class="line"> #include &lt;QApplication&gt;</span> <span id="LC3" class="line"> #include &lt;QEvent&gt;</span> <span id="LC4" class="line"> #include &lt;QDebug&gt;</span> <span id="LC5" class="line"> #include &lt;QMouseEvent&gt;</span> <span id="LC6" class="line"> #include &lt;QKeyEvent&gt;</span> <span id="LC7" class="line"></span> <span id="LC8" class="line">DWin::DWin(QWidget *parent) : QWidget(parent)</span> <span id="LC9" class="line">{</span> <span id="LC10" class="line"> this-&gt;setMouseTracking(true);</span> <span id="LC11" class="line"> pb=new QPushButton("OK",this);</span> <span id="LC12" class="line"> pb-&gt;setDefault(true);</span> <span id="LC13" class="line"> //Lamda 表达式。。。。</span> <span id="LC14" class="line"> connect(pb,&amp;QPushButton::clicked,[](){</span> <span id="LC15" class="line"> qDebug()&lt;&lt;"OK Pushed ............";</span> <span id="LC16" class="line"> });</span> <span id="LC17" class="line"></span> <span id="LC18" class="line">}</span> <span id="LC19" class="line">/*</span> <span id="LC20" class="line"> * QAppkication 先得到消息 -》具体负责每个窗口的event()获取-》具体负责每项消息处理的虚函数</span> <span id="LC21" class="line"> *我们可以做的:</span> <span id="LC22" class="line"> * 1》可以重载具体的虚函数来实现自己的功能</span> <span id="LC23" class="line"> * 2》可以重载event() 函数用来处理或者截取消息</span> <span id="LC24" class="line"> */</span> <span id="LC25" class="line"></span> <span id="LC26" class="line">// event 管理所有的消息,用来截断所有消息 return ture ; 就能截取消息</span> <span id="LC27" class="line"></span> <span id="LC28" class="line">bool DWin::event(QEvent *event)</span> <span id="LC29" class="line">{</span> <span id="LC30" class="line"></span> <span id="LC31" class="line">return QWidget::event(event);</span> <span id="LC32" class="line">}</span> <span id="LC33" class="line"></span> <span id="LC34" class="line">void DWin::mouseReleaseEvent(QMouseEvent *event)</span> <span id="LC35" class="line">{</span> <span id="LC36" class="line"> qDebug()&lt;&lt;"Your mouse have been release!";</span> <span id="LC37" class="line">}</span> <span id="LC38" class="line"></span> <span id="LC39" class="line"></span> <span id="LC40" class="line"></span> <span id="LC41" class="line">void DWin::mousePressEvent(QMouseEvent *event)</span> <span id="LC42" class="line">{</span> <span id="LC43" class="line"> QPoint pt=event-&gt;pos();</span> <span id="LC44" class="line"> qDebug()&lt;&lt;pt;</span> <span id="LC45" class="line"> if (event-&gt;button() == Qt::LeftButton)</span> <span id="LC46" class="line"> {</span> <span id="LC47" class="line"> qDebug()&lt;&lt;"Left button have been pressed!";</span> <span id="LC48" class="line"> }</span> <span id="LC49" class="line"></span> <span id="LC50" class="line"> if(event-&gt;modifiers() == Qt::ShiftModifier)</span> <span id="LC51" class="line"> {</span> <span id="LC52" class="line"> qDebug()&lt;&lt;"Shift Pressed LOL...";</span> <span id="LC53" class="line"> }</span> <span id="LC54" class="line">}</span> <span id="LC55" class="line"></span> <span id="LC56" class="line"></span> <span id="LC57" class="line">void DWin::keyPressEvent(QKeyEvent *event)</span> <span id="LC58" class="line">{</span> <span id="LC59" class="line"> //返回大写的字母ASCII代码!</span> <span id="LC60" class="line"> auto mod=event-&gt;modifiers();</span> <span id="LC61" class="line"> auto key=event-&gt;key();</span> <span id="LC62" class="line"> qDebug()&lt;&lt;(char)key&lt;&lt;" "&lt;&lt;mod;</span> <span id="LC63" class="line">}</span> <span id="LC64" class="line">void DWin::keyReleaseEvent(QKeyEvent *event)</span> <span id="LC65" class="line">{</span> <span id="LC66" class="line"></span> <span id="LC67" class="line">}</span> <span id="LC68" class="line">void DWin::mouseMoveEvent(QMouseEvent *event)</span> <span id="LC69" class="line">{</span> <span id="LC70" class="line"> static long i;</span> <span id="LC71" class="line"></span> <span id="LC72" class="line"> //默认的是鼠标按下后移动</span> <span id="LC73" class="line"> //设置窗口类 setMouseTracking(true) 后可实现鼠标不按下就接受移动消息!</span> <span id="LC74" class="line"> qDebug()&lt;&lt;"mouse Moved"&lt;&lt;i++;</span> <span id="LC75" class="line">}</span> <span id="LC76" class="line"></span> <span id="LC77" class="line">void DWin::closeEvent(QCloseEvent *event)</span> <span id="LC78" class="line">{</span> <span id="LC79" class="line"> qDebug()&lt;&lt;"Closed Window";</span> <span id="LC80" class="line">}</span> <span id="LC81" class="line">void DWin::showEvent(QShowEvent *event)</span> <span id="LC82" class="line">{</span> <span id="LC83" class="line"> qDebug()&lt;&lt;"showing Window";</span> <span id="LC84" class="line">}</span> <span id="LC85" class="line">void DWin::hideEvent(QHideEvent *event)</span> <span id="LC86" class="line">{</span> <span id="LC87" class="line"> qDebug()&lt;&lt;"hidding Window";</span> <span id="LC88" class="line"></span> <span id="LC89" class="line">}</span> <span id="LC90" class="line"></span> <span id="LC91" class="line">int main (int argc ,char ** argv)</span> <span id="LC92" class="line">{</span> <span id="LC93" class="line"> QApplication app(argc,argv);</span> <span id="LC94" class="line"></span> <span id="LC95" class="line"> DWin w;</span> <span id="LC96" class="line"> w.show();</span> <span id="LC97" class="line"></span> <span id="LC98" class="line"> return app.exec();</span> <span id="LC99" class="line">}</span></pre></div></div> <p><a name="015-qt-%E6%B6%88%E6%81%AF%E4%B8%AD%E7%BA%A7"></a></p> <h3> <a id="015-qt-消息中级" class="anchor" href="#015-qt-%E6%B6%88%E6%81%AF%E4%B8%AD%E7%BA%A7"></a>0.1.5. QT 消息中级</h3> <p><a name="0151-eventfilter-%E6%88%AA%E5%8F%96%E6%B6%88%E6%81%AF"></a></p> <h4> <a id="0151-eventfilter-截取消息" class="anchor" href="#0151-eventfilter-%E6%88%AA%E5%8F%96%E6%B6%88%E6%81%AF"></a>0.1.5.1. eventFilter 截取消息</h4> <ul> <li> <p>首先需要安装EventFilter .经过这个控件的消息都必须经过这个过滤器</p> <ul> <li>pb-&gt;installEventFilter(this);</li> <li>安装处理特定消息后必须将 消息返回 QWidget 。</li> </ul> </li> <li> <p>代码示例:</p> <ul> <li>dWidget.h</li> </ul> </li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"></span> <span id="LC2" class="line"> #ifndef DWIDGET_H</span> <span id="LC3" class="line"> #define DWIDGET_H</span> <span id="LC4" class="line"></span> <span id="LC5" class="line"> #include &lt;QWidget&gt;</span> <span id="LC6" class="line"></span> <span id="LC7" class="line">class dWidget : public QWidget</span> <span id="LC8" class="line">{</span> <span id="LC9" class="line"> Q_OBJECT</span> <span id="LC10" class="line">public:</span> <span id="LC11" class="line"> explicit dWidget(QWidget *parent = nullptr);</span> <span id="LC12" class="line"></span> <span id="LC13" class="line"> bool eventFilter(QObject *watched, QEvent *event);</span> <span id="LC14" class="line"></span> <span id="LC15" class="line"> QObject* _ob;</span> <span id="LC16" class="line">signals:</span> <span id="LC17" class="line"></span> <span id="LC18" class="line">public slots:</span> <span id="LC19" class="line">};</span> <span id="LC20" class="line"> # endif // DWIDGET_H</span></pre></div></div> <ul> <li>dWidget.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #include &lt;QApplication&gt;</span> <span id="LC2" class="line"> #include &lt;QEvent&gt;</span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QPushButton&gt;</span> <span id="LC5" class="line"> #include &lt;QDebug&gt;</span> <span id="LC6" class="line"></span> <span id="LC7" class="line"> #include "dwidget.h"</span> <span id="LC8" class="line"></span> <span id="LC9" class="line">dWidget::dWidget(QWidget *parent) : QWidget(parent)</span> <span id="LC10" class="line">{</span> <span id="LC11" class="line"></span> <span id="LC12" class="line"> QPushButton* pb=new QPushButton("lalal");</span> <span id="LC13" class="line"> pb-&gt;setParent(this);</span> <span id="LC14" class="line"> connect(pb,SIGNAL(clicked(bool)),this,SLOT(close()));</span> <span id="LC15" class="line"></span> <span id="LC16" class="line"> _ob=pb;</span> <span id="LC17" class="line"> pb-&gt;installEventFilter(this);</span> <span id="LC18" class="line">}</span> <span id="LC19" class="line"></span> <span id="LC20" class="line">bool dWidget::eventFilter(QObject *watched, QEvent *event){</span> <span id="LC21" class="line"></span> <span id="LC22" class="line"> if( watched== (QObject*)_ob &amp;&amp; (event-&gt;type()==QEvent::MouseButtonPress</span> <span id="LC23" class="line"> || event-&gt;type()==QEvent::MouseButtonDblClick))</span> <span id="LC24" class="line"> {</span> <span id="LC25" class="line"> qDebug()&lt;&lt;"Msg have been catched!\n an Never exit!!......";</span> <span id="LC26" class="line"> return true;</span> <span id="LC27" class="line"></span> <span id="LC28" class="line"> }</span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> return QWidget::eventFilter(watched,event); //必须返回给Qwidget</span> <span id="LC31" class="line">}</span> <span id="LC32" class="line"></span> <span id="LC33" class="line">int main (int argc,char ** argv)</span> <span id="LC34" class="line">{</span> <span id="LC35" class="line"> QApplication ap(argc,argv);</span> <span id="LC36" class="line"> dWidget* dd=new dWidget();</span> <span id="LC37" class="line"> dd-&gt;show();</span> <span id="LC38" class="line"> return ap.exec();</span> <span id="LC39" class="line"></span> <span id="LC40" class="line">}</span> <span id="LC41" class="line"></span></pre></div></div> <p><a name="1052-notify-%E9%80%9A%E7%9F%A5%E6%B6%88%E6%81%AF"></a></p> <h4> <a id="1052-notify-通知消息" class="anchor" href="#1052-notify-%E9%80%9A%E7%9F%A5%E6%B6%88%E6%81%AF"></a>1.0.5.2. notify 通知消息</h4> <ul> <li>notify 函数属于 QApplication .需要重载QApplciation 实现自己的通知消息</li> <li>示例代码见最后</li> </ul> <p><a name="1053-postevent-%E5%92%8C-sendevent"></a></p> <h4> <a id="1053-postevent-和-sendevent" class="anchor" href="#1053-postevent-%E5%92%8C-sendevent"></a>1.0.5.3. postEvent 和 sendEvent</h4> <ul> <li> <p>ap.postEvent(dd,new QEvent(QEvent::User)); //postEvent 加入消息队里等待处理</p> </li> <li> <p>ap.sendEvent(dd,new QEvent(QEvent::User)); //发送给消息队列并立即处理</p> </li> <li> <p>示例代码见最后</p> </li> <li> <p>qApp 是全局指针。可以全局调用</p> <ul> <li>app.h</li> </ul> </li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #ifndef APP_H</span> <span id="LC2" class="line"> #define APP_H</span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QApplication&gt;</span> <span id="LC5" class="line"> #include &lt;QDebug&gt;</span> <span id="LC6" class="line">class APP : public QApplication</span> <span id="LC7" class="line">{</span> <span id="LC8" class="line"> Q_OBJECT</span> <span id="LC9" class="line">public:</span> <span id="LC10" class="line"> APP(int argc,char** argv):QApplication(argc,argv)</span> <span id="LC11" class="line"> {</span> <span id="LC12" class="line"></span> <span id="LC13" class="line"> }</span> <span id="LC14" class="line"></span> <span id="LC15" class="line"> QWidget* topW;</span> <span id="LC16" class="line"> bool notify(QObject *, QEvent *);</span> <span id="LC17" class="line">};</span> <span id="LC18" class="line"></span> <span id="LC19" class="line">#endif // APP_H</span> <span id="LC20" class="line"></span></pre></div></div> <ul> <li>app.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #include "app.h"</span> <span id="LC2" class="line"></span> <span id="LC3" class="line">bool APP::notify(QObject *ob, QEvent *ev)</span> <span id="LC4" class="line">{</span> <span id="LC5" class="line"> if(this-&gt;topLevelWidgets().count()&gt;0)</span> <span id="LC6" class="line"> {</span> <span id="LC7" class="line"> topW=this-&gt;topLevelWidgets().at(0);</span> <span id="LC8" class="line"> if(ob==(QObject*)topW &amp;&amp; ev-&gt;type() ==QEvent::MouseButtonPress)</span> <span id="LC9" class="line"> {</span> <span id="LC10" class="line"></span> <span id="LC11" class="line"> qDebug()&lt;&lt;"main window is clicked";</span> <span id="LC12" class="line"> }</span> <span id="LC13" class="line"> }</span> <span id="LC14" class="line"></span> <span id="LC15" class="line"></span> <span id="LC16" class="line"> return QApplication::notify(ob,ev);</span> <span id="LC17" class="line">}</span> <span id="LC18" class="line"></span></pre></div></div> <ul> <li>dwidget.h</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #ifndef DWIDGET_H</span> <span id="LC2" class="line"> #define DWIDGET_H</span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QWidget&gt;</span> <span id="LC5" class="line"></span> <span id="LC6" class="line">class dWidget : public QWidget</span> <span id="LC7" class="line">{</span> <span id="LC8" class="line"> Q_OBJECT</span> <span id="LC9" class="line">public:</span> <span id="LC10" class="line"> explicit dWidget(QWidget *parent = nullptr);</span> <span id="LC11" class="line"></span> <span id="LC12" class="line"> bool eventFilter(QObject *watched, QEvent *event);</span> <span id="LC13" class="line"></span> <span id="LC14" class="line"> QObject* _ob;</span> <span id="LC15" class="line"></span> <span id="LC16" class="line">bool event(QEvent *event);</span> <span id="LC17" class="line">signals:</span> <span id="LC18" class="line"></span> <span id="LC19" class="line">public slots:</span> <span id="LC20" class="line">};</span> <span id="LC21" class="line"></span> <span id="LC22" class="line">#endif // DWIDGET_H</span> <span id="LC23" class="line"></span></pre></div></div> <ul> <li>dwidget.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">//#include &lt;QApplication&gt;</span> <span id="LC2" class="line"> #include &lt;QEvent&gt;</span> <span id="LC3" class="line"> #include &lt;QPushButton&gt;</span> <span id="LC4" class="line"> #include &lt;QDebug&gt;</span> <span id="LC5" class="line"> #include "dwidget.h"</span> <span id="LC6" class="line"> #include "app.cpp"</span> <span id="LC7" class="line"></span> <span id="LC8" class="line">dWidget::dWidget(QWidget *parent) : QWidget(parent)</span> <span id="LC9" class="line">{</span> <span id="LC10" class="line"></span> <span id="LC11" class="line"> QPushButton* pb=new QPushButton("lalal");</span> <span id="LC12" class="line"> pb-&gt;setParent(this);</span> <span id="LC13" class="line"> connect(pb,SIGNAL(clicked(bool)),this,SLOT(close()));</span> <span id="LC14" class="line"></span> <span id="LC15" class="line"> _ob=pb;</span> <span id="LC16" class="line"> pb-&gt;installEventFilter(this);</span> <span id="LC17" class="line">}</span> <span id="LC18" class="line"></span> <span id="LC19" class="line">bool dWidget::eventFilter(QObject *watched, QEvent *event){</span> <span id="LC20" class="line"></span> <span id="LC21" class="line"> if( watched== (QObject*)_ob &amp;&amp; (event-&gt;type()==QEvent::MouseButtonPress</span> <span id="LC22" class="line"> || event-&gt;type()==QEvent::MouseButtonDblClick))</span> <span id="LC23" class="line"> {</span> <span id="LC24" class="line"> qDebug()&lt;&lt;"Msg have been catched!\n an Never exit!!......";</span> <span id="LC25" class="line"> return true;</span> <span id="LC26" class="line"></span> <span id="LC27" class="line"> }</span> <span id="LC28" class="line"></span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> return QWidget::eventFilter(watched,event);</span> <span id="LC31" class="line">}</span> <span id="LC32" class="line"></span> <span id="LC33" class="line"></span> <span id="LC34" class="line">bool dWidget::event(QEvent *event)</span> <span id="LC35" class="line">{</span> <span id="LC36" class="line"> if(event-&gt;type()==QEvent::User)</span> <span id="LC37" class="line"> {</span> <span id="LC38" class="line"> qDebug()&lt;&lt;"User Event is comming";</span> <span id="LC39" class="line"> }</span> <span id="LC40" class="line"></span> <span id="LC41" class="line"> return QWidget::event(event);</span> <span id="LC42" class="line">}</span> <span id="LC43" class="line">int main (int argc,char ** argv)</span> <span id="LC44" class="line">{</span> <span id="LC45" class="line"> APP ap(argc,argv);</span> <span id="LC46" class="line"> dWidget* dd=new dWidget();</span> <span id="LC47" class="line"> dd-&gt;show();</span> <span id="LC48" class="line"> //发送一个Event 给 dWidget</span> <span id="LC49" class="line"> ap.postEvent(dd,new QEvent(QEvent::User)); //postEvent 加入消息队里等待处理</span> <span id="LC50" class="line"> ap.sendEvent(dd,new QEvent(QEvent::User)); //发送给消息队列并立即处理</span> <span id="LC51" class="line"></span> <span id="LC52" class="line"> return ap.exec();</span> <span id="LC53" class="line"></span> <span id="LC54" class="line">}</span> <span id="LC55" class="line"></span></pre></div></div> <p><a name="106-qpainter-%E5%92%8C%E9%87%8D%E5%86%99%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B6"></a></p> <h3> <a id="106-qpainter-和重写自定义控件" class="anchor" href="#106-qpainter-%E5%92%8C%E9%87%8D%E5%86%99%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A7%E4%BB%B6"></a>1.0.6. QPainter 和重写自定义控件</h3> <ul> <li>头文件 </li> <li>常用功能: drawLine drawRect drawText 等 ,可通过translate transform 变换</li> </ul> <p><a name="1061-qpainter-%E7%9A%84%E7%BB%98%E5%9B%BE%E6%95%88%E7%8E%87%E5%BC%95%E7%94%A8"></a></p> <h4> <a id="1061-qpainter-的绘图效率引用" class="anchor" href="#1061-qpainter-%E7%9A%84%E7%BB%98%E5%9B%BE%E6%95%88%E7%8E%87%E5%BC%95%E7%94%A8"></a>1.0.6.1. QPainter 的绘图效率(引用)</h4> <ul> <li> <p>通过定义 PaintDevice 然后在 PainDevice 中画,然后再显示</p> </li> <li> <p>可以通过 Qpainter 实现 一个自定义的画图工具</p> </li> <li> <p>代码如下:</p> <ul> <li>ww.h</li> </ul> </li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"></span> <span id="LC2" class="line"> #ifndef WW_H</span> <span id="LC3" class="line"> #define WW_H</span> <span id="LC4" class="line"> #include &lt;QApplication&gt;</span> <span id="LC5" class="line"> #include &lt;QPainter&gt;</span> <span id="LC6" class="line"> #include &lt;QEvent&gt;</span> <span id="LC7" class="line"> #include &lt;QWidget&gt;</span> <span id="LC8" class="line"> #include &lt;QMouseEvent&gt;</span> <span id="LC9" class="line"></span> <span id="LC10" class="line">class ww : public QWidget</span> <span id="LC11" class="line">{</span> <span id="LC12" class="line"> Q_OBJECT</span> <span id="LC13" class="line">public:</span> <span id="LC14" class="line"> explicit ww(QWidget *parent = nullptr);</span> <span id="LC15" class="line"></span> <span id="LC16" class="line"></span> <span id="LC17" class="line">// void Pain()</span> <span id="LC18" class="line">// {</span> <span id="LC19" class="line">// p-&gt;drawLine(QPoint(0,0),QPoint(0,100));</span> <span id="LC20" class="line">// p-&gt;drawRect(QRect(QPoint(50,50),QPoint(200,100)));</span> <span id="LC21" class="line">// }</span> <span id="LC22" class="line"></span> <span id="LC23" class="line"> void mouseMoveEvent(QMouseEvent *event);</span> <span id="LC24" class="line"> void mousePressEvent(QMouseEvent *event);</span> <span id="LC25" class="line"> void mouseReleaseEvent(QMouseEvent *event);</span> <span id="LC26" class="line"></span> <span id="LC27" class="line"> QVector&lt;QVector&lt;QPoint&gt;&gt;_lines; //用Qvector 记录连起来的点是line ,——lines 则是这些线的集合</span> <span id="LC28" class="line"></span> <span id="LC29" class="line"> void paintEvent(QPaintEvent*);</span> <span id="LC30" class="line">signals:</span> <span id="LC31" class="line"></span> <span id="LC32" class="line">public slots:</span> <span id="LC33" class="line">};</span> <span id="LC34" class="line"></span> <span id="LC35" class="line">#endif // WW_H</span> <span id="LC36" class="line"></span></pre></div></div> <ul> <li>ww.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include "ww.h"</span> <span id="LC2" class="line"></span> <span id="LC3" class="line">ww::ww(QWidget *parent) : QWidget(parent)</span> <span id="LC4" class="line">{</span> <span id="LC5" class="line">// QPaintDevice* qd=new QPaintDevice();</span> <span id="LC6" class="line"></span> <span id="LC7" class="line"></span> <span id="LC8" class="line">}</span> <span id="LC9" class="line"></span> <span id="LC10" class="line">void ww::paintEvent(QPaintEvent* e)</span> <span id="LC11" class="line">{</span> <span id="LC12" class="line"> QPixmap* map=new QPixmap(size());</span> <span id="LC13" class="line"> map-&gt;fill(Qt::gray);</span> <span id="LC14" class="line"> //QPainter* p=new QPainter(this);</span> <span id="LC15" class="line"> QPainter* p=new QPainter(map);</span> <span id="LC16" class="line"> p-&gt;setBackground(QBrush(QColor(Qt::gray)));</span> <span id="LC17" class="line"> p-&gt;setBrush(Qt::red);</span> <span id="LC18" class="line"> p-&gt;setPen(QPen(Qt::green));</span> <span id="LC19" class="line"> p-&gt;translate(50,50);</span> <span id="LC20" class="line"> QTransform* trans=new QTransform;</span> <span id="LC21" class="line"> trans-&gt;rotate(30);</span> <span id="LC22" class="line"> trans-&gt;scale(0.3,0.3);</span> <span id="LC23" class="line"> p-&gt;setTransform(*trans);</span> <span id="LC24" class="line"></span> <span id="LC25" class="line"> p-&gt;drawLine(QPoint(0,0),QPoint(100,100));</span> <span id="LC26" class="line"> p-&gt;drawRect(QRect(QPoint(50,50),QPoint(200,100)));</span> <span id="LC27" class="line"> p-&gt;drawText(QPoint(100,100),"Hello 你好");</span> <span id="LC28" class="line"> p-&gt;drawPixmap(QPoint(200,200),QPixmap("11.png"));</span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> p-&gt;end(); //在 PixMAp 绘画结束</span> <span id="LC31" class="line"></span> <span id="LC32" class="line"></span> <span id="LC33" class="line"> p-&gt;begin(this); //重新开始在屏幕上画</span> <span id="LC34" class="line"> p-&gt;drawPixmap(QPoint(0,0),*map); //重新在窗口中显示图片</span> <span id="LC35" class="line"></span> <span id="LC36" class="line"> p-&gt;setBrush(Qt::black);</span> <span id="LC37" class="line"> p-&gt;setPen(QPen(Qt::black));</span> <span id="LC38" class="line"></span> <span id="LC39" class="line"> // 下面为定义 划线的函数</span> <span id="LC40" class="line"></span> <span id="LC41" class="line"> for(int x=0;x&lt;_lines.size();x++)</span> <span id="LC42" class="line"> {</span> <span id="LC43" class="line"> const QVector&lt;QPoint&gt;&amp; line = _lines.at(x);</span> <span id="LC44" class="line"> for (int i=0;i&lt;line.size()-1;i++)</span> <span id="LC45" class="line"> {</span> <span id="LC46" class="line"> p-&gt;drawLine(line.at(i),line.at(i+1));</span> <span id="LC47" class="line"> }</span> <span id="LC48" class="line"></span> <span id="LC49" class="line"></span> <span id="LC50" class="line"> }</span> <span id="LC51" class="line"></span> <span id="LC52" class="line"></span> <span id="LC53" class="line">}</span> <span id="LC54" class="line"></span> <span id="LC55" class="line"></span> <span id="LC56" class="line"></span> <span id="LC57" class="line">void ww::mouseMoveEvent(QMouseEvent *event)</span> <span id="LC58" class="line">{</span> <span id="LC59" class="line"> QVector&lt;QPoint&gt;&amp; lastLine=_lines.last();</span> <span id="LC60" class="line"> lastLine.append(event-&gt;pos());</span> <span id="LC61" class="line"> update(); //重绘窗口,隐含调用PaintEvent</span> <span id="LC62" class="line">}</span> <span id="LC63" class="line">void ww::mousePressEvent(QMouseEvent *event)</span> <span id="LC64" class="line">{</span> <span id="LC65" class="line"></span> <span id="LC66" class="line"> QVector&lt;QPoint&gt; line;</span> <span id="LC67" class="line"> _lines.append(line);</span> <span id="LC68" class="line"></span> <span id="LC69" class="line"> QVector&lt;QPoint&gt;&amp; lastLine=_lines.last();</span> <span id="LC70" class="line"> lastLine.append(event-&gt;pos());</span> <span id="LC71" class="line">}</span> <span id="LC72" class="line"></span> <span id="LC73" class="line"></span> <span id="LC74" class="line">void ww::mouseReleaseEvent(QMouseEvent *event)</span> <span id="LC75" class="line">{</span> <span id="LC76" class="line"> QVector&lt;QPoint&gt;&amp; lastLine=_lines.last();</span> <span id="LC77" class="line"> lastLine.append(event-&gt;pos());</span> <span id="LC78" class="line">}</span> <span id="LC79" class="line"></span> <span id="LC80" class="line"></span> <span id="LC81" class="line">int main (int argc,char** argv)</span> <span id="LC82" class="line">{</span> <span id="LC83" class="line"> QApplication app(argc,argv );</span> <span id="LC84" class="line"></span> <span id="LC85" class="line"> ww* window=new ww();</span> <span id="LC86" class="line">// window-&gt;Pain();</span> <span id="LC87" class="line"> window-&gt;show();</span> <span id="LC88" class="line"></span> <span id="LC89" class="line"> return app.exec();</span> <span id="LC90" class="line">}</span> <span id="LC91" class="line"></span></pre></div></div> <p><a name="1062-qpainter-%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%B7%B1%E7%9A%84%E6%8E%A7%E4%BB%B6"></a></p> <h4> <a id="1062-qpainter-实现自己的控件" class="anchor" href="#1062-qpainter-%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%B7%B1%E7%9A%84%E6%8E%A7%E4%BB%B6"></a>1.0.6.2. QPainter 实现自己的控件</h4> <p><a name="107-%E4%BF%A1%E5%8F%B7%E5%92%8C%E6%A7%BD"></a></p> <h3> <a id="107-信号和槽" class="anchor" href="#107-%E4%BF%A1%E5%8F%B7%E5%92%8C%E6%A7%BD"></a>1.0.7. 信号和槽</h3> <p><a name="1071-%E5%AE%9A%E4%B9%89"></a></p> <h4> <a id="1071-定义" class="anchor" href="#1071-%E5%AE%9A%E4%B9%89"></a>1.0.7.1. 定义</h4> <ul> <li>信号(Signal) 函数必须放在signals 保留字下,并且不需要实现</li> <li>槽 必须放在slots 保留字下,切必须实现</li> <li>信号和槽通过QObject::connect()函数连接</li> <li>当信号被触发时,槽函数执行</li> <li>触发信号用emit 关键字</li> </ul> <p><a name="1072-%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84%E4%BA%8B%E9%A1%B9"></a></p> <h4> <a id="1072-需要注意的事项" class="anchor" href="#1072-%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84%E4%BA%8B%E9%A1%B9"></a>1.0.7.2. 需要注意的事项</h4> <ul> <li>信号和槽,时QT的扩展,所以实现信号和槽的类,必须是QObject的子类</li> <li>实现信号和槽的类,必须以 Q_OBJECT 开始</li> <li>连接信号和槽,需要用到 SIGNAL 和 SLOT 宏转换为字符串</li> <li>一个信号可以和多个槽连接,槽函数调用的顺序是不确定的</li> <li>多个信号可以同时连接一个槽</li> <li>信号可以连接信号,形成传递</li> <li>信号和槽的参数参数应该一样多,类型必须一样。</li> <li>信号可以重载</li> <li>槽可以重载</li> <li>信号和槽都可以有默认参数</li> <li>槽函数可以向普通函数一样调用(Widget::close())</li> <li>在槽函数中,调用sender可以获得信号调用者</li> <li>.....</li> </ul> <p><a name="1073-%E6%80%BB%E7%BB%93"></a></p> <h4> <a id="1073-总结" class="anchor" href="#1073-%E6%80%BB%E7%BB%93"></a>1.0.7.3. 总结:</h4> <ul> <li>一个类 QObject</li> <li>三个宏 Q_OBJECT SIGNAL SLOT</li> <li>三个保留字 signals slots emit</li> </ul> <p><a name="108-%E9%AB%98%E7%BA%A7painter"></a></p> <h3> <a id="108-高级painter" class="anchor" href="#108-%E9%AB%98%E7%BA%A7painter"></a>1.0.8. 高级Painter</h3> <ul> <li>QGraphicSence 和 QGraphicsItem 头文件使用</li> </ul> <p><a name="109-qdialog"></a></p> <h3> <a id="109-qdialog" class="anchor" href="#109-qdialog"></a>1.0.9. QDialog</h3> <ul> <li> <p>模块对话框和普通对话框:</p> <ul> <li>通过exec() 运行的是模块对话框,模块对话框有自己的消息循环,且把APP 的消息循环接管了(就是模块对话框出来以后,主窗口就点不动了</li> <li>通过show() 运行的是普通对话框,普通对话框可以有多个,且互不干扰</li> <li>如果Dialog 是通过exec() 来来显示,可以通过 accept() 和 Reject() 来关闭窗口。show() 显示则通过 close() 来关闭,和 Qwidget 一样。</li> </ul> </li> <li> <p>有很多特殊的 Dialog 如打印(预览) ,文件选择,MessageBox,颜色选择,字体选择</p> </li> </ul> <p><a name="1091-%E4%BB%A3%E7%A0%81%E5%B1%95%E7%A4%BA"></a></p> <h4> <a id="1091-代码展示" class="anchor" href="#1091-%E4%BB%A3%E7%A0%81%E5%B1%95%E7%A4%BA"></a>1.0.9.1. 代码展示</h4> <ul> <li>Dialog.h</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #ifndef DIALOG_H</span> <span id="LC2" class="line"> #define DIALOG_H </span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QDialog&gt;</span> <span id="LC5" class="line"> #include &lt;QPushButton&gt;</span> <span id="LC6" class="line"> #include &lt;QDebug&gt;</span> <span id="LC7" class="line"> #include &lt;QFileDialog&gt;</span> <span id="LC8" class="line"> #include &lt;QString&gt;</span> <span id="LC9" class="line"> #include &lt;QFileInfo&gt;</span> <span id="LC10" class="line"> #include &lt;QFontDialog&gt;</span> <span id="LC11" class="line"> </span> <span id="LC12" class="line"> #include &lt;QMessageBox&gt;</span> <span id="LC13" class="line"></span> <span id="LC14" class="line">namespace Ui {</span> <span id="LC15" class="line">class Dialog;</span> <span id="LC16" class="line">}</span> <span id="LC17" class="line"></span> <span id="LC18" class="line">class Dialog : public QDialog</span> <span id="LC19" class="line">{</span> <span id="LC20" class="line"> Q_OBJECT</span> <span id="LC21" class="line"></span> <span id="LC22" class="line">public:s</span> <span id="LC23" class="line"> explicit Dialog(QWidget *parent = 0);</span> <span id="LC24" class="line"> ~Dialog();</span> <span id="LC25" class="line"></span> <span id="LC26" class="line">signals:</span> <span id="LC27" class="line"></span> <span id="LC28" class="line"></span> <span id="LC29" class="line">public slots:</span> <span id="LC30" class="line"></span> <span id="LC31" class="line"> void _clicked()</span> <span id="LC32" class="line"> {</span> <span id="LC33" class="line">#if 0</span> <span id="LC34" class="line"> QDialog* qd=new QDialog();</span> <span id="LC35" class="line">// qd-&gt;setParent(this); //不能设置Parent。。。</span> <span id="LC36" class="line"> qDebug()&lt;&lt;"Clicked";</span> <span id="LC37" class="line"></span> <span id="LC38" class="line"> QPushButton* qpp=new QPushButton("Accept",qd);</span> <span id="LC39" class="line"> connect(qpp,SIGNAL(clicked(bool)),qd,SLOT(accept()));</span> <span id="LC40" class="line"> int rect;</span> <span id="LC41" class="line"> if(rect=qd-&gt;exec() == QDialog::Accepted) //exec() 显示是个模块对话框 只能有一个</span> <span id="LC42" class="line"> {</span> <span id="LC43" class="line"></span> <span id="LC44" class="line"> qDebug()&lt;&lt;"Accept";</span> <span id="LC45" class="line"> }</span> <span id="LC46" class="line"></span> <span id="LC47" class="line"> if(rect==QDialog::Rejected)</span> <span id="LC48" class="line"> {</span> <span id="LC49" class="line"> qDebug()&lt;&lt;"Rejected";</span> <span id="LC50" class="line"> }</span> <span id="LC51" class="line">// qd-&gt;show(); //show() 显示,是正常对话框 可以有无数个</span> <span id="LC52" class="line"></span> <span id="LC53" class="line">#endif</span> <span id="LC54" class="line">#if 0</span> <span id="LC55" class="line"></span> <span id="LC56" class="line"></span> <span id="LC57" class="line"> QString Filename =</span> <span id="LC58" class="line"> QFileDialog::getSaveFileName(NULL,"Slect File for save",_str,"PNG File(*.png)"); //QFIleDialog 一般直接直接使用静态函数 getOpenFileName 指定打开文件</span> <span id="LC59" class="line"> qDebug()&lt;&lt;Filename;</span> <span id="LC60" class="line"> QFileInfo FileInfo(Filename);</span> <span id="LC61" class="line"> _str=FileInfo.path();</span> <span id="LC62" class="line"></span> <span id="LC63" class="line"></span> <span id="LC64" class="line"> QFontDialog* font=new QFontDialog(this);</span> <span id="LC65" class="line"> font-&gt;exec();</span> <span id="LC66" class="line"> qDebug()&lt;&lt;font-&gt;selectedFont();</span> <span id="LC67" class="line"> #endif</span> <span id="LC68" class="line"></span> <span id="LC69" class="line"> QMessageBox::warning(this,"警告","警告@!!!!");</span> <span id="LC70" class="line"></span> <span id="LC71" class="line"> }</span> <span id="LC72" class="line"></span> <span id="LC73" class="line">private:</span> <span id="LC74" class="line"> Ui::Dialog *ui;</span> <span id="LC75" class="line"> QString _str;</span> <span id="LC76" class="line">};</span> <span id="LC77" class="line"></span> <span id="LC78" class="line">#endif // DIALOG_H</span> <span id="LC79" class="line"></span> <span id="LC80" class="line"></span></pre></div></div> <ul> <li>Dialog.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include "dialog.h"</span> <span id="LC2" class="line">#include "ui_dialog.h"</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">Dialog::Dialog(QWidget *parent) :</span> <span id="LC5" class="line"> QDialog(parent),</span> <span id="LC6" class="line"> ui(new Ui::Dialog)</span> <span id="LC7" class="line">{</span> <span id="LC8" class="line"> QPushButton* pb=new QPushButton("new one");</span> <span id="LC9" class="line"> pb-&gt;setParent(this);</span> <span id="LC10" class="line"> ui-&gt;setupUi(this);</span> <span id="LC11" class="line"> connect(pb,SIGNAL(clicked(bool)),this,SLOT(_clicked()));</span> <span id="LC12" class="line">}</span> <span id="LC13" class="line"></span> <span id="LC14" class="line">Dialog::~Dialog()</span> <span id="LC15" class="line">{</span> <span id="LC16" class="line"> delete ui;</span> <span id="LC17" class="line">}</span> <span id="LC18" class="line"></span></pre></div></div> <ul> <li>main.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include "dialog.h"</span> <span id="LC2" class="line">#include &lt;QApplication&gt;</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">int main(int argc, char *argv[])</span> <span id="LC5" class="line">{</span> <span id="LC6" class="line"> QApplication a(argc, argv);</span> <span id="LC7" class="line"> Dialog w;</span> <span id="LC8" class="line"> w.show();</span> <span id="LC9" class="line"></span> <span id="LC10" class="line"> return a.exec();</span> <span id="LC11" class="line">}</span> <span id="LC12" class="line"></span></pre></div></div> <ul> <li>dialog.ui</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span> <span id="LC2" class="line">&lt;ui version="4.0"&gt;</span> <span id="LC3" class="line"> &lt;class&gt;Dialog&lt;/class&gt;</span> <span id="LC4" class="line"> &lt;widget class="QDialog" name="Dialog"&gt;</span> <span id="LC5" class="line"> &lt;property name="geometry"&gt;</span> <span id="LC6" class="line"> &lt;rect&gt;</span> <span id="LC7" class="line"> &lt;x&gt;0&lt;/x&gt;</span> <span id="LC8" class="line"> &lt;y&gt;0&lt;/y&gt;</span> <span id="LC9" class="line"> &lt;width&gt;946&lt;/width&gt;</span> <span id="LC10" class="line"> &lt;height&gt;560&lt;/height&gt;</span> <span id="LC11" class="line"> &lt;/rect&gt;</span> <span id="LC12" class="line"> &lt;/property&gt;</span> <span id="LC13" class="line"> &lt;property name="windowTitle"&gt;</span> <span id="LC14" class="line"> &lt;string&gt;Dialog&lt;/string&gt;</span> <span id="LC15" class="line"> &lt;/property&gt;</span> <span id="LC16" class="line"> &lt;/widget&gt;</span> <span id="LC17" class="line"> &lt;layoutdefault spacing="6" margin="11"/&gt;</span> <span id="LC18" class="line"> &lt;resources/&gt;</span> <span id="LC19" class="line"> &lt;connections/&gt;</span> <span id="LC20" class="line">&lt;/ui&gt;</span> <span id="LC21" class="line"></span></pre></div></div> <p><a name="1010-mainwindow"></a></p> <h3> <a id="1010-mainwindow" class="anchor" href="#1010-mainwindow"></a>1.0.10. MainWindow</h3> <ul> <li> <p>主窗口</p> </li> <li> <p>菜单栏 : Qmenu 菜单类 QMenuBar 菜单栏 QAction 菜带项目</p> </li> <li> <p>Status Bar</p> </li> <li> <p>Tool Bar</p> </li> <li> <p>QSystemTrayIcon 系统托盘类</p> </li> <li> <p>示例代码如下:</p> <ul> <li>mw.h</li> </ul> </li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #ifndef MW_H</span> <span id="LC2" class="line"> #define MW_H</span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QMainWindow&gt;</span> <span id="LC5" class="line"> #include &lt;QApplication&gt;</span> <span id="LC6" class="line"> #include &lt;QMenu&gt;</span> <span id="LC7" class="line"> #include &lt;QMenuBar&gt;</span> <span id="LC8" class="line"> #include &lt;QAction&gt;</span> <span id="LC9" class="line"> #include &lt;QList&gt;</span> <span id="LC10" class="line"> #include &lt;QFileDialog&gt;</span> <span id="LC11" class="line"> #include &lt;QString&gt;</span> <span id="LC12" class="line"> #include &lt;QDebug&gt;</span> <span id="LC13" class="line"> #include &lt;QToolBar&gt;</span> <span id="LC14" class="line"> #include &lt;QStatusBar&gt;</span> <span id="LC15" class="line"> #include &lt;QLabel&gt;</span> <span id="LC16" class="line"> #include &lt;QSystemTrayIcon&gt; //系统托盘类</span> <span id="LC17" class="line"> #include "mview.h"</span> <span id="LC18" class="line"></span> <span id="LC19" class="line">class mw : public QMainWindow</span> <span id="LC20" class="line">{</span> <span id="LC21" class="line"> Q_OBJECT</span> <span id="LC22" class="line"></span> <span id="LC23" class="line">public:</span> <span id="LC24" class="line"> mw(QWidget *parent = 0);</span> <span id="LC25" class="line"> ~mw();</span> <span id="LC26" class="line"></span> <span id="LC27" class="line">private:</span> <span id="LC28" class="line">QString _str;</span> <span id="LC29" class="line">mview* _view;</span> <span id="LC30" class="line">QMenu* _menu;</span> <span id="LC31" class="line">QSystemTrayIcon* _icon;</span> <span id="LC32" class="line"></span> <span id="LC33" class="line">public slots:</span> <span id="LC34" class="line"> void open_file()</span> <span id="LC35" class="line"> {</span> <span id="LC36" class="line"> _str=QFileDialog::getOpenFileName(this,"",_str,"TXT File (*.txt)");</span> <span id="LC37" class="line"></span> <span id="LC38" class="line"> qDebug()&lt;&lt;"Opened File :"&lt;&lt;_str;</span> <span id="LC39" class="line"></span> <span id="LC40" class="line"></span> <span id="LC41" class="line"></span> <span id="LC42" class="line"> }</span> <span id="LC43" class="line"></span> <span id="LC44" class="line"> void open_dir()</span> <span id="LC45" class="line"> {</span> <span id="LC46" class="line"> _str=QFileDialog::getExistingDirectory(this,"",_str);</span> <span id="LC47" class="line"></span> <span id="LC48" class="line"> qDebug()&lt;&lt;"Opened Directory :"&lt;&lt;_str;</span> <span id="LC49" class="line"></span> <span id="LC50" class="line"></span> <span id="LC51" class="line"></span> <span id="LC52" class="line"> }</span> <span id="LC53" class="line"></span> <span id="LC54" class="line">// void paintEvent(QPaintEvent *event);</span> <span id="LC55" class="line"></span> <span id="LC56" class="line"> void mousePressEvent(QMouseEvent *event);</span> <span id="LC57" class="line"> bool event(QEvent *event);</span> <span id="LC58" class="line"></span> <span id="LC59" class="line">signals:</span> <span id="LC60" class="line"> void clicked();</span> <span id="LC61" class="line">};</span> <span id="LC62" class="line"></span> <span id="LC63" class="line"></span> <span id="LC64" class="line"></span> <span id="LC65" class="line">#endif // MW_H</span> <span id="LC66" class="line"></span> <span id="LC67" class="line"></span></pre></div></div> <ul> <li>mw.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #include "mw.h"</span> <span id="LC2" class="line"></span> <span id="LC3" class="line">mw::mw(QWidget *parent)</span> <span id="LC4" class="line"> : QMainWindow(parent)</span> <span id="LC5" class="line">{</span> <span id="LC6" class="line"> //加菜单</span> <span id="LC7" class="line"> QMenuBar* menubar=this-&gt;menuBar(); //获取mainWindow 里面的MenuBar</span> <span id="LC8" class="line"> QMenu* menu=menubar-&gt;addMenu("&amp;Open"); //通过MenuBar 添加菜单,每个菜单对弈一个菜单项</span> <span id="LC9" class="line"> QAction* open_file=new QAction("Open File");</span> <span id="LC10" class="line"> QAction* open_Directory=new QAction("Open Directory");</span> <span id="LC11" class="line"> QAction* exit_this=new QAction("Exit");</span> <span id="LC12" class="line">// QAction* blank=new QAction("Exit");</span> <span id="LC13" class="line"></span> <span id="LC14" class="line"> exit_this-&gt;setShortcut(QKeySequence::Quit);</span> <span id="LC15" class="line"> open_file-&gt;setShortcut(QKeySequence::Open);</span> <span id="LC16" class="line"> connect(exit_this,SIGNAL(triggered(bool)),this,SLOT(close())); //绑定槽函数,信号使用trggered()</span> <span id="LC17" class="line"> connect(open_file,SIGNAL(triggered(bool)),this,SLOT(open_file()));</span> <span id="LC18" class="line"> connect(open_Directory,SIGNAL(triggered(bool)),this,SLOT(open_dir()));</span> <span id="LC19" class="line"> QList&lt;QAction*&gt; ql={</span> <span id="LC20" class="line"> open_file,</span> <span id="LC21" class="line"> open_Directory,</span> <span id="LC22" class="line"> exit_this</span> <span id="LC23" class="line"> };</span> <span id="LC24" class="line"> menu-&gt;addActions(ql);</span> <span id="LC25" class="line"> _menu =menu;</span> <span id="LC26" class="line"></span> <span id="LC27" class="line"> // ToolBar</span> <span id="LC28" class="line"></span> <span id="LC29" class="line"> QToolBar* tb1= this-&gt;addToolBar("TollBar");</span> <span id="LC30" class="line"> tb1-&gt;addActions(ql);</span> <span id="LC31" class="line"></span> <span id="LC32" class="line"></span> <span id="LC33" class="line"> //Status Bar</span> <span id="LC34" class="line"></span> <span id="LC35" class="line"> QStatusBar* stb=this-&gt;statusBar();</span> <span id="LC36" class="line"> stb-&gt;addWidget(new QLabel("OK"));</span> <span id="LC37" class="line"> stb-&gt;addActions(ql);</span> <span id="LC38" class="line"></span> <span id="LC39" class="line"></span> <span id="LC40" class="line"></span> <span id="LC41" class="line"> _view = new mview;</span> <span id="LC42" class="line"></span> <span id="LC43" class="line"> this-&gt;setCentralWidget(_view);</span> <span id="LC44" class="line"></span> <span id="LC45" class="line"> this-&gt;setWindowOpacity(0.9); //设置 窗口透明度 小数</span> <span id="LC46" class="line"></span> <span id="LC47" class="line"> //创建托盘类</span> <span id="LC48" class="line"> _icon=new QSystemTrayIcon(this);</span> <span id="LC49" class="line"> _icon-&gt;setIcon(QIcon("E:/code/universal/QT/MW/1.ico"));</span> <span id="LC50" class="line"> _icon-&gt;setToolTip("我是Elliot 的小程序的哦");</span> <span id="LC51" class="line"> _icon-&gt;show();</span> <span id="LC52" class="line"></span> <span id="LC53" class="line">}</span> <span id="LC54" class="line"></span> <span id="LC55" class="line"></span> <span id="LC56" class="line">//void mw::paintEvent(QPaintEvent *event)</span> <span id="LC57" class="line">//{</span> <span id="LC58" class="line"></span> <span id="LC59" class="line"></span> <span id="LC60" class="line">//}</span> <span id="LC61" class="line"></span> <span id="LC62" class="line"></span> <span id="LC63" class="line"> void mw::mousePressEvent(QMouseEvent *event)</span> <span id="LC64" class="line"> {</span> <span id="LC65" class="line"> if(event-&gt;button()==Qt::RightButton)</span> <span id="LC66" class="line"> _menu-&gt;exec(QCursor::pos());</span> <span id="LC67" class="line"> }</span> <span id="LC68" class="line"></span> <span id="LC69" class="line"> bool mw::event(QEvent *event)</span> <span id="LC70" class="line"> {</span> <span id="LC71" class="line"> if(event-&gt;type()==QEvent::Close)</span> <span id="LC72" class="line"> {</span> <span id="LC73" class="line"> return true;</span> <span id="LC74" class="line"> }</span> <span id="LC75" class="line"></span> <span id="LC76" class="line"> return QMainWindow::event(event);</span> <span id="LC77" class="line"> }</span> <span id="LC78" class="line"></span> <span id="LC79" class="line"></span> <span id="LC80" class="line">mw::~mw()</span> <span id="LC81" class="line">{</span> <span id="LC82" class="line"></span> <span id="LC83" class="line">}</span> <span id="LC84" class="line"></span></pre></div></div> <ul> <li>mview.h</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #ifndef MVIEW_H</span> <span id="LC2" class="line"> #define MVIEW_H</span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QWidget&gt;</span> <span id="LC5" class="line"> #include &lt;QPainter&gt;</span> <span id="LC6" class="line"> #include &lt;QPaintEvent&gt;</span> <span id="LC7" class="line"> #include &lt;QPixmap&gt;</span> <span id="LC8" class="line"></span> <span id="LC9" class="line">class mview : public QWidget</span> <span id="LC10" class="line">{</span> <span id="LC11" class="line"> Q_OBJECT</span> <span id="LC12" class="line">public:</span> <span id="LC13" class="line"> explicit mview(QWidget *parent = nullptr){}</span> <span id="LC14" class="line"></span> <span id="LC15" class="line"> void paintEvent(QPaintEvent *event)</span> <span id="LC16" class="line"> {</span> <span id="LC17" class="line"> QPainter* panter=new QPainter(this);</span> <span id="LC18" class="line"> panter-&gt;fillRect(rect(),Qt::black);</span> <span id="LC19" class="line"> panter-&gt;drawArc(50,50,30,30,10,50);</span> <span id="LC20" class="line"> panter-&gt;drawPixmap(QPoint(0,0),QPixmap("E:/code/universal/QT/MW/11.png"));</span> <span id="LC21" class="line"> }</span> <span id="LC22" class="line"></span> <span id="LC23" class="line">signals:</span> <span id="LC24" class="line"></span> <span id="LC25" class="line">public slots:</span> <span id="LC26" class="line">};</span> <span id="LC27" class="line"></span> <span id="LC28" class="line">#endif // MVIEW_H</span> <span id="LC29" class="line"></span></pre></div></div> <ul> <li>mview.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include "mview.h"</span> <span id="LC2" class="line"></span> <span id="LC3" class="line">mview::mview(QWidget *parent) : QWidget(parent)</span> <span id="LC4" class="line">{</span> <span id="LC5" class="line"></span> <span id="LC6" class="line"></span> <span id="LC7" class="line">}</span> <span id="LC8" class="line"></span> <span id="LC9" class="line"></span></pre></div></div> <ul> <li>main.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #include "mw.h"</span> <span id="LC2" class="line"> #include &lt;QApplication&gt;</span> <span id="LC3" class="line"></span> <span id="LC4" class="line">int main(int argc, char *argv[])</span> <span id="LC5" class="line">{</span> <span id="LC6" class="line"> QApplication a(argc, argv);</span> <span id="LC7" class="line"> mw w;</span> <span id="LC8" class="line"> w.show();</span> <span id="LC9" class="line"></span> <span id="LC10" class="line"> return a.exec();</span> <span id="LC11" class="line"></span> <span id="LC12" class="line">}</span> <span id="LC13" class="line"></span></pre></div></div> <p><a name="1011-qfile-qbuffer-qxxxxstream-mapping"></a></p> <h3> <a id="1011-qfile-qbuffer-qxxxxstream-mapping" class="anchor" href="#1011-qfile-qbuffer-qxxxxstream-mapping"></a>1.0.11. QFile-QBuffer-QXXXXStream-Mapping</h3> <ul> <li>QIODevice 是所有IO 类的父类 <ul> <li>QFile</li> <li>QBuffer //Memery File</li> <li>QTCPSocket</li> <li>QUDPSocket</li> </ul> </li> </ul> <p><a name="10111-qfile"></a></p> <h4> <a id="10111-qfile" class="anchor" href="#10111-qfile"></a>1.0.11.1. QFile</h4> <ul> <li>头文件 QFile</li> <li>函数 open write</li> <li>QString 是多字节字符串 QByteArray 是字节数</li> </ul> <p><a name="10112-qbuffer-%E5%86%85%E5%AD%98-%E6%96%87%E4%BB%B6"></a></p> <h4> <a id="10112-qbuffer-内存-文件" class="anchor" href="#10112-qbuffer-%E5%86%85%E5%AD%98-%E6%96%87%E4%BB%B6"></a>1.0.11.2. QBuffer //内存 文件</h4> <ul> <li>头文件 QBuffer</li> <li>函数 open write</li> </ul> <p><a name="10113-qdatastream-qtextstream"></a></p> <h4> <a id="10113-qdatastream-qtextstream" class="anchor" href="#10113-qdatastream-qtextstream"></a>1.0.11.3. QDataStream QTextStream</h4> <ul> <li>代码展示 <ul> <li>mw.h</li> </ul> </li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line"> #ifndef MW_H</span> <span id="LC2" class="line"> #define MW_H</span> <span id="LC3" class="line"></span> <span id="LC4" class="line"> #include &lt;QWidget&gt;</span> <span id="LC5" class="line"> #include &lt;QApplication&gt;</span> <span id="LC6" class="line"> #include &lt;QFile&gt;</span> <span id="LC7" class="line"> #include &lt;QBuffer&gt;</span> <span id="LC8" class="line"> #include &lt;QTextBrowser&gt;</span> <span id="LC9" class="line"> #include &lt;QPixmap&gt;</span> <span id="LC10" class="line"> #include &lt;QLabel&gt;</span> <span id="LC11" class="line"> #include &lt;QTextStream&gt;</span> <span id="LC12" class="line"> #include &lt;QDataStream&gt;</span> <span id="LC13" class="line"></span> <span id="LC14" class="line"></span> <span id="LC15" class="line">class mw : public QWidget</span> <span id="LC16" class="line">{</span> <span id="LC17" class="line"> Q_OBJECT</span> <span id="LC18" class="line">public:</span> <span id="LC19" class="line"> explicit mw(QWidget *parent = nullptr);</span> <span id="LC20" class="line"></span> <span id="LC21" class="line">signals:</span> <span id="LC22" class="line"></span> <span id="LC23" class="line">public slots:</span> <span id="LC24" class="line">};</span> <span id="LC25" class="line"></span> <span id="LC26" class="line">#endif // MW_H</span> <span id="LC27" class="line"></span></pre></div></div> <ul> <li>mw.cpp</li> </ul> <div class="white"><div class="highlight"><pre><span id="LC1" class="line">#include "mw.h"</span> <span id="LC2" class="line"></span> <span id="LC3" class="line">mw::mw(QWidget *parent) : QWidget(parent)</span> <span id="LC4" class="line">{</span> <span id="LC5" class="line"> QTextBrowser* qbb=new QTextBrowser(this);</span> <span id="LC6" class="line"> QFile file("devil.txt");</span> <span id="LC7" class="line"> file.open(QFile::ReadWrite);</span> <span id="LC8" class="line"></span> <span id="LC9" class="line"> file.write("jaingnan");</span> <span id="LC10" class="line"></span> <span id="LC11" class="line"></span> <span id="LC12" class="line"> file.close();</span> <span id="LC13" class="line"></span> <span id="LC14" class="line"></span> <span id="LC15" class="line"> QBuffer buffer;</span> <span id="LC16" class="line"></span> <span id="LC17" class="line"> buffer.open(QBuffer::ReadWrite);</span> <span id="LC18" class="line">#if 0</span> <span id="LC19" class="line"> buffer.write("aa");</span> <span id="LC20" class="line">// QString aa('江南的风');</span> <span id="LC21" class="line"> buffer.write("Nothing to read");</span> <span id="LC22" class="line"></span> <span id="LC23" class="line">#endif</span> <span id="LC24" class="line"></span> <span id="LC25" class="line"> QPixmap* map=new QPixmap("11.png");</span> <span id="LC26" class="line"></span> <span id="LC27" class="line"> map-&gt;save(&amp;buffer,"png");</span> <span id="LC28" class="line"></span> <span id="LC29" class="line"></span> <span id="LC30" class="line"> qbb-&gt;setText(buffer.buffer());</span> <span id="LC31" class="line"> QString str=tr("%1").arg(buffer.size());</span> <span id="LC32" class="line"> qbb-&gt;append(str);</span> <span id="LC33" class="line"> buffer.close();</span> <span id="LC34" class="line"></span> <span id="LC35" class="line"></span> <span id="LC36" class="line"> QPixmap* png=new QPixmap;</span> <span id="LC37" class="line"> png-&gt;loadFromData(buffer.buffer(),"PNG");</span> <span id="LC38" class="line"> const QPixmap png1=*png;</span> <span id="LC39" class="line"> QLabel* label=new QLabel(this);</span> <span id="LC40" class="line"> label-&gt;setPixmap(png1);</span> <span id="LC41" class="line"></span> <span id="LC42" class="line"></span> <span id="LC43" class="line"> //Text Stream</span> <span id="LC44" class="line"> QFile file1("stream.txt");</span> <span id="LC45" class="line"> file1.open(QFile::ReadWrite);</span> <span id="LC46" class="line"></span> <span id="LC47" class="line"> QTextStream qs(&amp;file1);</span> <span id="LC48" class="line"></span> <span id="LC49" class="line"> qs&lt;&lt;"nimeide"&lt;&lt;1.2&lt;&lt;22;</span> <span id="LC50" class="line"></span> <span id="LC51" class="line"></span> <span id="LC52" class="line"> file.close();</span> <span id="LC53" class="line"></span> <span id="LC54" class="line"></span> <span id="LC55" class="line"> //虚拟内存 Virtual Memory</span> <span id="LC56" class="line"></span> <span id="LC57" class="line">}</span> <span id="LC58" class="line"></span> <span id="LC59" class="line"></span> <span id="LC60" class="line">int main (int argc,char ** argv)</span> <span id="LC61" class="line">{</span> <span id="LC62" class="line"> QApplication app(argc,argv);</span> <span id="LC63" class="line"></span> <span id="LC64" class="line"></span> <span id="LC65" class="line"> mw* mw1=new mw();</span> <span id="LC66" class="line"> mw1-&gt;show();</span> <span id="LC67" class="line"> return app.exec();</span> <span id="LC68" class="line"></span> <span id="LC69" class="line">}</span> <span id="LC70" class="line"></span></pre></div></div> <p><a name="1012-socket-tcp-udp-boardcast-muli-cast"></a></p> <h3> <a id="1012-socket-tcp-udp-boardcast-muli-cast" class="anchor" href="#1012-socket-tcp-udp-boardcast-muli-cast"></a>1.0.12. Socket-TCP-UDP-BOARDCAST-MULI-CAST</h3> <ul> <li>QT socket 需要在 .pro 文件中添加 QT += newwork</li> <li>TCPServer 头文件</li> </ul> <p><a name="1013-%E5%85%B3%E4%BA%8E%E5%9C%A8new-%E7%94%9F%E6%88%90%E6%8E%A7%E4%BB%B6%E7%9A%84%E6%97%B6%E5%80%99this-%E7%9A%84%E8%A7%A3%E9%87%8A-%E5%92%8C-deletelater"></a></p> <h3> <a id="1013-关于在new-生成控件的时候this-的解释-和-deletelater" class="anchor" href="#1013-%E5%85%B3%E4%BA%8E%E5%9C%A8new-%E7%94%9F%E6%88%90%E6%8E%A7%E4%BB%B6%E7%9A%84%E6%97%B6%E5%80%99this-%E7%9A%84%E8%A7%A3%E9%87%8A-%E5%92%8C-deletelater"></a>1.0.13. 关于在new 生成控件的时候this 的解释 和 deleteLater</h3> <p><a name="10131-%E5%85%B3%E4%BA%8E%E5%9C%A8new-%E7%94%9F%E6%88%90%E6%8E%A7%E4%BB%B6%E7%9A%84%E6%97%B6%E5%80%99this"></a></p> <h4> <a id="10131-关于在new-生成控件的时候this" class="anchor" href="#10131-%E5%85%B3%E4%BA%8E%E5%9C%A8new-%E7%94%9F%E6%88%90%E6%8E%A7%E4%BB%B6%E7%9A%84%E6%97%B6%E5%80%99this"></a>1.0.13.1. 关于在new 生成控件的时候this</h4> <ul> <li>可以再生成控件的时候指定父对象this.此时,子对象的内存管理由父对象控制。 <ul> <li>当父对象销毁的时候,子对象也被父对象销毁。避免了内存泄漏</li> <li>窗口类可指定父子关系,避免了多一步的绑定</li> <li>指定了父对象后也可以使用delete 释放,此时子对象的析构函数会通知父对象接触父子关系</li> </ul> </li> </ul> <p><a name="10132-deletelater"></a></p> <h4> <a id="10132-deletelater" class="anchor" href="#10132-deletelater"></a>1.0.13.2. deleteLater</h4> <ul> <li>定义 延时删除,只是发送标记,不是立即删除</li> </ul> <p><a name="1014-qt%E5%8A%A8%E6%80%81%E5%BA%93"></a></p> <h3> <a id="1014-qt动态库" class="anchor" href="#1014-qt%E5%8A%A8%E6%80%81%E5%BA%93"></a>1.0.14. QT动态库</h3> <ul> <li>QT 使用LIBRARYSHARED_EXPORT (Q_DECL_EXPORT) 声明类来跨平台宏</li> <li>使用动态库是,需要在使用动态库的程序内包含源动态库的头文件,同时,包含库文件 <ul> <li>1.包含库头文件</li> <li>2.包含库位置: <ul> <li>在QT 的 .pro 文件中添加</li> <li><code> LIBS += LE:\code\universal\QT\build-Library-Desktop_Qt_5_14_1_MSVC2017_32bit-Debug\debug -lLibrary</code></li> <li><code>LIBS += L库的目录 -l库的名称</code></li> <li>不需要后缀 .dll</li> </ul> </li> </ul> </li> </ul> <p><a name="1015-qt%E9%9D%99%E6%80%81%E5%BA%93"></a></p> <h3> <a id="1015-qt静态库" class="anchor" href="#1015-qt%E9%9D%99%E6%80%81%E5%BA%93"></a>1.0.15. QT静态库</h3> <ul> <li>....</li> </ul> <p><a name="1016-qt-json"></a></p> <h3> <a id="1016-qt-json" class="anchor" href="#1016-qt-json"></a>1.0.16. QT JSON</h3> <ul> <li>头文件 </li> <li> </li> </ul> <p><a name="1017-qt-%E5%8A%A0%E5%AF%86"></a></p> <h3> <a id="1017-qt-加密" class="anchor" href="#1017-qt-%E5%8A%A0%E5%AF%86"></a>1.0.17. QT 加密</h3> <ul> <li>头文件 #include <ul> <li>有MD5 SHA 等加密</li> </ul> </li> </ul> <p><a name="1018-qsqldatabase-%E7%9A%84%E4%BD%BF%E7%94%A8"></a></p> <h3> <a id="1018-qsqldatabase-的使用" class="anchor" href="#1018-qsqldatabase-%E7%9A%84%E4%BD%BF%E7%94%A8"></a>1.0.18. QSQLDatabase 的使用</h3> <p><a name="10181-%E7%BC%96%E8%AF%91mysql-%E9%A9%B1%E5%8A%A8"></a></p> <h4> <a id="10181-编译mysql-驱动" class="anchor" href="#10181-%E7%BC%96%E8%AF%91mysql-%E9%A9%B1%E5%8A%A8"></a>1.0.18.1. 编译Mysql 驱动</h4> <ul> <li>1.下载MySQL Connector C 6.1并安装</li> <li>2.打开 VS 2017的开发人员命令提示符</li> <li>3.进入 C:\Qt\Qt5.14.1\5.14.1\Src\qtbase\src\plugins\sqldrivers\mysql\ <ul> <li>即进入 QT安装目录\版本号\Src\qtbase\src\plugins\sqldrivers\mysql\ 目录,实为QT mysql 驱动的源代码目录</li> </ul> </li> <li>4.修改 C:\Qt\Qt5.14.1\5.14.1\Src\qtbase\src\plugins\sqldrivers\mysql\mysql.pro 文件 <ul> <li>增加 <code> LIBS += -LC:\Qt\Qt5.14.1\5.14.1\Src\qtbase\src\plugins\sqldrivers\mysql -lmysql INCLUDEPATH +="C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/include" QMAKE_LIBDIR +="C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/lib"</code> </li> <li>即 增加 MySQL Connector C 6.1 安装目录下的include路径 和MySQL Connector C 6.1 安装目录下的lib路径 ,同时 增加MySQL Connector C 6.1 安装目录下的lib路径下的libmysql.dll(.lib)库</li> </ul> </li> <li> <ol start="5"> <li>输入 <code>C:\Qt\Qt5.14.1\5.14.1\msvc2017\bin\qmake -- MYSQL_INCDIR="C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include" MYSQL_LIBDIR="C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib" </code> </li> </ol> <ul> <li>即 qmake -- -- MYSQL_INCDIR=MySQL Connector C 6.1 安装目录下的include路径 MYSQL_LIBDIR=MySQL Connector C 6.1 安装目录下的lib路径</li> </ul> </li> <li>6.命令行输入 nmake</li> <li>7.命令行输入 nmake install</li> <li> <ol start="8"> <li>出错处理:</li> </ol> <ul> <li> <ol> <li>找不到 mysql 库 :</li> </ol> <ul> <li>qtsqldrivers-config.pri 文件找不到,在 C:\Qt\Qt5.14.1\5.14.1\Src\qtbase\src\plugins\sqldrivers\ 下有这个文件,提示在哪里找不到就复制到哪里去。我的是在文件生产目录找不到,那就把这个复制过去</li> <li>MySQL Connector C 6.1 未安装正确</li> <li>增加MySQL Connector C 6.1 安装目录下的lib路径下的libmysql.dll(.lib)库 的操作不正确。(mysql.pro 文件没写对)</li> <li>qmake -- -- MYSQL_INCDIR=MySQL Connector C 6.1 安装目录下的include路径 MYSQL_LIBDIR=MySQL Connector C 6.1 安装目录下的lib路径 命令写错C Connector 的路径了</li> </ul> </li> </ul> </li> </ul> <p><a name="1019-qsqldatabase-%E7%9A%84%E4%BD%BF%E7%94%A8"></a></p> <h3> <a id="1019-qsqldatabase-的使用" class="anchor" href="#1019-qsqldatabase-%E7%9A%84%E4%BD%BF%E7%94%A8"></a>1.0.19. QSQLDatabase 的使用</h3> <p><a name="10191-%E5%8E%9F%E7%94%9F%E6%89%A7%E8%A1%8Csql%E8%AF%AD%E5%8F%A5"></a></p> <h4> <a id="10191-原生执行sql语句" class="anchor" href="#10191-%E5%8E%9F%E7%94%9F%E6%89%A7%E8%A1%8Csql%E8%AF%AD%E5%8F%A5"></a>1.0.19.1. 原生执行SQL语句</h4> <p><a name="10192-qsqlmodel-%E6%96%B9%E5%BC%8F-%E6%89%A7%E8%A1%8C"></a></p> <h4> <a id="10192-qsqlmodel-方式-执行" class="anchor" href="#10192-qsqlmodel-%E6%96%B9%E5%BC%8F-%E6%89%A7%E8%A1%8C"></a>1.0.19.2. QSQLModel 方式 执行</h4> <p><a name="10193-qtableview-%E7%89%B9%E5%88%AB%E6%96%B9%E4%BE%BF"></a></p> <h4> <a id="10193-qtableview-特别方便" class="anchor" href="#10193-qtableview-%E7%89%B9%E5%88%AB%E6%96%B9%E4%BE%BF"></a>1.0.19.3. QTableView 特别方便</h4> <ul> <li>用来显示QTable 类型的数据</li> </ul> <p><a name="1020-qprocess-%E7%9A%84%E4%BD%BF%E7%94%A8"></a></p> <h3> <a id="1020-qprocess-的使用" class="anchor" href="#1020-qprocess-%E7%9A%84%E4%BD%BF%E7%94%A8"></a>1.0.20. Qprocess 的使用</h3> <ul> <li>头文件 <ul> <li>Qprocess</li> </ul> </li> </ul></em></em></div>

原文出处:https://www.cnblogs.com/dev995/p/Elliot.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部