C++初學者指南:輕松掌握STL容器與算法
STL(Standard Template Library)是C++标准库的一部分,旨在提供通用、高效、可复用的算法和数据结构,旨在简化C++编程,支持泛型编程并实现并行、跨平台解决方案。STL包括多种容器如vector
、list
和deque
,以及算法如sort
、reverse
和unique
,还有迭代器和模板,为开发者提供强大的工具集,以实现在C++中高效处理数据的功能。
STL(Standard Template Library)是C++标准库的一部分,旨在提供通用、高效、可复用的算法和数据结构。其设计目标是提供一种并行、跨平台的解决方案,以满足在C++程序中高效处理数据的需求。STL的历史可以追溯到20世纪80年代,由Bjarne Stroustrup和C++团队在C++初版中引入,旨在补充和支持C++语言的泛型编程特性。
STL容器
1. vector
容器
vector
是一种动态大小的数组,它支持随机访问,且内部使用数组实现。vector
在插入、删除元素时可能会导致内存调整,这使得它在大多数情况下具有良好的性能。
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums; // 创建一个空的vector
nums.push_back(10); // 添加元素
nums.push_back(20);
nums.push_back(30);
std::cout << "Vector size: " << nums.size() << std::endl;
std::cout << "Vector elements: ";
for (int i = 0; i < nums.size(); ++i) {
std::cout << nums[i] << " ";
}
std::cout << std::endl;
return 0;
}
2. list
容器
list
是一种链表实现,支持双向随机访问。list
在插入、删除元素时操作较快,但其空间利用率较低,因为需要额外的指针存储元素间的链接。
#include <iostream>
#include <list>
int main() {
std::list<int> nums;
nums.push_back(10);
nums.push_back(20);
nums.push_back(30);
std::cout << "List elements: ";
for (std::list<int>::iterator it = nums.begin(); it != nums.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
nums.pop_front(); // 移除头部元素
nums.pop_back(); // 移除尾部元素
std::cout << "List elements after removals: ";
for (std::list<int>::iterator it = nums.begin(); it != nums.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
3. deque
容器
deque
(double-ended queue)是一种两端都能插入和删除元素的动态数组实现。它在两端的性能与vector
相当,但在中间的性能则不如vector
。
#include <iostream>
#include <deque>
int main() {
std::deque<int> nums;
nums.push_back(10); // 在一端添加元素
nums.push_front(20); // 在另一端添加元素
nums.push_back(30);
nums.push_front(40);
std::cout << "Deque elements: ";
for (std::deque<int>::iterator it = nums.begin(); it != nums.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
nums.pop_back(); // 从一端移除元素
nums.pop_front(); // 从另一端移除元素
std::cout << "Deque elements after removals: ";
for (std::deque<int>::iterator it = nums.begin(); it != nums.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
STL算法
1. sort
算法
sort
算法用于对容器中的元素进行排序,可以使用多种排序算法(例如快速排序、归并排序等)实现。
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(nums.begin(), nums.end()); // 对vector进行排序
std::cout << "Sorted list: ";
for (int num : nums) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
2. reverse
算法
reverse
算法用于反转容器中元素的顺序。
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::reverse(nums.begin(), nums.end()); // 反转vector
std::cout << "Reversed list: ";
for (int num : nums) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
3. unique
算法
unique
算法用于去除容器中重复的元素并保持顺序。
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::vector<int>::iterator new_end = std::unique(nums.begin(), nums.end());
nums.erase(new_end, nums.end()); // 去除重复元素
std::cout << "List with unique elements: ";
for (int num : nums) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
STL迭代器
迭代器是访问容器中元素的一种方式,可以用于遍历容器、执行操作等。C++ STL提供了多种迭代器类型,包括前向迭代器、双向迭代器和随机访问迭代器等。
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5};
for (std::vector<int>::iterator it = nums.begin(); it != nums.end(); ++it) {
std::cout << "Element at " << it - nums.begin() << ": " << *it << std::endl;
}
return 0;
}
STL模板
模板在STL中用于创建通用的、类型安全的算法和容器。模板允许在运行时确定函数或类的特定类型和参数,从而实现代码的复用和泛型编程。
#include <iostream>
#include <string>
template <typename T>
void print(const T& item) {
std::cout << item << std::endl;
}
int main() {
print("Hello, World!"); // 对字符串使用模板
print(42); // 对整数使用模板
return 0;
}
实践案例
假设我们需要为一个图书管理系统开发一个功能,该功能需要根据图书的作者或标题进行排序、查找和删除操作。可以使用STL容器和算法来实现这个功能。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct Book {
std::string title;
std::string author;
double price;
};
bool compareByAuthor(const Book& a, const Book& b) {
return a.author < b.author;
}
bool compareByTitle(const Book& a, const Book& b) {
return a.title < b.title;
}
int main() {
std::vector<Book> books = {
{"The Alchemist", "Paulo Coelho", 15.99},
{"1984", "George Orwell", 9.99},
{"To Kill a Mockingbird", "Harper Lee", 12.99}
};
// 按作者排序
std::sort(books.begin(), books.end(), compareByAuthor);
std::cout << "Books sorted by author:" << std::endl;
for (const auto& book : books) {
std::cout << "Title: " << book.title << ", Author: " << book.author << std::endl;
}
// 查找标题为"The Alchemist"的书籍
auto it = std::find_if(books.begin(), books.end(), [title = "The Alchemist"](const Book& book) {
return book.title == title;
});
if (it != books.end()) {
std::cout << "Found book: Title: " << it->title << ", Author: " << it->author << std::endl;
} else {
std::cout << "Book not found." << std::endl;
}
// 删除标题为"1984"的书籍
auto erase_it = std::find_if(books.begin(), books.end(), [title = "1984"](const Book& book) {
return book.title == title;
});
if (erase_it != books.end()) {
books.erase(erase_it);
}
std::cout << "Books after removal:" << std::endl;
for (const auto& book : books) {
std::cout << "Title: " << book.title << ", Author: " << book.author << std::endl;
}
return 0;
}
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章