C++ STL容器操作:6种常用场景算法
[TOC]
引言
在日常软件开发工作中,容器操作如查找、排序、和元素替换等任务极为常见。幸运的是,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
- 作用:对容器元素进行排序。
- 示例
vector<std::string> testArrgs = {"XiaoFang", "LiHua", "XiaoMing", "XiaoMing", "XiaoHong", "JianGuo", "XiaoFangFang", "XiaoMing"};
// sort
vector<std::string> sortArrgs(testArrgs);
std::sort(sortArrgs.begin(), sortArrgs.end());
DumpVector("sortArrgs", sortArrgs);
std::sort(sortArrgs.begin(), sortArrgs.end(), [](const std::string &str1, const std::string &str2) { return str1.size() > str2.size(); });
DumpVector("sortArrgs", sortArrgs);
- 结果
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",