c++STL容器的使用(vector, list, map, set等),c++STL算法的理解与使用(sort, find, binary_search等)

c++STL容器的使用(vector, list, map, set等)

在C++的STL(Standard Template Library)中,容器是重要的一部分,它们提供了各种数据结构来存储和管理数据。以下是一些常见的STL容器及其使用方法的简要说明:

  1. Vector(向量)
    std::vector 是一个动态数组,可以动态地添加和删除元素。
cpp 复制代码
#include <vector>  
#include <iostream>  
  
int main() {  
    // 创建一个空的vector  
    std::vector<int> vec;  
  
    // 向vector中添加元素  
    vec.push_back(1);  
    vec.push_back(2);  
    vec.push_back(3);  
  
    // 访问元素  
    std::cout << "First element: " << vec[0] << std::endl;  
  
    // 遍历vector  
    for (const auto& elem : vec) {  
        std::cout << elem << " ";  
    }  
  
    // 删除元素  
    vec.pop_back(); // 删除最后一个元素  
  
    return 0;  
}
  1. List(列表)
    std::list 是一个双向链表,提供了高效的插入和删除操作。
cpp 复制代码
#include <list>  
#include <iostream>  
  
int main() {  
    // 创建一个空的list  
    std::list<int> lst;  
  
    // 向list中添加元素  
    lst.push_back(1);  
    lst.push_front(0);  
  
    // 遍历list  
    for (const auto& elem : lst) {  
        std::cout << elem << " ";  
    }  
  
    // 删除元素  
    lst.remove(1); // 删除所有值为1的元素  
  
    return 0;  
}
  1. Map(映射)
    std::map 是一个关联容器,它存储的元素是键值对,并按键进行排序。
cpp 复制代码
#include <map>  
#include <iostream>  
  
int main() {  
    // 创建一个空的map  
    std::map<std::string, int> m;  
  
    // 向map中添加元素  
    m["apple"] = 1;  
    m["banana"] = 2;  
  
    // 访问元素  
    std::cout << "Apple count: " << m["apple"] << std::endl;  
  
    // 遍历map  
    for (const auto& pair : m) {  
        std::cout << pair.first << ": " << pair.second << std::endl;  
    }  
  
    // 删除元素  
    m.erase("banana"); // 删除键为"banana"的元素  
  
    return 0;  
}
  1. Set(集合)
    std::set 是一个关联容器,它存储的元素是唯一的,并按值进行排序。
cpp 复制代码
#include <set>  
#include <iostream>  
  
int main() {  
    // 创建一个空的set  
    std::set<int> s;  
  
    // 向set中添加元素  
    s.insert(1);  
    s.insert(2);  
    s.insert(1); // 这个元素不会被插入,因为set中不允许重复  
  
    // 遍历set  
    for (const auto& elem : s) {  
        std::cout << elem << " ";  
    }  
  
    // 删除元素  
    s.erase(1); // 删除值为1的元素  
  
    return 0;  
}

在使用这些STL容器时,通常会用到一些通用的STL算法,如std::sort(排序),std::find(查找),std::for_each(对容器中每个元素执行操作)等。此外,容器还提供了很多成员函数来操作元素,如push_back,pop_back,insert,erase,begin,end等。

c++STL算法的理解与使用(sort, find, binary_search等)

C++的STL(Standard Template Library)提供了一系列算法,这些算法可以与STL容器配合使用,以执行各种常见的任务,如排序、查找和搜索。以下是对一些常见STL算法的理解与使用的简要说明:

  1. std::sort(排序)
    std::sort是一个通用排序算法,可以对任何提供随机访问迭代器的序列进行排序。
cpp 复制代码
#include <algorithm>  
#include <vector>  
#include <iostream>  
  
int main() {  
    std::vector<int> numbers = {4, 2, 5, 3, 1};  
  
    // 使用std::sort对vector进行排序  
    std::sort(numbers.begin(), numbers.end());  
  
    // 输出排序后的vector  
    for (const auto& num : numbers) {  
        std::cout << num << " ";  
    }  
  
    return 0;  
}
  1. std::find(查找)
    std::find用于在序列中查找特定元素。如果找到,则返回指向该元素的迭代器;否则,返回序列的尾迭代器。
cpp 复制代码
#include <algorithm>  
#include <vector>  
#include <iostream>  
  
int main() {  
    std::vector<int> numbers = {1, 2, 3, 4, 5};  
    int target = 3;  
  
    // 使用std::find查找target  
    auto it = std::find(numbers.begin(), numbers.end(), target);  
  
    if (it != numbers.end()) {  
        std::cout << "Found " << target << " at position: " << std::distance(numbers.begin(), it) << std::endl;  
    } else {  
        std::cout << target << " not found" << std::endl;  
    }  
  
    return 0;  
}
  1. std::binary_search(二分查找)
    std::binary_search用于在已排序的序列中执行二分查找。它要求序列必须是已排序的,否则结果将是未定义的。
cpp 复制代码
#include <algorithm>  
#include <vector>  
#include <iostream>  
  
int main() {  
    std::vector<int> numbers = {1, 2, 3, 4, 5};  
    int target = 3;  
  
    // 确保序列已排序  
    std::sort(numbers.begin(), numbers.end());  
  
    // 使用std::binary_search进行查找  
    bool found = std::binary_search(numbers.begin(), numbers.end(), target);  
  
    if (found) {  
        std::cout << target << " found in the sorted sequence" << std::endl;  
    } else {  
        std::cout << target << " not found" << std::endl;  
    }  
  
    return 0;  
}

使用注意事项:

std::sort默认使用<操作符来比较元素,但也可以传递自定义比较函数或lambda表达式。

std::find和std::binary_search返回的都是迭代器,需要与容器的开始迭代器比较来确定是否找到了目标元素。

std::binary_search要求序列必须是已排序的,否则会返回未定义的结果。

这些算法都提供了很大的灵活性,因为它们是模板化的,可以与任何类型的容器一起使用,只要这些容器提供适当的迭代器类型。此外,它们也可以很容易地与C++的lambda表达式结合使用,以提供自定义的比较逻辑。

相关推荐
2401_892070981 天前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
yuzhuanhei1 天前
Visual Studio 配置C++opencv
c++·学习·visual studio
小O的算法实验室1 天前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
‎ദ്ദിᵔ.˛.ᵔ₎1 天前
LIST 的相关知识
数据结构·list
不爱吃炸鸡柳1 天前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发1 天前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
‎ദ്ദിᵔ.˛.ᵔ₎1 天前
STL 栈 队列
开发语言·c++
2401_892070981 天前
【Linux C++ 日志系统实战】高性能文件写入 AppendFile 核心方法解析
linux·c++·日志系统·文件写对象
郭涤生1 天前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法