您的位置:首页 > 汽车 > 时评 > cms建站模板app_网站建设方案平台架构_关键词工具软件_网站优化排名公司哪家好

cms建站模板app_网站建设方案平台架构_关键词工具软件_网站优化排名公司哪家好

2025/2/13 10:27:53 来源:https://blog.csdn.net/qq_44494578/article/details/145533537  浏览:    关键词:cms建站模板app_网站建设方案平台架构_关键词工具软件_网站优化排名公司哪家好
cms建站模板app_网站建设方案平台架构_关键词工具软件_网站优化排名公司哪家好

Algorithm库概述

<algorithm>库中的算法主要分为以下几类:

  1. 非修改序列操作:对序列进行遍历、查找、计数等操作,但不修改序列中的元素。
  2. 修改序列操作:对序列中的元素进行复制、替换、删除、旋转等操作。
  3. 排序和相关操作:对序列进行排序、合并、二分查找等操作。
  4. 数值操作:对序列中的元素进行数值计算,如累加、内积等。

常用算法详解

1. 非修改序列操作

std::for_each

std::for_each用于对序列中的每个元素执行指定的操作。

#include <iostream>
#include <vector>
#include <algorithm>void print(int n) {std::cout << n << " ";
}int main() {std::vector<int> vec = {1, 2, 3, 4, 5};std::for_each(vec.begin(), vec.end(), print); // 输出:1 2 3 4 5return 0;
}
std::find

std::find用于在序列中查找指定的值,返回指向该元素的迭代器。如果未找到,则返回end()迭代器。

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> vec = {1, 2, 3, 4, 5};auto it = std::find(vec.begin(), vec.end(), 3);if (it != vec.end()) {std::cout << "Found: " << *it << '\n'; // 输出:Found: 3} else {std::cout << "Not found\n";}return 0;
}

2. 修改序列操作

std::copy

std::copy用于将一个序列中的元素复制到另一个序列中。

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> src = {1, 2, 3, 4, 5};std::vector<int> dst(5);std::copy(src.begin(), src.end(), dst.begin());for (int n : dst) {std::cout << n << " "; // 输出:1 2 3 4 5}return 0;
}
std::replace

std::replace用于将序列中所有等于指定值的元素替换为另一个值。

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> vec = {1, 2, 3, 2, 5};std::replace(vec.begin(), vec.end(), 2, 99);for (int n : vec) {std::cout << n << " "; // 输出:1 99 3 99 5}return 0;
}

3. 排序和相关操作

std::sort

std::sort用于对序列中的元素进行排序。

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> vec = {5, 3, 1, 4, 2};std::sort(vec.begin(), vec.end());for (int n : vec) {std::cout << n << " "; // 输出:1 2 3 4 5}return 0;
}
std::binary_search

std::binary_search用于在已排序的序列中进行二分查找。

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> vec = {1, 2, 3, 4, 5};bool found = std::binary_search(vec.begin(), vec.end(), 3);std::cout << "Found: " << std::boolalpha << found << '\n'; // 输出:Found: truereturn 0;
}

4. 数值操作

std::accumulate

std::accumulate用于计算序列中元素的累加和。

#include <iostream>
#include <vector>
#include <numeric>int main() {std::vector<int> vec = {1, 2, 3, 4, 5};int sum = std::accumulate(vec.begin(), vec.end(), 0);std::cout << "Sum: " << sum << '\n'; // 输出:Sum: 15return 0;
}
std::inner_product

std::inner_product用于计算两个序列的内积。

#include <iostream>
#include <vector>
#include <numeric>int main() {std::vector<int> vec1 = {1, 2, 3};std::vector<int> vec2 = {4, 5, 6};int result = std::inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0);std::cout << "Inner product: " << result << '\n'; // 输出:Inner product: 32return 0;
}

实际应用示例

示例1:自定义排序

我们可以通过传递自定义的比较函数来对序列进行自定义排序。

#include <iostream>
#include <vector>
#include <algorithm>bool compare(int a, int b) {return a > b; // 降序排序
}int main() {std::vector<int> vec = {5, 3, 1, 4, 2};std::sort(vec.begin(), vec.end(), compare);for (int n : vec) {std::cout << n << " "; // 输出:5 4 3 2 1}return 0;
}

示例2:删除特定元素

我们可以使用std::remove_ifstd::erase来删除序列中满足特定条件的元素。

#include <iostream>
#include <vector>
#include <algorithm>bool is_odd(int n) {return n % 2 != 0;
}int main() {std::vector<int> vec = {1, 2, 3, 4, 5};vec.erase(std::remove_if(vec.begin(), vec.end(), is_odd), vec.end());for (int n : vec) {std::cout << n << " "; // 输出:2 4}return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com