C++ 资源库学习笔记

1. STL (Standard Template Library)

STL 是 C++ 标准库的一部分,提供了一系列的通用数据结构和算法。其中包括容器、迭代器、算法、函数对象等。

1.1 容器

容器是类模板,可以存储各种类型的对象,常见的有 vector、list、map 等。

  • vector

    vector 是一个动态数组,支持随机访问和动态扩展。

    cppCopy Code
    #include <vector> #include <iostream> using namespace std; int main() { // 创建一个 vector 对象 vector<int> v; // 在尾部添加元素 v.push_back(1); v.push_back(2); v.push_back(3); // 使用下标访问元素 cout << v[0] << endl; // 输出 1 // 使用迭代器遍历 vector for (auto iter = v.begin(); iter != v.end(); ++iter) { cout << *iter << ' '; } cout << endl; // 输出 1 2 3 return 0; }
  • list

    list 是双向链表,支持常数时间内在任意位置进行插入和删除操作。

    cppCopy Code
    #include <list> #include <iostream> using namespace std; int main() { // 创建一个 list 对象 list<int> l; // 在尾部添加元素 l.push_back(1); l.push_back(2); l.push_back(3); // 在头部插入元素 l.push_front(0); // 使用迭代器遍历 list for (auto iter = l.begin(); iter != l.end(); ++iter) { cout << *iter << ' '; } cout << endl; // 输出 0 1 2 3 return 0; }
  • map

    map 是关联数组,存储键值对,并根据键值进行排序。

    cppCopy Code
    #include <map> #include <iostream> using namespace std; int main() { // 创建一个 map 对象 map<string, int> m; // 存储键值对 m["apple"] = 1; m["banana"] = 2; m["orange"] = 3; // 使用迭代器遍历 map for (auto iter = m.begin(); iter != m.end(); ++iter) { cout << iter->first << ": " << iter->second << endl; } return 0; }

1.2 迭代器

迭代器是 STL 的核心,用于遍历容器元素。迭代器分为输入迭代器、输出迭代器、前向迭代器、双向迭代器和随机访问迭代器等类型。

cppCopy Code
#include <vector> #include <iostream> using namespace std; int main() { // 创建一个 vector 对象 vector<int> v = {1, 2, 3}; // 使用迭代器遍历 vector for (auto iter = v.begin(); iter != v.end(); ++iter) { cout << *iter << ' '; } cout << endl; // 输出 1 2 3 return 0; }

1.3 算法

STL 提供了大量的算法,涵盖了常见的排序、查找、拷贝、替换、计数、求和等操作。

  • sort

    对容器元素进行排序。

    cppCopy Code
    #include <vector> #include <algorithm> #include <iostream> using namespace std; int main() { // 创建一个 vector 对象 vector<int> v = {3, 2, 1}; // 对 vector 进行排序 sort(v.begin(), v.end()); // 使用迭代器遍历 vector for (auto iter = v.begin(); iter != v.end(); ++iter) { cout << *iter << ' '; } cout << endl; // 输出 1 2 3 return 0; }
  • find

    在容器中查找指定元素。

    cppCopy Code
    #include <vector> #include <algorithm> #include <iostream> using namespace std; int main() { // 创建一个 vector 对象 vector<int> v = {1, 2, 3}; // 查找元素 2 auto iter = find(v.begin(), v.end(), 2); if (iter != v.end()) { cout << "Found element: " << *iter << endl; } else { cout << "Element not found" << endl; } return 0; }
  • copy

    将容器中的元素拷贝到另一个容器中。

    cppCopy Code
    #include <vector> #include <algorithm> #include <iostream> using namespace std; int main() { // 创建一个 vector 对象 vector<int> v1 = {1, 2, 3}; vector<int> v2(3); // 将 v1 中的元素拷贝到 v2 中 copy(v1.begin(), v1.end(), v2.begin()); // 使用迭代器遍历 v2 for (auto iter = v2.begin(); iter != v2.end(); ++iter) { cout << *iter << ' '; } cout << endl; // 输出 1 2 3 return 0; }

1.4 函数对象

函数对象是用于封装函数或函数指针的对象,常见的有仿函数和 lambda 表达式。

  • 仿函数

    仿函数是重载了函数调用运算符的类,可以像函数一样调用。

    cppCopy Code
    #include <vector> #include <algorithm> #include <iostream> using namespace std; // 定义一个仿函数,用于比较两个元素的大小 class MyLess { public: bool operator()(int a, int b) { return a < b; } }; int main() { // 创建一个 vector 对象 vector<int> v = {3, 2, 1}; // 使用仿函数进行排序 sort(v.begin(), v.end(), MyLess()); // 使用迭代器遍历 vector for (auto iter = v.begin(); iter != v.end(); ++iter) { cout << *iter << ' '; } cout << endl; // 输出 1 2 3 return 0; }
  • lambda 表达式

    lambda 表达式是一种匿名函数,可以在需要函数对象的地方使用。

    cppCopy Code
    #include <vector> #include <algorithm> #include <iostream> using namespace std; int main() { // 创建一个 vector 对象 vector<int> v = {3, 2, 1}; // 使用 lambda 表达式进行排序 sort(v.begin(), v.end(), [](int a, int b){ return a < b; }); // 使用迭代器遍历 vector for (auto iter = v.begin(); iter != v.end(); ++iter) { cout << *iter << ' '; } cout << endl; // 输出 1 2 3 return 0; }

2. Boost C++ 库

Boost 是一个高质量、开源且跨平台的 C++ 库。其中包括了许多常用的应用程序框架和库,如文件系统、正则表达式、线程、日期时间等。

2.1 文件系统

文件系统库提供了一组类和函数,用于操作文件和目录。

cppCopy Code
#include <boost/filesystem.hpp> #include <iostream> using namespace std; using namespace boost::filesystem; int main() { // 创建一个路径对象 path p("."); // 遍历当前目录下的所有文件和目录 for (auto iter = directory_iterator(p); iter != directory_iterator(); ++iter) { cout << iter->path().filename() << endl; } return 0; }

2.2 正则表达式

正则表达式库提供了一组类和函数,用于解析和匹配正则表达式。

cppCopy Code
#include <boost/regex.hpp> #include <iostream> using namespace std; using namespace boost; int main() { // 创建一个正则表达式对象 regex reg("hello.*world"); // 匹配字符串 string s = "hello boost world"; if (regex_match(s, reg)) { cout << "Matched" << endl; } else { cout << "Not matched" << endl; } return 0; }

2.3 线程

线程库提供了一组类和函数,用于创建和管理线程。

cppCopy Code
#include <boost/thread.hpp> #include <iostream> using namespace std; // 线程函数 void thread_func() { cout << "Thread start" << endl; boost::this_thread::sleep(boost::posix_time::seconds(1)); cout << "Thread end" << endl; } int main() { // 创建一个线程 boost::thread t(thread_func); // 等待线程结束 t.join(); return 0; }

3. OpenCV 库

OpenCV 是一个开源计算机视觉库,提供了一系列图像处理和计算机视觉相关的函数。

3.1 图像读取和显示

cppCopy Code
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; int main() { // 读取图像 Mat img = imread("lena.png"); // 显示图像 namedWindow("image", WINDOW_NORMAL); imshow("image", img); waitKey(); return 0; }

3.2 图像处理

cppCopy Code
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; int main() { // 读取图像 Mat img = imread("lena.png"); // 灰度化 Mat gray; cvtColor(img, gray, COLOR_BGR2GRAY); // 边缘检测 Mat edges; Canny(gray, edges, 100, 200); // 显示边缘图像 namedWindow("edges", WINDOW_NORMAL); imshow("edges", edges); waitKey(); return 0; }

示例

下面是一个使用 STL 和 OpenCV 库的示例代码,实现了人脸检测和绘制矩形框的功能。

cppCopy Code
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; // 人脸检测 void detect_faces(const Mat& img, vector<Rect>& rects) { // 加载人脸分类器 CascadeClassifier classifier("haarcascade_frontalface_default.xml"); // 执行人脸检测 classifier.detectMultiScale(img, rects, 1.1, 3, 0, Size(30, 30)); } int main() { // 读取图像 Mat img = imread("faces.jpg"); // 灰度化 Mat gray; cvtColor(img, gray, COLOR_BGR2GRAY); // 人脸检测 vector<Rect> rects; detect_faces(gray, rects); // 绘制矩形框 for (auto rect : rects) { rectangle(img, rect, Scalar(0, 0, 255), 2); } // 显示结果图像 namedWindow("result", WINDOW_NORMAL); imshow("result", img); waitKey(); return 0; }