跳转到内容

STL 标准模板库

STL(Standard Template Library)是 C++ 的标准模板库,提供了丰富的容器和算法。

#include <vector>
using namespace std;
int main() {
// 创建 vector
vector<int> v;
// 添加元素
v.push_back(10);
v.push_back(20);
v.push_back(30);
// 访问元素
cout << v[0] << endl; // 输出:10
cout << v.at(1) << endl; // 输出:20
cout << v.front() << endl; // 输出:10
cout << v.back() << endl; // 输出:30
// 获取大小
cout << v.size() << endl; // 输出:3
// 删除最后一个元素
v.pop_back();
// 遍历
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
// 使用迭代器遍历
for (auto it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
// 使用基于范围的 for 循环
for (int num : v) {
cout << num << " ";
}
cout << endl;
return 0;
}
#include <string>
using namespace std;
int main() {
string s = "Hello World";
cout << s.length() << endl; // 长度:11
cout << s[0] << endl; // H
cout << s.substr(6) << endl; // World
cout << s.find("World") << endl; // 6
s += "!"; // 拼接
return 0;
}
#include <stack>
using namespace std;
int main() {
stack<int> st;
// 入栈
st.push(10);
st.push(20);
st.push(30);
// 访问栈顶
cout << st.top() << endl; // 输出:30
// 出栈
st.pop();
cout << st.top() << endl; // 输出:20
cout << st.size() << endl; // 输出:2
return 0;
}
#include <queue>
using namespace std;
int main() {
queue<int> q;
// 入队
q.push(10);
q.push(20);
q.push(30);
// 访问队首
cout << q.front() << endl; // 输出:10
// 出队
q.pop();
cout << q.front() << endl; // 输出:20
cout << q.size() << endl; // 输出:2
return 0;
}
#include <map>
#include <string>
using namespace std;
int main() {
map<string, int> ages;
// 插入元素
ages["Alice"] = 20;
ages["Bob"] = 25;
ages.insert({"Charlie", 30});
// 访问元素
cout << ages["Alice"] << endl; // 输出:20
cout << ages.at("Bob") << endl; // 输出:25
// 检查是否存在
if (ages.find("David") != ages.end()) {
cout << "找到了" << endl;
} else {
cout << "没有找到" << endl;
}
// 遍历
for (auto &pair : ages) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5, 9};
// 升序排序
sort(v.begin(), v.end());
// 降序排序
sort(v.begin(), v.end(), greater<int>());
// 输出排序后的结果
for (int num : v) {
cout << num << " ";
}
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
// 查找元素
auto it = find(v.begin(), v.end(), 3);
if (it != v.end()) {
cout << "找到了,位置是: " << (it - v.begin()) << endl;
}
// 二分查找(需要先排序)
sort(v.begin(), v.end());
bool found = binary_search(v.begin(), v.end(), 3);
cout << (found ? "找到了" : "没有找到") << endl;
return 0;
}
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
// 反转
reverse(v.begin(), v.end());
// 去重(需要先排序)
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
// 求和
int sum = accumulate(v.begin(), v.end(), 0);
// 最大值和最小值
int maxVal = *max_element(v.begin(), v.end());
int minVal = *min_element(v.begin(), v.end());
return 0;
}
  • 优先使用 vector,除非有特殊需求
  • 使用 auto 关键字简化迭代器类型
  • 掌握常用算法,提高编程效率
  • 注意容器的性能特性(如 vector 的随机访问是 O(1),而 list 的插入是 O(1))