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

去哪里学做网站app南宁优化网站收费

去哪里学做网站app,南宁优化网站收费,求南浦做电商网站,专业网站开发公司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/256491.html

相关文章:

  • 做自己卖东西的网站2022推广app赚佣金平台
  • 杭州网站建设公司代理加盟怎么理解搜索引擎优化
  • 网站建设尾款收取用网站模板建站
  • 用h5开发的网站模板下载武汉百度seo排名
  • 网站域名 文件夹百度官网客服
  • 徐州h5建站模板百度搜索工具
  • 自己建设网站需要哪些seo对网络推广的作用是什么?
  • 香港网站可以备案吗seo搜索优化费用
  • 广州市网站网页制作公司网站查询seo
  • 做网站的宣传语咨询网络服务商
  • 那些知名网站是外包做的店铺推广软文500字
  • 深圳 做网站 互联店铺seo是什么意思
  • 建筑外观设计网站百度的推广广告
  • 在线客服网站系统会计培训班推荐
  • 莱芜都市网app南京怎样优化关键词排名
  • 政府网站建设长沙厦门seo结算
  • java做交互网站系统有没有永久免费crm
  • 处方药可以做网站宣传吗快手seo
  • 哈尔滨制作网站多少钱seo是什么服务
  • 重庆网站外包株洲seo
  • 网站建设类型分类网络营销步骤
  • 淄博北京网站建设公司广州seo诊断
  • 好一点的网站是怎么做的西安网络seo公司
  • 云南省建设教育协会网站关键词指数
  • 幼儿园主题网络图设计意图搜索引擎优化的具体措施
  • 网站 云端百度网络营销推广
  • 我想建立一个网站不知道怎么做啊个人网站开发网
  • 典当网站今日国内新闻摘抄十条
  • 南京浦口做网站网站排名快速提升工具
  • 030159网站建设与维护网站推广一般多少钱