【常用技巧】C++ STL容器操作:6种常用场景算法

本文涉及的产品
可观测监控 Prometheus 版,每月50GB免费额度
函数计算FC,每月15万CU 3个月
性能测试 PTS,5000VUM额度
简介: STL在Linux C++中使用的非常普遍,掌握并合适的使用各种容器至关重要!

C++ STL容器操作:6种常用场景算法

引言

  在日常软件开发工作中,容器操作如查找、排序、和元素替换等任务极为常见。幸运的是,C++的标准化利器——STL(Standard Template Library,标准模板库)为我们封装了一系列高效且灵活的算法,极大简化了这些操作。特别是自C++11版本以来,STL引入了一批新晋的通用算法,它们不仅功能强大,而且设计得更加人性化,旨在进一步提升我们的编码效率与程序性能。本文列举一些常用到的方法,便于日后回顾和查阅。

概述

  日常开发中的容器操作,如查找、删除、排序、分割、拷贝和合并,均可借助STL中的精选算法高效完成。本文将概览这些核心STL算法,助力开发流程优化。

查找与计数

std::find

  • 作用: 找容器中第一个匹配给定值的元素
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
      DumpVector("testArrgs", testArrgs);
    
      auto find = std::find(testArrgs.begin(), testArrgs.end(), "XiaoMing");
      if (find != testArrgs.end()) {
          SPR_LOGD("find: dest = \"XiaoMing\", result = %s, index = %ld\n", find->c_str(), find - testArrgs.begin());
      }
    
  • 结果
    D: find: dest = "XiaoMing", result = XiaoMing, index = 2
    

std::find_if

  • 作用: 查找容器中第一个满足谓词条件的元素
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      auto findIf = std::find_if(testArrgs.begin(), testArrgs.end(),  [](const std::string &str) { return str.find("Ming") != std::string::npos; });
      if (findIf != testArrgs.end()) {
          SPR_LOGD("find_if: dest = \"Ming\", result = %s, index = %ld\n", findIf->c_str(), findIf - testArrgs.begin());
      }
    

    ③ 结果

    D: find_if: dest = "Ming", result = XiaoMing, index = 2
    

std::find_if_not

  • 作用: 寻找第一个不符合指定条件的元素
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      auto findIfNot = std::find_if_not(testArrgs.begin(), testArrgs.end(),  [](const std::string &str) { return str.find("Xiao") != std::string::npos; });
      if (findIfNot != testArrgs.end()) {
          SPR_LOGD("find_if_not: dest = \"Xiao\", result = %s, index = %ld\n", findIfNot->c_str(), findIfNot - testArrgs.begin());
      }
    
  • 结果
    D: find_if_not: dest = "Xiao", result = LiHua, index = 1
    

std::find_end

  • 作用: 查找一个子序列最后一次出现的位置
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> subArrgs1 = {"XiaoMing", "XiaoHong"};
      auto findEnd = std::find_end(testArrgs.begin(), testArrgs.end(), subArrgs1.begin(), subArrgs1.end());
      if (findEnd != testArrgs.end()) {
          SPR_LOGD("find_end: dest = {\"XiaoMing\", \"XiaoHong\"}, result = %s, index = %ld\n", findEnd->c_str(), findEnd - testArrgs.begin());
      }
    
  • 结果
    D: find_end: dest = {"XiaoMing", "XiaoHong"}, result = XiaoMing, index = 3
    

std::find_first_of

  • 作用: 查找序列中任何一个元素首次出现的位置
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> subArrgs2 = {"JianGuo"};
      auto findFirstOf = std::find_first_of(testArrgs.begin(), testArrgs.end(), subArrgs2.begin(), subArrgs2.end());
      if (findFirstOf != testArrgs.end()) {
          SPR_LOGD("find_first_of: dest = {\"JianGuo\"}, result = %s, index = %ld\n", findFirstOf->c_str(), findFirstOf - testArrgs.begin());
      }
    
  • 结果
    D: find_first_of: dest = {"JianGuo"}, result = JianGuo, index = 5
    

std::adjacent_find

  • 作用: 查找相邻重复的元素
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      auto adjacentFind = std::adjacent_find(testArrgs.begin(), testArrgs.end(), [](const std::string &str1, const std::string &str2) { return str1 == str2; });
      if (adjacentFind != testArrgs.end()) {
          SPR_LOGD("adjacent_find: result = %s, index = %ld\n", adjacentFind->c_str(), adjacentFind - testArrgs.begin());
      }
    
  • 结果
    D: adjacent_find: result = XiaoMing, index = 2
    

std::count

  • 作用: 计算容器中指定值出现的次数
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      auto cnt = std::count(testArrgs.begin(), testArrgs.end(), "XiaoFang");
      if (cnt > 0) {
          SPR_LOGD("count: dest = \"XiaoFang\", result = %ld\n", cnt);
      }
    
  • 结果
    D: count: dest = "XiaoFang", result = 1
    

std::count_if

  • 作用: 计算满足谓词条件的元素数量
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      auto cntIf = std::count_if(testArrgs.begin(), testArrgs.end(), [](const std::string &str) { return str.find("Xiao") != std::string::npos; });
      if (cntIf > 0) {
          SPR_LOGD("count_if: dest = \"Xiao\", result = %ld\n", cntIf);
      }
    
  • 结果
    D: count_if: dest = "Xiao", result = 6
    

移除与替换

std::remove

  • 作用: 重新排列容器元素,使得所有不匹配特定值的元素被放置在容器的前端。配合earse实现移除不匹配元素。
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> rmArrgs(testArrgs);
      auto remove = std::remove(rmArrgs.begin(), rmArrgs.end(), "XiaoMing");
      if (remove != rmArrgs.end()) {
          rmArrgs.erase(remove, rmArrgs.end());
          DumpVector("rmArrgs", rmArrgs);
      }
    
  • 结果
    D: rmArrgs: "XiaoFang", "LiHua", "XiaoHong", "JianGuo", "XiaoFangFang",
    

std::remove_if

  • 作用: 重新排列容器元素,使得所有不满足谓词条件的元素被放置在容器的前端。配合earse实现移除不匹配元素。
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> rmIfArrgs(testArrgs);
      auto removeIf = std::remove_if(rmIfArrgs.begin(), rmIfArrgs.end(), [](const std::string &str) { return str.find("Xiao") != std::string::npos; });
      if (removeIf != rmIfArrgs.end()) {
          rmIfArrgs.erase(removeIf, rmIfArrgs.end());
          DumpVector("rmArrgs", rmArrgs);
      }
    
  • 结果
    D: rmIfArrgs: "XiaoFang", "LiHua", "XiaoHong", "JianGuo", "XiaoFangFang"
    

std::remove_copy

  • 作用: 从源序列拷贝元素到目标序列,排除满足条件的元素。
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> dstArrgs;
      vector<std::string> rcArrgs(testArrgs);
      std::remove_copy(rcArrgs.begin(), rcArrgs.end(), std::back_inserter(dstArrgs), "XiaoMing");
      DumpVector("dstArrgs", dstArrgs);
    
  • 结果
    D: dstArrgs: "XiaoFang", "LiHua", "XiaoHong", "JianGuo", "XiaoFangFang"
    

std::replace

  • 作用: 将所有出现的指定元素替换为新值。
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> rpArrgs(testArrgs);
      std::replace(rpArrgs.begin(), rpArrgs.end(), "XiaoMing", "MingMing");
      DumpVector("rpArrgs", rpArrgs);
    
  • 结果
    D: rpArrgs: "XiaoFang", "LiHua", "MingMing", "MingMing", "XiaoHong", "JianGuo", "XiaoFangFang", "MingMing",
    

std::replace_if

  • 作用: 满足谓词条件的元素,替换为指定的替换值。
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> rpIfArrgs(testArrgs);
      std::replace_if(rpIfArrgs.begin(), rpIfArrgs.end(), [](const std::string &str) { return (str == "XiaoFang"); }, "FangFang");
      DumpVector("rpIfArrgs", rpIfArrgs);
    
  • 结果
    D: rpIfArrgs: "FangFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing",
    

std::replace_copy

  • 作用:用于在指定范围内将所有匹配某个值的元素替换为另一个值,拷贝到另一个的容器中。
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> rpCpDstArrgs;
      vector<std::string> rpCopyArrgs(testArrgs);
      std::replace_copy(rpCopyArrgs.begin(), rpCopyArrgs.end(), std::back_inserter(rpCpDstArrgs), "XiaoMing", "MingMing");
      DumpVector("rpCpDstArrgs", rpCpDstArrgs);
    
  • 结果
    D: rpCpDstArrgs: "XiaoFang", "LiHua", "MingMing", "MingMing", "XiaoHong", "JianGuo", "XiaoFangFang", "MingMing"
    

std::swap

  • 作用:交换两个变量的值。
  • 示例

      vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
    
      vector<std::string> swapSrcArrgs(testArrgs);
      vector<std::string> swapDstArrgs = {"Vince Carter", "Rose", "James"};
      std::swap(swapSrcArrgs, swapDstArrgs);
      DumpVector("swapSrcArrgs", swapSrcArrgs);
      DumpVector("swapDstArrgs", swapDstArrgs);
    
  • 结果
    D: swapSrcArrgs: "Vince Carter", "Rose", "James", 
    D: swapDstArrgs: "XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing",
    

排序与组织

std::sort

  • 作用:对容器元素进行排序。
  • 示例
    ```c++
    vector testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};

    // sort
    vector sortArrgs(testArrgs);
    std::sort(sortArrgs.begin(), sortArrgs.end());
    DumpVector("sortArrgs", sortArrgs);
    std::sort(sortArrgs.begin(), sortArrgs.end(), { return str1.size() > str2.size(); });
    DumpVector("sortArrgs", sortArrgs);

* 结果
```c++
  D: sortArrgs: "JianGuo", "LiHua", "XiaoFang", "XiaoFangFang", "XiaoHong", "XiaoMing", "XiaoMing", "XiaoMing", 
  D: sortArrgs: "XiaoFangFang", "XiaoFang", "XiaoHong", "XiaoMing", "XiaoMing", "XiaoMing", "JianGuo", "LiHua",

std::stable_sort

  • 作用:稳定排序,保持相等元素的原有顺序。
  • 示例
      // stable_sort
      vector<Persion> srcPersions = {
        {"LiWei", 18}, {"LiHua", 20}, {"ZhangLei", 19}, {"WeiGuo", 19}, {"ZhenHua", 20}, {"JianGuo", 18}, {"ZhiGuo", 19}, {"JianHua", 19}};
      vector<Persion> sPersions(srcPersions);
      std::stable_sort(sPersions.begin(), sPersions.end(), [](const Persion &p1, const Persion &p2) { return p1.age > p2.age; });
      DumpVector("sPersions", sPersions);
      std::stable_sort(sPersions.begin(), sPersions.end(), [](const Persion &p1, const Persion &p2) { return p1.age < p2.age; });
      DumpVector("sPersions", sPersions);
    
  • 结果
    D: sPersions = { {"LiHua", 20}, {"ZhenHua", 20}, {"ZhangLei", 19}, {"WeiGuo", 19}, {"ZhiGuo", 19}, {"JianHua", 19}, {"LiWei", 18}, {"JianGuo", 18}, }
    D: sPersions = { {"LiWei", 18}, {"JianGuo", 18}, {"ZhangLei", 19}, {"WeiGuo", 19}, {"ZhiGuo", 19}, {"JianHua", 19}, {"LiHua", 20}, {"ZhenHua", 20}, }
    

std::partial_sort

  • 作用:对部分区间排序。
  • 示例

      vector<Persion> srcPersions = {
        {"LiWei", 18}, {"LiHua", 20}, {"ZhangLei", 19}, {"WeiGuo", 19}, {"ZhenHua", 20}, {"JianGuo", 18}, {"ZhiGuo", 19}, {"JianHua", 19}};
    
      // partial_sort
      vector<Persion> prtPersions(srcPersions);
      vector<Persion> sortPersions(srcPersions);
      std::partial_sort(prtPersions.begin(), prtPersions.begin() + 5, prtPersions.end(), [](const Persion &p1, const Persion &p2) { return p1.age > p2.age; });
      std::sort(sortPersions.begin(), sortPersions.begin() + 5, [](const Persion &p1, const Persion &p2) { return p1.age > p2.age; });
      DumpVector("prtPersions", prtPersions);
      DumpVector("sortPersions", sortPersions);
    
  • 结果
    D: prtPersions = { {"ZhenHua", 20}, {"LiHua", 20}, {"WeiGuo", 19}, {"ZhiGuo", 19}, {"ZhangLei", 19}, {"JianGuo", 18}, {"LiWei", 18}, {"JianHua", 19}, }
    D: sortPersions = { {"LiHua", 20}, {"ZhenHua", 20}, {"ZhangLei", 19}, {"WeiGuo", 19}, {"LiWei", 18}, {"JianGuo", 18}, {"ZhiGuo", 19}, {"JianHua", 19}, }
    

std::reverse

  • 作用:对部分区间排序。
  • 示例

      vector<Persion> srcPersions = {
        {"LiWei", 18}, {"LiHua", 20}, {"ZhangLei", 19}, {"WeiGuo", 19}, {"ZhenHua", 20}, {"JianGuo", 18}, {"ZhiGuo", 19}, {"JianHua", 19}};
    
      // reverse
      vector<Persion> rPersions(srcPersions);
      std::reverse(rPersions.begin(), rPersions.end());
      DumpVector("rPersions", rPersions);
    
  • 结果
    D: rPersions = { {"JianHua", 19}, {"ZhiGuo", 19}, {"JianGuo", 18}, {"ZhenHua", 20}, {"WeiGuo", 19}, {"ZhangLei", 19}, {"LiHua", 20}, {"LiWei", 18}, }
    

std::rotate

  • 作用:对部分区间排序。
  • 示例

      vector<Persion> srcPersions = {
        {"LiWei", 18}, {"LiHua", 20}, {"ZhangLei", 19}, {"WeiGuo", 19}, {"ZhenHua", 20}, {"JianGuo", 18}, {"ZhiGuo", 19}, {"JianHua", 19}};
    
      // rotate
      vector<Persion> rtPersions(srcPersions);
      std::rotate(rtPersions.begin(), rtPersions.begin() + 3, rtPersions.end());
      DumpVector("rtPersions", rtPersions);
    
  • 结果
    D: rPersions = { {"JianHua", 19}, {"ZhiGuo", 19}, {"JianGuo", 18}, {"ZhenHua", 20}, {"WeiGuo", 19}, {"ZhangLei", 19}, {"LiHua", 20}, {"LiWei", 18}, }
    

分割

std::partition

  • 作用:分割容器,使满足谓词的元素在前,不满足的在后。
  • 示例

      vector<std::string> testArrgs = {"LiHua", "ZhangHong", "HeDa", "LiWei", "ZhangLei", "WeiGuo", "ZhenHua"};
    
      // partition
      vector<std::string> partArrgs(testArrgs);
      std::partition(partArrgs.begin(), partArrgs.end(), [](const std::string& str) { return (str.compare(0, 5, "Zhang") == 0); });
      DumpVector("partArrgs", partArrgs);
    
  • 结果
    D: partArrgs: "ZhangLei", "ZhangHong", "HeDa", "LiWei", "LiHua", "WeiGuo", "ZhenHua",
    

std::stable_partition

  • 作用:类似partition,但保持相同元素的相对顺序不变。
  • 示例

      vector<std::string> testArrgs = {"LiHua", "ZhangHong", "HeDa", "LiWei", "ZhangLei", "WeiGuo", "ZhenHua"};
    
      // stable_partition
      vector<std::string> stPartArrgs(testArrgs);
      std::stable_partition(stPartArrgs.begin(), stPartArrgs.end(), [](const std::string& str) { return (str.compare(0, 5, "Zhang") == 0); });
      DumpVector("stPartArrgs", stPartArrgs);
    
  • 结果
    D: stPartArrgs: "ZhangHong", "ZhangLei", "LiHua", "HeDa", "LiWei", "WeiGuo", "ZhenHua",
    

拷贝与生成

std::copy

  • 作用:拷贝容器或范围内的元素到另一位置。
  • 示例

      vector<std::string> srcArrgs = {"LiHua", "ZhangHong", "HeDa", "LiWei", "ZhangLei", "WeiGuo", "ZhenHua"};
    
      // copy
      vector<std::string> dstCopyArrgs;
      std::copy(srcArrgs.begin(), srcArrgs.end(), std::back_inserter(dstCopyArrgs));
      DumpVector("dstCopyArrgs", dstCopyArrgs);
    
  • 结果
    D: dstCopyArrgs: "LiHua", "ZhangHong", "HeDa", "LiWei", "ZhangLei", "WeiGuo", "ZhenHua",
    

std::copy_if

  • 作用:根据谓词条件拷贝元素。
  • 示例

      vector<std::string> srcArrgs = {"LiHua", "ZhangHong", "HeDa", "LiWei", "ZhangLei", "WeiGuo", "ZhenHua"};
    
      // copy_if
      vector<std::string> dstCopyIfArrgs;
      std::copy_if(srcArrgs.begin(), srcArrgs.end(), std::back_inserter(dstCopyIfArrgs), [](const std::string& str) { return (str.compare(0, 5, "Zhang") == 0); });
      DumpVector("dstCopyIfArrgs", dstCopyIfArrgs);
    
  • 结果
    D: dstCopyIfArrgs: "ZhangHong", "ZhangLei",
    

std::copy_n

  • 作用:拷贝指定数量的元素。
  • 示例

      vector<std::string> srcArrgs = {"LiHua", "ZhangHong", "HeDa", "LiWei", "ZhangLei", "WeiGuo", "ZhenHua"};
    
      // copy_n
      vector<std::string> dstCopyNArrgs;
      std::copy_n(srcArrgs.begin(), 3, std::back_inserter(dstCopyNArrgs));
      DumpVector("dstCopyNArrgs", dstCopyNArrgs);
    
  • 结果
    D: dstCopyNArrgs: "LiHua", "ZhangHong", "HeDa",
    

std::generate

  • 作用:使用生成器函数填充容器。
  • 示例
      // generate
      vector<std::string> genArrgs(5);
      std::generate(genArrgs.begin(), genArrgs.end(), [](){ return "Hello"; });
      DumpVector("genArrgs", genArrgs);
    
  • 结果
    D: genArrgs: "Hello", "Hello", "Hello", "Hello", "Hello",
    

std::generate_n

  • 作用:生成指定数量的元素填充容器。
  • 示例
      // generate_n
      vector<std::string> genNArrgs;
      std::generate_n(std::back_inserter(genNArrgs), 3, [](){ return "Hello"; });
      DumpVector("genNArrgs", genNArrgs);
    
  • 结果
    D: genNArrgs: "Hello", "Hello", "Hello",
    

集合操作

std::merge

  • 作用:生成指定数量的元素填充容器。
  • 示例

      vector<std::string> srcArrgs = {"Vince Carter", "Rose", "James"};
    
      // merge
      vector<std::string> mergeArrgs;
      vector<std::string> tmpArrgs = {"Irving"};
      std::merge(srcArrgs.begin(), srcArrgs.end(), tmpArrgs.begin(), tmpArrgs.end(), std::back_inserter(mergeArrgs));
      DumpVector("mergeArrgs", mergeArrgs);
    
  • 结果
    D: mergeArrgs: "Irving", "Vince Carter", "Rose", "James",
    

std::inplace_merge

  • 作用:就地归并两个有序范围。
  • 示例

      vector<std::string> srcArrgs = {"Vince Carter", "Rose", "James"};
    
      // inplace_merge
      vector<std::string> inplaceMergeArrgs(srcArrgs);
      std::inplace_merge(inplaceMergeArrgs.begin(), inplaceMergeArrgs.begin() + 2, inplaceMergeArrgs.end());
      DumpVector("inplaceMergeArrgs", inplaceMergeArrgs);
    
  • 结果

    D: inplaceMergeArrgs: "James", "Vince Carter", "Rose",
    
相关文章
|
4天前
|
存储 算法 测试技术
【C++数据结构——树】二叉树的遍历算法(头歌教学实验平台习题) 【合集】
本任务旨在实现二叉树的遍历,包括先序、中序、后序和层次遍历。首先介绍了二叉树的基本概念与结构定义,并通过C++代码示例展示了如何定义二叉树节点及构建二叉树。接着详细讲解了四种遍历方法的递归实现逻辑,以及层次遍历中队列的应用。最后提供了测试用例和预期输出,确保代码正确性。通过这些内容,帮助读者理解并掌握二叉树遍历的核心思想与实现技巧。
21 2
|
13天前
|
存储 算法 安全
基于红黑树的局域网上网行为控制C++ 算法解析
在当今网络环境中,局域网上网行为控制对企业和学校至关重要。本文探讨了一种基于红黑树数据结构的高效算法,用于管理用户的上网行为,如IP地址、上网时长、访问网站类别和流量使用情况。通过红黑树的自平衡特性,确保了高效的查找、插入和删除操作。文中提供了C++代码示例,展示了如何实现该算法,并强调其在网络管理中的应用价值。
|
11天前
|
人工智能 运维 监控
容器服务Kubernetes场景下可观测体系生产级最佳实践
阿里云容器服务团队在2024年继续蝉联Gartner亚洲唯一全球领导者象限,其可观测体系是运维的核心能力之一。该体系涵盖重保运维、大规模集群稳定性、业务异常诊断等场景,特别是在AI和GPU场景下提供了全面的观测解决方案。通过Tracing、Metric和Log等技术,阿里云增强了对容器网络、存储及多集群架构的监控能力,帮助客户实现高效运维和成本优化。未来,结合AI助手,将进一步提升问题定位和解决效率,缩短MTTR,助力构建智能运维体系。
|
11天前
|
存储 算法 安全
基于哈希表的文件共享平台 C++ 算法实现与分析
在数字化时代,文件共享平台不可或缺。本文探讨哈希表在文件共享中的应用,包括原理、优势及C++实现。哈希表通过键值对快速访问文件元数据(如文件名、大小、位置等),查找时间复杂度为O(1),显著提升查找速度和用户体验。代码示例展示了文件上传和搜索功能,实际应用中需解决哈希冲突、动态扩容和线程安全等问题,以优化性能。
|
15天前
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
19 1
|
18天前
|
算法 安全 C++
用 C++ 算法控制员工上网的软件,关键逻辑是啥?来深度解读下
在企业信息化管理中,控制员工上网的软件成为保障网络秩序与提升办公效率的关键工具。该软件基于C++语言,融合红黑树、令牌桶和滑动窗口等算法,实现网址精准过滤、流量均衡分配及异常连接监测。通过高效的数据结构与算法设计,确保企业网络资源优化配置与安全防护升级,同时尊重员工权益,助力企业数字化发展。
38 4
|
27天前
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
47 7
|
1月前
|
缓存 Serverless 调度
Moka:ACS 容器计算服务助力CI/CD场景提效降本 70%
Moka,作为国内发展最快的HR SaaS厂商之一,在ACS产品初期就与阿里云产品团队紧密联系产品需求及体验反馈,本文将着重分享我们使用ACS在CI/CD 场景的最佳实践。
|
1月前
|
人工智能 Cloud Native 调度
阿里云容器服务在AI智算场景的创新与实践
本文源自张凯在2024云栖大会的演讲,介绍了阿里云容器服务在AI智算领域的创新与实践。从2018年推出首个开源GPU容器共享调度方案至今,阿里云容器服务不断推进云原生AI的发展,包括增强GPU可观测性、实现多集群跨地域统一调度、优化大模型推理引擎部署、提供灵活的弹性伸缩策略等,旨在为客户提供高效、低成本的云原生AI解决方案。
|
2月前
|
存储 编译器 C语言
【c++丨STL】vector的使用
本文介绍了C++ STL中的`vector`容器,包括其基本概念、主要接口及其使用方法。`vector`是一种动态数组,能够根据需要自动调整大小,提供了丰富的操作接口,如增删查改等。文章详细解释了`vector`的构造函数、赋值运算符、容量接口、迭代器接口、元素访问接口以及一些常用的增删操作函数。最后,还展示了如何使用`vector`创建字符串数组,体现了`vector`在实际编程中的灵活性和实用性。
82 4