巧妙收缩 vector 空间

resize() 或者 erase() 后 vector 的空间大小不会自动减小 int main() { vector<int> v; for(int i = 0; i < 100000; ++i){ v.push_back(i); } cout << "size: " << v.size() << endl; cout << "capacity: " << v.capacity() << endl; cout << "-----------------------" << endl; v.resize(10); cout << "size: " << v.size() << endl; cout << "capacity: " << v.capacity() << endl; cout << "-----------------------" << endl; vector<int>(v).swap(v); cout << "size: " << v....

January 16, 2022 · 1 min · Rick Cui

vector 中的元素存放在堆上还是栈上

结论 allocator 分配器是定义内存模型的类,用于标准库的某些部分,尤其是 STL 容器,如果所有标准容器的最后一个(可选)模板参数没有指定,那么它将使用这个分配器,并且它是标准库中唯一一个预定义的分配器 vector 中存放的如果是对象类型,则会通过 allocator 在堆上开辟足够的空间来存放和管理集合中的对象 vector 中存放指针类型,一定要记得手动释放内存 存放对象 class Person{ public: Person(int age, int id){ m_age = age; m_id = id; cout << "Person(int, int)..." << endl; } Person(const Person& p){ m_age = p.m_age; m_id = p.m_id; cout << "Person(const Person& p)..." << endl; } ~Person(){ cout << "~Person()..." << endl; } void* operator new(size_t size){ void* p = malloc(size); cout << "new()..." << endl; return p; } void operator delete(void *p){ cout << "delete()....

January 16, 2022 · 3 min · Rick Cui

STL Hello World

平时要有容器、算法、迭代器的思维模式 容器提供迭代器,算法使用迭代器 // 算法 int count(int* begin, int* end, int val){ int n = 0; while(begin != end){ if(*begin == val){ n++; } begin++; } return n; } int main() { // 容器 int arr[] = {1, 3, 0, 5, 1, 3, 1, 0}; // 迭代器 int* begin = arr; int* end = *(&arr + 1); int n = count(begin, end, 1); cout << "count: " << n << endl; return 0; }

January 16, 2022 · 1 min · Rick Cui

文件操作

一、文件读写 文件输入流 ifstream 文件输出流 ofstream 文件输入输出流 fstream 文件的打开方式 文件流的状态 文件流的定位:文件指针(输入指针、输出指针) 二、文本文件 #include <iostream>#include <fstream>using namespace std; void ReadWriteFile(){ ifstream ifs("D:\\Users\\cui_z\\Desktop\\source.txt", ios::in); ofstream ofs("D:\\Users\\cui_z\\Desktop\\target.txt", ios::out | ios::app); if (!ifs) { cout << "输入文件打开失败" << endl; return; } if (!ofs) { cout << "输出文件打开失败" << endl; return; } char ch; while (ifs.get(ch)) { cout << ch; ofs << ch; } ifs.close(); ofs.close(); } 三、二进制文件 文本文件和二进制文件在计算机中都是以二进制的方式存储的 程序中的对象都是二进制存储的 Windows 中的文本文件换行符用 \r\n 表示,二进制是以 \n 存储,所以存储和显示时会做一下转换 Linux 中二进制和文本文件换行都是以 \n 存储和表示 class Person { private: int m_age; int m_id; public: Person():m_age(0), m_id(0){ } Person(int age, int id){ m_age = age; m_id = id; } ~Person() = default; void show(){ cout << "Age: " << m_age << " ID: " << m_id << endl; } }; void BinaryReadWrite(){ // 存储二进制 ofstream ofs("D:\\Users\\cui_z\\Desktop\\target....

January 16, 2022 · 1 min · Rick Cui

格式化输出

#include <iostream>#include <cstdlib>#include <cstring>#include <iomanip> // 控制符头文件using namespace std; // 格式化输出 void func(){ // 方式一:使用成员方法 int number = 10; cout << number << endl; cout.unsetf(ios::dec); // 八进制 cout.setf(ios::oct); cout.setf(ios::showbase); cout << number << endl; // 012 // 十六进制 cout.unsetf(ios::oct); cout.setf(ios::hex); cout << number << endl; // 0xa // 固定宽度 cout.width(10); cout.fill('*'); cout << number << endl; // *******0xa // 上面的设置只对当前输出有效,下次的输出格式要重新设置 cout.setf(ios::left); cout.width(10); cout....

January 16, 2022 · 1 min · Rick Cui