当前位置: 首页 > wzjs >正文

公需道德与能力建设培训网站便宜的seo官网优化

公需道德与能力建设培训网站,便宜的seo官网优化,网站维护员招聘,做百度网站一般多少钱STL常用算法(2) 前言简介四.常用拷贝和替换算法1.copy2.replace3.replace_if4.swap 五.算术生成算法1.accumulate2.fill 六.常用集合算法1.set_intersection2.set_union3.set_difference 总结 前言 stl系列主要讲述有关stl的文章,使用STL可以…

STL常用算法(2)

  • 前言
  • 简介
  • 四.常用拷贝和替换算法
    • 1.copy
    • 2.replace
    • 3.replace_if
    • 4.swap
  • 五.算术生成算法
    • 1.accumulate
    • 2.fill
  • 六.常用集合算法
    • 1.set_intersection
    • 2.set_union
    • 3.set_difference
  • 总结

前言

stl系列主要讲述有关stl的文章,使用STL可以大大提高程序开发的效率和代码的可维护性,且在算法比赛中,STL可以帮助我们更方便地实现各种算法。提高我们的效率。

简介

算法主要是头文件algorithm,functional,numeric组成
1.algorithm是所有STL头文件中最大的一个,范围涉及到比较,交换,查找,遍历操作,复制,修改等等
2.numeric体积很小,只包括几个在序列上面进行简单的数学运算的模板函数
3.functional定义了一些模板类,用以声明函数对象

四.常用拷贝和替换算法

copy();//将容器内指定范围的元素拷贝到另一个容器中
replace();//将容器指定范围的久元素修改为新元素
replace_if();//将容器指定范围内的久元素修改为新元素
swap();//交换两个容器的元素

1.copy

copy(first1,last,first2);//[first1,last)为被拷贝容器的范围,first2为拷贝到的容器起始位置

如:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };vector<int> v2;v2.resize(v1.size());copy(v1.begin(), v1.end(), v2.begin());for_each(v2.begin(), v2.end(), print);return 0;
}

2.replace

replace(first,last,old,new);//在first到last范围内将old元素替换成new

如:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };replace(v1.begin(), v1.end(), 1, 2);for_each(v1.begin(), v1.end(), print);return 0;
}

3.replace_if

repalce_if(first,last,f,new);//在first到last范围内将满足f元素替换成new

如:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}
bool f(int val) {return val > 2;
}int main() {vector<int> v1 = { 1,2,3,4,5 };replace_if(v1.begin(), v1.end(), f, 2);for_each(v1.begin(), v1.end(), print);return 0;
}

4.swap

swap(c1,c2);//将容器c1和c2交换

如:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };vector<int> v2 = { 4,5,6,7,8 };	swap(v1, v2);for_each(v1.begin(), v1.end(), print);cout << endl;for_each(v2.begin(), v2.end(), print);return 0;
}

五.算术生成算法

在头文件numeric中

accumulate();//计算容器元素累计总和
fill();//向容器添加元素

1.accumulate

accumulate(first,last,value);//在容器[first,last)范围内从值value开始计算总和

如:

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;int main() {vector<int> v1 = { 1,2,3,4,5 };int total = accumulate(v1.begin(), v1.end(), 2);cout << total << endl;return 0;
}

2.fill

fill(first,last,value);//在容器[first,last)范围内填充值value

如:

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };fill(v1.begin(), v1.end(), 2);for_each(v1.begin(), v1.end(), print);return 0;
}

六.常用集合算法

set_intersection();//求两个容器的交集
set_union();//求两个容器的并集
set_difference();//求两个容器的差集

1.set_intersection

set_intersection(first1,last1,first2,last2,first3);//first1,last1,first2,last2,为两个容器,first3为新容器

如:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };vector<int> v2 = { 4,5,6,7,8 };vector<int> v3;v3.resize(min(v1.size(), v2.size()));set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), v3.end(), print);return 0;
}

2.set_union

set_union(irst1,last1,first2,last2,first3);//first1,last1,first2,last2,为两个容器,first3为新容器

如:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };vector<int> v2 = { 4,5,6,7,8 };vector<int> v3;v3.resize(v1.size() + v2.size());set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), v3.end(), print);return 0;
}

3.set_difference

set_difference(first1,last1,first2,last2,first3);//first1,last1,first2,last2,为两个容器,first3为新容器

如:
v1 - v2

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };vector<int> v2 = { 4,5,6,7,8 };vector<int> v3;v3.resize(max(v1.size(), v2.size()));set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), v3.end(), print);return 0;
}

v2 - v1

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;void print(int val) {cout << val << " ";
}int main() {vector<int> v1 = { 1,2,3,4,5 };vector<int> v2 = { 4,5,6,7,8 };vector<int> v3;v3.resize(max(v1.size(), v2.size()));set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());for_each(v3.begin(), v3.end(), print);return 0;
}

总结

希望大家点赞收藏我会尽快更新STL!!!

http://www.dtcms.com/wzjs/177353.html

相关文章:

  • 北京科技网站建设我想学做互联网怎么入手
  • wordpress github主题济南网站优化
  • 如何自助建网站一站式建网站网站建设需求模板
  • 自建网站备案网站页面优化方法
  • 沧州市做网站的营销型网站模板
  • 互联网网站建设彭聪百度数据研究中心
  • 网站建设赌博搜索引擎网站提交入口
  • 网站注册域名如何推广一个平台
  • 做火锅加盟哪个网站好wordpress seo教程
  • 跨境电商培训哪家最好长沙seo袁飞
  • 企业网站 费用seo优化或网站编辑
  • 徐州企业建站系统模板如何创建一个网站
  • 福州网站建设公司青岛百度seo代理
  • 软件下载网站哪个比较好网站外链的优化方法
  • 网站排名优化服务商内容营销
  • 网站设计与网页制作模板谷歌搜索引擎营销
  • 中国石化工程建设公司网站外包优化网站
  • 天蝎网站建设爱站网关键词查询网站
  • 行唐县网站建设公司东莞网络营销网站建设
  • 网站测试方法有哪些关联词有哪些 全部
  • 网页设计表格广东企业网站seo哪里好
  • 常州做上市公司律所网站优化公司开始上班了
  • 做营销网站建设价格短视频精准获客
  • 网站建设账单记账日360网站收录提交入口
  • 外贸做网站的好处重庆森林影评
  • 网站建设服务项目表格怎么弄一个自己的网址
  • 滨海新区网站建设推广文案怎么写
  • 响应式网站代码规范怎么做网站宣传
  • 手机网页微信登录入口搜索引擎优化的定义
  • 用群晖做网站服务器网站改版