【嵌入式——C++】算法(STL)

【嵌入式------C++】算法(STL)

需要引入头文件 #include <algorithm>

遍历算法

for_each

遍历容器。
代码示例

c 复制代码
//普通函数
void print01(int val) {
 cout << val << " ";
}

//仿函数
class print02 {
public :
 void operator()(int val) {
  cout << val << " ";
 }
};

void testForEach() {
 vector<int> v;

 for (int i = 0; i < 10;i++) {
  v.push_back(i);
 }
 for_each(v.begin(),v.end(), print01);//0 1 2 3 4 5 6 7 8 9
 cout << endl;
 for_each(v.begin(), v.end(), print02());//0 1 2 3 4 5 6 7 8 9
}

transform

搬运容器到另一个容器中。参数1 原容器起始迭代器,参数2 原容器结束迭代器,参数3 目标容器开始迭代器 参数4 函数或者仿函数。
代码示例

c 复制代码
class Transform {

public:
 int operator()(int v) { 
  return v+100;
 }

};

void testTransform() {
 vector<int> v;

 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 vector<int> target;
 target.resize(v.size());
 transform(v.begin(),v.end(),target.begin(), Transform());

 for_each(target.begin(), target.end(), print02());// 100 101 102 103 104 105 106 107 108 109
}

查找算法

find

查找元素,查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()。
代码示例

c 复制代码
class Person {
public:
 Person(string name, int age) {
  this->name = name;
  this->age = age;
 }
 string name;
 int age;

 //重载== 为了让底层知道怎么比较数据
 bool operator==(const Person& p){
  if (this->name == p.name && this->age == p.age) {
   return true;
  }
  else {
   return false;
  }
 }
};

void testFind() {

 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 vector<int> ::iterator it = find(v.begin(),v.end(),6);
 if (it != v.end()) {
  cout << *it << " "; //6
 }
 
 Person p1("111",20);
 Person p2("222",23);
 Person p3("333", 33);
 Person p4("444", 55);


 vector<Person> vp;
 vp.push_back(p1);
 vp.push_back(p2);
 vp.push_back(p3);
 vp.push_back(p4);

 vector<Person> ::iterator it1 = find(vp.begin(), vp.end(), p2);
 if (it1 != vp.end()) {
  cout << it1->name << " " << it1->age<<endl; // 222 23
 }
}

find_if

按条件查找元素,参数1 开始迭代器 参数2 结束迭代器 参数3 谓词(返回bool类型的仿函数)。
代码示例

c 复制代码
class GreaterFive {
public :
 bool operator()(int val) {
  return val > 5;
 }
};

class Greater20 {
public :
 bool operator()(Person &p) {
  return p.age > 20;
 }
};

void testFindIf() {

 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 //找到大于5的数
 vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
 if (it != v.end()) {
  cout << *it<<" "; // 6
 }

 Person p1("111", 20);
 Person p2("222", 23);
 Person p3("333", 33);
 Person p4("444", 55);

 vector<Person> vp;
 vp.push_back(p1);
 vp.push_back(p2);
 vp.push_back(p3);
 vp.push_back(p4);

 //找到年龄大于20的
 vector<Person>::iterator it1 = find_if(vp.begin(), vp.end(), Greater20());
 if (it1 != vp.end()) {
  cout << it1->name << " "; // 222
 }
}

adjacent_find

查找相邻重复元素,返回相邻元素的第一个位置的迭代器。
代码示例

c 复制代码
void testAdjacen() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 v.push_back(2);
 v.push_back(1);
 v.push_back(1);
 v.push_back(3);
 v.push_back(4);
 v.push_back(5);
 v.push_back(5);

 vector<int>::iterator it = adjacent_find(v.begin(),v.end());
 cout << *it << " "; //1

}

二分查找法,查找指定元素是否存在,在无序序列中不可用,查到返回true(1),查不到返回false(0)。
代码示例

c 复制代码
void testBinary() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }

 bool b = binary_search(v.begin(), v.end(),6);
 cout << b<<endl; // 1
}

count

统计元素出现的次数 参数1 开始迭代器 参数2 结束迭代器 参数3 统计的元素。
代码示例

c 复制代码
class Person {
public:
 Person(string name, int age) {
  this->name = name;
  this->age = age;
 }
 string name;
 int age;

 //重载==
 bool operator==(const Person& p){
  if (this->name == p.name && this->age == p.age) {
   return true;
  }
  else {
   return false;
  }
 }
};

void testCount() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 v.push_back(6);
 v.push_back(6);
 v.push_back(6);
 v.push_back(6);

 int a = count(v.begin(), v.end(), 6);
 cout << a << endl; //5


 Person p1("111", 20);
 Person p2("222", 23);
 Person p3("333", 33);
 Person p4("444", 55);

 Person p("444", 55);

 vector<Person> vp;
 vp.push_back(p1);
 vp.push_back(p2);
 vp.push_back(p3);
 vp.push_back(p4);
//容器中和p姓名相同 年龄相同的数量
 int c = count(vp.begin(), vp.end(), p);
 cout << c << endl; //1

}

count_if

按条件统计元素个数,参数1 开始迭代器 参数2 结束迭代器 参数3 谓词。
代码示例

c 复制代码
class GreaterFive {
public :
 bool operator()(int val) {
  return val > 5;
 }
};

class Greater20 {
public :
 bool operator()(Person &p) {
  return p.age > 20;
 }
};

void testCountIf() {

 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }

 //统计大于5的数
 int c= count_if(v.begin(), v.end(), GreaterFive());
 cout << c << endl; //4


 Person p1("111", 14);
 Person p2("222", 16);
 Person p3("333", 22);
 Person p4("444", 55);

 vector<Person> vp;
 vp.push_back(p1);
 vp.push_back(p2);
 vp.push_back(p3);
 vp.push_back(p4);

 int cc = count_if(vp.begin(), vp.end(), Greater20());
 cout << cc << endl;// 2
}

排序算法

sort

对容器内元素进行排序,默认升序,参数1 开始迭代器 参数2 结束迭代器 参数3 谓词。
代码示例

c 复制代码
void testSort() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 printVector(v);0 1 2 3 4 5 6 7 8 9

 //默认升序
 sort(v.begin(),v.end());
 printVector(v);//0 1 2 3 4 5 6 7 8 9

 //添加谓词 降序
 sort(v.begin(), v.end(),greater<int>());
 printVector(v);//9 8 7 6 5 4 3 2 1 0

}

random_shuffle

洗牌 指定范围内的元素随机调整次序。
代码示例

c 复制代码
void testRandomShuffle() {
 //该行代码是为了每次随机排序结果不一样 
 srand((unsigned int)time(NULL));

 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 printVector(v);//0 1 2 3 4 5 6 7 8 9
 random_shuffle(v.begin(), v.end());
 printVector(v);//8 1 9 2 0 5 7 3 4 6
 
}

merge

两个容器元素合并,并存储到另一容器中,两个容器必须是有序的,参数1 容器1开始迭代器 参数2 容器1结束迭代器 参数3 容器2开始迭代器 参数4 容器2结束迭代器 参数5 目标容器开始迭代器。
代码示例

c 复制代码
void testMerge() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }

 vector<int> v1;
 for (int i = 10; i < 20; i++) {
  v1.push_back(i);
 }

 vector<int> v2;
 v2.resize(v.size()+ v1.size());
 merge(v.begin(),v.end(),v1.begin(),v1.end(),v2.begin());

 printVector(v2);//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
}

reverse

反转指定范围的元素。
代码示例

c 复制代码
void testReverse() {

 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }

 reverse(v.begin(), v.end());
 printVector(v);//9 8 7 6 5 4 3 2 1 0
}

拷贝算法和替换算法

copy

容器内指定范围的元素拷贝到另一容器中。
代码示例

c 复制代码
void testCopy() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }

 vector<int> target;
 target.resize(v.size());
 copy(v.begin(),v.end(),target.begin());
 printVector(target);//0 1 2 3 4 5 6 7 8 9
}

replace

将容器内指定范围的旧元素修改为新元素,参数1 开始迭代器 参数2 结束迭代器 参数3 旧元素 参数4 新元素。
代码示例

c 复制代码
void testReplace() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 v.push_back(6);

 printVector(v);//0 1 2 3 4 5 6 7 8 9 6
 replace(v.begin(), v.end(),6,100);
 printVector(v);//0 1 2 3 4 5 100 7 8 9 100
}

replace_if

容器内指定范围满足条件的元素替换为新元素,参数1 开始迭代器 参数2 结束迭代器 参数3 谓词 参数4 新元素。
代码示例

c 复制代码
class GreaterFive {
public :
 bool operator()(int val) {
  return val > 5;
 }
};

void testReplaceIf() {

 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 printVector(v);//0 1 2 3 4 5 6 7 8 9
 //大于5的 都替换为100
 replace_if(v.begin(), v.end(), GreaterFive(), 100);
 printVector(v);//0 1 2 3 4 5 100 100 100 100
}

swap

互换两个容器的元素,必须是同种类型的容器,参数1 容器1 参数2 容器2。
代码示例

c 复制代码
void testSwap() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }

 vector<int> v2;
 for (int i = 10; i < 20; i++) {
  v2.push_back(i);
 }
 printVector(v);//0 1 2 3 4 5 6 7 8 9
 printVector(v2);//10 11 12 13 14 15 16 17 18 19
 swap(v,v2);
 printVector(v);//10 11 12 13 14 15 16 17 18 19
 printVector(v2);//0 1 2 3 4 5 6 7 8 9
}

算术生成算法

引入头文件 #include <numeric>

accumulate

计算容器元素累计总和,参数1 开始迭代器 参数2 结束迭代器 参数3 起始值。

代码示例

c 复制代码
void testAccumulate() {
 vector<int> v;
 for (int i = 0; i <= 100; i++) {
  v.push_back(i);
 }

 int sum = accumulate(v.begin(),v.end(),0);
 cout << "sum=" << sum<<endl;//sum=5050
}

fill

将容器区间内元素填充为指定的值,参数1 开始迭代器 参数2 结束迭代器 参数3 添加值。
代码示例

c 复制代码
void testFill() {
 vector<int> v;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
 }
 printVector(v);//0 1 2 3 4 5 6 7 8 9
 fill(v.begin(), v.end(),1230);
 printVector(v);//1230 1230 1230 1230 1230 1230 1230 1230 1230 1230
}

集合算法

set_intersection

求两个集合的交集,两个集合必须是有序序列,目标容器开辟空间需要从两个容器中取最小值,返回的值是最后一个元素的位置。参数1 容器1开始迭代器 参数2 容器1结束迭代器 参数3 容器2开始迭代器 参数4 容器2结束迭代器 参数5 目标迭代器。
代码示例

c 复制代码
void testIntersection() {
 vector<int> v;
 vector<int> v2;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
  v2.push_back(i+4);
 }

 vector<int> target;
 target.resize(min(v.size(), v2.size()));
 //返回最后一个迭代器
 vector<int>::iterator itEnd = set_intersection(v.begin(),v.end(),v2.begin(),v2.end(),target.begin());

 //遍历
 for (vector<int>::iterator it = target.begin(); it != itEnd; it++) {
  cout << *it << " ";// 4 5 6 7 8 9
 }
 cout << endl;
 //这个是遍历容器中所有的元素
 printVector(target);//4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
}

set_union

求两个集合的并集,两个集合必须是有序序列,目标容器开辟空间是两个容器的和,返回的值是最后一个元素的位置,参数1 容器1开始迭代器 参数2 容器1结束迭代器 参数3 容器2开始迭代器 参数4 容器2结束迭代器 参数5 目标迭代器。
代码示例

c 复制代码
void testUion() {
 vector<int> v;
 vector<int> v2;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
  v2.push_back(i + 4);
 }
 vector<int> target;
 target.resize(v.size()+ v2.size());
 vector<int>::iterator itEnd = set_union(v.begin(), v.end(), v2.begin(), v2.end(), target.begin());
 for (vector<int>::iterator it = target.begin(); it != itEnd; it++) {
  cout << *it << " ";// 0 1 2 3 4 5 6 7 8 9 10 11 12 13
 }
 cout << endl;

 printVector(target);//0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 0 0 0 0 0
}

set_difference

求两个容器的差集,两个集合必须是有序序列,目标容器开辟空间需要从两个容器中取最大值,返回的值是最后一个元素的位置,参数1 容器1开始迭代器 参数2 容器1结束迭代器 参数3 容器2开始迭代器 参数4 容器2结束迭代器 参数5 目标迭代器。
代码示例

c 复制代码
void testDifference() {
 vector<int> v;
 vector<int> v2;
 for (int i = 0; i < 10; i++) {
  v.push_back(i);
  v2.push_back(i + 4);
 }
 printVector(v);//0 1 2 3 4 5 6 7 8 9
 printVector(v2);//4 5 6 7 8 9 10 11 12 13
 vector<int> target;
 target.resize(max(v.size(),v2.size()));

 //v和v2的差集
 vector<int>::iterator itEnd = set_difference(v.begin(), v.end(), v2.begin(), v2.end(), target.begin());
 for (vector<int>::iterator it = target.begin(); it != itEnd; it++) {
  cout << *it << " ";//  0 1 2 3
 }
 cout << endl;

 //v2和v的差集
 vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(),  v.begin(), v.end(),  target.begin());
 for (vector<int>::iterator it = target.begin(); it != itEnd1; it++) {
  cout << *it << " ";//  10 11 12 13
 }
 cout << endl;
}
相关推荐
wjs20241 天前
DOM CDATA
开发语言
Tingjct1 天前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法
C雨后彩虹1 天前
计算疫情扩散时间
java·数据结构·算法·华为·面试
猷咪1 天前
C++基础
开发语言·c++
IT·小灰灰1 天前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
快点好好学习吧1 天前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
秦老师Q1 天前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
烟锁池塘柳01 天前
解决Google Scholar “We‘re sorry... but your computer or network may be sending automated queries.”的问题
开发语言
是誰萆微了承諾1 天前
php 对接deepseek
android·开发语言·php
CSDN_RTKLIB1 天前
WideCharToMultiByte与T2A
c++