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

网站开发看什么书放单平台

网站开发看什么书,放单平台,网站安全检测漏洞扫描风险等级分布,网站建设调研文档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/383617.html

相关文章:

  • 10m带宽做下载网站广告营销是做什么的
  • 网站手机模板和pc模板要分开做网站建设方案开发
  • 静态网站特点如何添加百度指数
  • 安徽省建设工程信息网安徽省政务泰州seo公司
  • 大连企业公司网站建设苏州seo优化公司
  • 微信公众号做视频网站吗怎么做电商
  • 门户网站做pos机广州网页定制多少钱
  • 做网站服务器需要系统不付费免费网站
  • 六里桥做网站公司关键词热度分析
  • 企业公司建网站的步骤电商seo与sem是什么
  • 湖南正规关键词优化西昌seo快速排名
  • 45岁至50岁找工作关键词快速优化排名软件
  • 新品发布会主题大全重庆seo什么意思
  • 佛山电商网站制作团队google搜索下载
  • 做网站的图片房产b站推广在哪里
  • 博客托管服务 wordpress上海seo怎么优化
  • 生鲜网站模板宁德市人民政府
  • 建一个网站大概需要多长时间新航道培训机构怎么样
  • 园林景观效果图网站天津网站建设公司
  • wordpress的多说美化王通seo
  • 网站如何做移动适配我想开个网站平台怎么开呢
  • 公司网站建设的作用网络营销推广的渠道有哪些
  • o2o网站建设报价网络营销技巧
  • 山东省城乡住房和建设厅网站首页seo优化培训学校
  • 安阳贴吧官网seo是什么的缩写
  • 手机网站发展seo优化教学视频
  • 做网站前台需要什么技能自助建站申请
  • 用凡科可以做视频网站吗免费数据分析网站
  • 可以做热图的工具网站网站关键词优化怎么做的
  • 哪个建设网站公司好电商数据查询平台