20240513,常用算法(查找,排序,拷贝替换)

做着一些和考试无关的事情

常用查找算法——续

FIND_IF 

find_if  //按条件查找元素,返回迭代器POS / END()
find_if(beg,end,_Fred)    _Fred函数或谓词(返回BOOL类型的仿函数)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//find_if  //按条件查找元素    find_if(beg,end,_Fred)_Fred函数或谓词(返回BOOL类型的仿函数)
class Greater {
public:bool operator()(int val) {return val>5;}
};
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
class Greater02 {
public:bool operator()(const Person &p) {return p._age==9;}
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}vector<int>::iterator it=find_if(v.begin(), v.end(), Greater());if (it == v.begin()) {cout << " no find" << endl;}else {cout << "find :  " <<*it<< endl;}
}
void test02() {vector<Person>p;Person p0("fsdef", 23);Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);Person p4("啊上网服务", 53);p.push_back(p0);p.push_back(p1);p.push_back(p2);p.push_back(p3);p.push_back(p4);vector<Person>::iterator it = find_if(p.begin(), p.end(), Greater02());if (it == p.begin()) {cout << " no find" << endl;}else {cout << "find :  " << it->_name<<it->_age<< endl;}
}
int main() {test01();test02();return 0;
}
ADJACENT_FIND

adjacent_find  //查找相邻重复元素 
adjacent_find(begin,end)返回相邻元素的第一个的POS

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//adjacent_find  //查找相邻重复元素   adjacent_find(begin,end)返回相邻元素的第一个的POS
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void test01() {vector<int>v;v.push_back(0);v.push_back(1);v.push_back(0);v.push_back(3);v.push_back(3);vector<int>::iterator it = adjacent_find(v.begin(), v.end());if (it == v.begin()) {cout << " no find" << endl;}else {cout << "find :  " <<*it<< endl;}
}
void test02() {vector<Person>p;Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);p.push_back(p2);p.push_back(p1);p.push_back(p2);p.push_back(p3);p.push_back(p3);vector<Person>::iterator it = adjacent_find(p.begin(), p.end());if (it == p.begin()) {cout << " no find" << endl;}else {cout << "find :  " << it->_name<<it->_age<< endl;}
}
int main() {test01();test02();return 0;
}
BINARY_SEARCH

binary_search  //二分法查找 
bool binary_search(beg,end,value)   //无序序列中不可用

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//binary_search  //二分法查找   bool binary_search(beg,end,value)//无序序列中不可用void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}if (binary_search(v.begin(),v.end(),7)) {cout << " no find" << endl;}else {cout << "find :  " << endl;}
}
void test02() {}
int main() {test01();test02();return 0;
}
COUNT

count  //统计元素个数   count(beg,end,value)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//count  //统计元素个数   count(beg,end,value)
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void test01() {vector<int>v;v.push_back(0);v.push_back(0);v.push_back(0);v.push_back(3);v.push_back(3);int it=count(v.begin(), v.end(), 0);if (it==0) {cout << " no find" << endl;}else {cout << "find :  " <<it<<"   ge" << endl;}
}
void test02() {vector<Person>p;Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);p.push_back(p2);p.push_back(p3);p.push_back(p2);p.push_back(p3);p.push_back(p3);int it = count(p.begin(), p.end(), p1);if (it == 0) {cout << " no find" << endl;}else {cout << "find :  " << it << "   ge" << endl;}
}
int main() {test01();test02();return 0;
}
COUNT_IF

count_if  //按条件统计元素个数    count_if(beg,end,——Pred)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//count_if  //按条件统计元素个数    count_if(beg,end,_Pred)
class grater {
public:bool operator()(int val) {return val > 5;}
};class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}/*bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}*/string _name;int _age;
};
class grater02 {
public:bool operator()(const Person &p) {return p._age > 5;}
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}int it=count_if(v.begin(), v.end(), grater());if (it==0) {cout << " no find" << endl;}else {cout << "find :  " <<it<<"   ge" << endl;}
}
void test02() {vector<Person>p;Person p2("粉色", 4);Person p3("得分·", 45);p.push_back(p2);p.push_back(p3);p.push_back(p2);p.push_back(p3);p.push_back(p3);int it = count_if(p.begin(), p.end(), grater02());if (it == 0) {cout << " no find" << endl;}else {cout << "find :  " << it << "   ge" << endl;}
}
int main() {test01();test02();return 0;
}

常用排序算法

sort  //对容器内元素进行排序
random_shuffle  //洗牌,指定范围内顺序变随机
merge  //容器元素合并,并存储道另一容器中
reverse  //反转指定范围的元素 

SORT

sort  //对容器内元素进行排序   sort(beg,end,_Pred/谓词)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
sort  //对容器内元素进行排序
sort(beg,end,_Pred/谓词)
*/
class grater {
public:bool operator()(int v1,int v2) {return v1>v2;}
};class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}/*bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}*/string _name;int _age;
};
class grater02 {
public:bool operator()(const Person &p1,const Person &p2) {return p1._age > p2._age;}
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;sort(v.begin(), v.end(), grater());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;v.push_back(3);sort(v.begin(), v.end(), greater<int>());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}
void test02() {vector<Person>v;Person p0("fsdef", 23);Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);Person p4("啊上务", 53);v.push_back(p0);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout <<"name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;sort(v.begin(), v.end(), grater02());for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;
}
int main() {test01();test02();return 0;
}
RANDOM_SHUFFLE

random_shuffle  //洗牌,指定范围内顺序变随机   
random_shuffle(beg,end)——加上随机数种子

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
using namespace std;class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}string _name;int _age;
};
void test01() {srand((unsigned int)time(NULL));//加上随机数种子vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;random_shuffle(v.begin(), v.end());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;}
void test02() {srand((unsigned int)time(NULL));//加上随机数种子vector<Person>v;Person p0("fsdef", 2);Person p1("复合工艺", 28);Person p2("粉色", 29);Person p3("得分·", 45);Person p4("啊上务", 53);v.push_back(p0);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout <<"name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;random_shuffle(v.begin(), v.end());for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;
}
int main() {test01();test02();return 0;
}
MERGE

merge  //容器元素合并,并存储道另一容器中           PS:两个容器必须是有序的
merge(v1.beg ,v1.end ,v2.beg ,v2.end ,iterator dest)  iterator dest目标容器开始迭代器
自定义类型失败

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;void test01() {vector<int>v;vector<int>v1;vector<int>vv;for (int i = 0; i < 4; i++) {v.push_back(i);}for (int i = 0; i < 6; i++) {v1.push_back(i+7);}vv.resize(v.size() + v1.size());  //先开辟空间merge(v.begin(), v.end(), v1.begin(), v1.end(), vv.begin());for (vector<int>::iterator it = vv.begin(); it != vv.end(); it++) {cout << *it << " ";}cout << endl;
}int main() {test01();return 0;
}
REVERSE

reverse  //反转指定范围的元素  reverse(beg,end)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;class Person {
public:Person() {};Person(string n, int a) {this->_name = n;this->_age = a;}string _name;int _age;
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl << "revers after:" << endl;reverse(v.begin(), v.end());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}
void test02() {vector<Person>v;Person p0("fsdef", 2);Person p1("复合工艺", 28);Person p2("粉色", 29);Person p3("得分·", 45);Person p4("啊上务", 53);v.push_back(p0);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl << "revers after:" << endl;reverse(v.begin(), v.end());for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}
}
int main() {test01();test02();return 0;
}

常用拷贝和替换算法

copy  //容器内指定范围拷贝到另一容器中
replace  //指定范围 旧元素改为新元素
replace_if  //指定范围 旧元素替换为新元素
swap  //互换两个容器元素 

COPY

copy  //容器内指定范围拷贝到另一容器中      copy(beg,end,iterator dest) ---iterator dest目标起始迭代器

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
copy(beg,end,iterator dest)
*/
void myprint(int val) {cout << val << "  ";
}
void test01() {vector<int>v;vector<int>v2;for (int i = 0; i < 10; i++) {v.push_back(i);}v2.resize(v.size());//开辟空间copy(v.begin(), v.end(),v2.begin());for_each(v2.begin(), v2.end(), myprint);cout << endl;
}
void test02() {}
int main() {test01();test02();return 0;
}
REPLACE

replace  //指定范围 旧元素改为新元素     replace(beg,end,old val,new val)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
replace(beg,end,old val,new val)
*/
class Person {
public:Person() {};Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person &p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void myprint(int val) {cout << val << "  ";
}
void myprint02(Person p) {cout << "name: " << p._name << "\tage:" << p._age << endl;
}
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {int t = i;if ((t % 2) == 0) {v.push_back(i);}else {v.push_back(0);}}for_each(v.begin(), v.end(), myprint);cout << endl;replace(v.begin(), v.end(),0,3000);for_each(v.begin(), v.end(), myprint);cout << endl;
}
void test02() {vector<Person>v;Person p2("粉色", 29);Person p3("得  分·", 45);v.push_back(p2);v.push_back(p2);v.push_back(p3);for_each(v.begin(), v.end(), myprint02);cout << endl;replace(v.begin(), v.end(), p2, p3);for_each(v.begin(), v.end(), myprint02);cout << endl;
}
int main() {test01();test02();return 0;
}
REPLACE_IF

replace_if  //指定范围 旧元素替换为新元素        replace_if(beg,end,_pred,new val) 

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
replace_if(beg,end,_pred,new val)
*/
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person &p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void myprint(int val) {cout << val << "  ";
}
class greater5 {
public:bool operator()(int val) {return val > 5;}
};
class greater30 {
public:bool operator()(Person &p2) {return  p2._age<30;}
};
void myprint02(Person p) {cout << "name: " << p._name << "\tage:" << p._age << endl;
}
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for_each(v.begin(), v.end(), myprint);cout << endl;replace_if(v.begin(), v.end(), greater5(), 3000);for_each(v.begin(), v.end(), myprint);cout << endl;
}
void test02() {vector<Person>v;Person p2("粉色", 29);Person p3("得  分·", 45);v.push_back(p2);v.push_back(p2);v.push_back(p3);for_each(v.begin(), v.end(), myprint02);cout << endl;replace_if(v.begin(), v.end(), greater30(), p3);for_each(v.begin(), v.end(), myprint02);cout << endl;
}
int main() {test01();test02();return 0;
}
SWAP

swap  //互换两个容器元素   swap(container c1,container c2)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
replace_if(beg,end,_pred,new val)
*/
void myprint(int val) {cout << val << "  ";
}
void test01() {vector<int>v;vector<int>v1;for (int i = 0; i < 10; i++) {v.push_back(i);if(i<5){v1.push_back(0);}}cout << "swap before:" << endl;for_each(v.begin(), v.end(), myprint);cout << endl;for_each(v1.begin(), v1.end(), myprint);cout << endl;cout << "swap after:" << endl;swap(v, v1);for_each(v.begin(), v.end(), myprint);cout << endl;for_each(v1.begin(), v1.end(), myprint);cout << endl;
}
void test02() {}
int main() {test01();test02();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/697632.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Raft论文阅读笔记+翻译:In Search of Understandable Consensus Algorithm

In Search of Understandable Consensus Algorithm 摘要 Raft是一种管理复制日志的共识算法。它产生与&#xff08;多&#xff09;Paxos等效的结果&#xff0c;并且与Paxos一样高效&#xff0c;但其结构与Paxos不同。这使得Raft比Paxos更易理解&#xff0c;也为构建实际系统提供…

​​​【收录 Hello 算法】第 6 章 哈希表

目录 第 6 章 哈希表 本章内容 第 6 章 哈希表 Abstract 在计算机世界中&#xff0c;哈希表如同一位聪慧的图书管理员。 他知道如何计算索书号&#xff0c;从而可以快速找到目标图书。 本章内容 6.1 哈希表6.2 哈希冲突6.3 哈希算法6.4 小结

爱普生推出适用于物联网小尺寸温补晶振TG1612SLN

爱普生推出一款小尺寸温补晶振TG1612SLN&#xff0c;之前推出的小尺寸温补晶振TG2016SLN&#xff0c;封装2016已经是很小了&#xff0c;而TG1612SLN的尺寸仅为1.6x1.2x0.45毫米&#xff0c;不得不佩服爱普生的研发能力。 温度补偿晶体振荡器TG1612SLN使用爱普生开发和制造…

企业级WEB服务Nginx安装

企业级WEB服务Nginx安装 1. Nginx版本和安装方式 Mainline version 主要开发版本,一般为奇数版本号,比如1.19Stable version 当前最新稳定版,一般为偶数版本,如:1.20Legacy versions 旧的稳定版,一般为偶数版本,如:1.18Nginx安装可以使用yum或源码安装,但是推荐使用源码编译安…

Ngnix VTS模块添加和测试

目录 VTS模块介绍 上传软件包xftp/lrzsz 执行脚本 添加vts的配置 测试 测试&#xff1a;nginx.conf配置文件是否有语法错误 测试&#xff1a;windows机器上访问效果 VTS模块介绍 Nginx VTS模块&#xff08;nginx Virtual Host Traffic Status Module&#xff09;是一个第三…

GLU(Gated Linear Unit) 门控线性单元

文章目录 一、RNN二、GLU2.1 整体结构2.2 输入层(Input SentenceLookup Table)2.3 中间层(ConvolutionGate)2.4 输出层(Softmax)2.5 实验结果2.6 实现代码 三、RNN与GLU的对比参考资料 GLU可以理解为能够并行处理时序数据的CNN网络架构&#xff0c;即利用CNN及门控机制实现了RN…

ES扩缩容

ES扩容 1.1 页面扩容ES1 1.2 拷贝插件及ssl文件 JSON [ec_admin@kde-offline3 ~]$ sudo rsync -avP /usr/kde_ec/2.3.6.6-1/elasticsearch1/plugins/* kde-offline6:/usr/kde_ec/2.3.6.6-1/elasticsearch1/plugins/ ;echo $? [ec_admin@kde-offline3 ~]$ sudo rsync -avP /us…

“圣诞树图案的打印~C语言”

圣诞树图案的打印~C语言 题目原文&#xff1a;[圣诞树](https://www.nowcoder.com/practice/9a03096ed8ab449e9b10b0466de29eb2?tpId107&rp1&ru/ta/beginner-programmers&qru/ta/beginner-programmers/question-ranking&difficulty&judgeStatus&tags&…

linux部署安装DataX和DataX-Web

1.基础环境 JDK&#xff08;1.8 及其以上都可以&#xff0c;推荐 1.8&#xff09;&#xff0c;安装过程略 Python&#xff08;2 或者 3 都可以&#xff09;&#xff0c;安装过程略 Apache Maven 3.6.1&#xff08;只有DataX源码编译安装时需要&#xff09; 1.1下载maven安装…

【MQTT】paho.mqtt.c 库的“介绍、下载、交叉编译” 详解,以及编写MQTT客户端例子源码

&#x1f601;博客主页&#x1f601;&#xff1a;&#x1f680;https://blog.csdn.net/wkd_007&#x1f680; &#x1f911;博客内容&#x1f911;&#xff1a;&#x1f36d;嵌入式开发、Linux、C语言、C、数据结构、音视频&#x1f36d; ⏰发布时间⏰&#xff1a;2024-05-13 1…

kubeflow文档-介绍与架构

1. kubeflow介绍 Kubeflow项目致力于使机器学习&#xff08;ML&#xff09;工作流在Kubernetes上的部署变得简单、可移植和可扩展。目标不是重新创建其他服务&#xff0c;而是提供一种直接的方法&#xff0c;将ML的开源系统部署到不同的基础设施中。无论在哪里运行Kubernetes&a…

【408真题】2009-10

“接”是针对题目进行必要的分析&#xff0c;比较简略&#xff1b; “化”是对题目中所涉及到的知识点进行详细解释&#xff1b; “发”是对此题型的解题套路总结&#xff0c;并结合历年真题或者典型例题进行运用。 涉及到的知识全部来源于王道各科教材&#xff08;2025版&…