STL模板常用算法

STL常用算法汇总

常用到的一些算法,记录下方便后续使用,小白自用,大神勿喷

1.遍历 & 查找算法

1)for_each:

常用的遍历算法。

vector<int> v = {1, 5, 3, 8};
for_each(v.begin(), v.end(), [](int val) {
    cout << val << endl;
});

2)transform:

将容器中的元素搬移到另一个容器中。

vector<int> s = { 1,4,7 };
vector<int> d;
d.resize(s.size());  //提前开辟空间,否则无法正常搬移
transform(s.begin(), s.end(), d.begin(), [](int val){
    return val;
});

3)find:

查找指定元素。找到时,返回该元素的迭代器;找不到时,返回end()。

vector<int> v = { 1,4,7 };
auto res = find(v.begin(), v.end(), 4);
if (res == v.end()){
    cout << "没有找到元素" << endl;
}
else{
    cout << "找到元素"<< *res << endl;
}

4)find_if:

按照条件查找元素。找到时,返回该元素的迭代器;找不到时,返回end()。

vector<int> v = { 1,4,7 };
auto res = find_if(v.begin(), v.end(), [](int val) {
    return val > 6;
});
if (res == v.end()){
    cout << "没有找到元素" << endl;
}
else{
    cout << "找到元素 "<< *res << endl;
}

5)adjacent_find:

查找相邻重复的元素。找到时,返回指向相邻元素中第一个元素的迭代器;找不到时,返回end()。

vector<int> v = { 1,4,7,7 };
auto res = adjacent_find(v.begin(), v.end());
cout << *res << endl;

6)binary_search:

查找指定的元素。找到时返回true,否则返回false。(注:无序序列中不可用)

vector<int> v = { 1,4,7,7 };
bool res = binary_search(v.begin(), v.end(), 7);
cout << res << endl;

7)count:

统计元素出现的次数。

vector<int> v = { 1,4,7,7 };
int res = count(v.begin(), v.end(), 7);
cout << "等于7的元素个数: " << res << endl;

8)count_if:

按照条件统计元素出现的次数。

vector<int> v = { 1,4,7,7 };
int res = count_if(v.begin(), v.end(), [](int val) {
    return val > 5;
});
cout << "大于5的元素个数为: " << res << endl;

2.排序算法

1)sort:

对容器内元素进行排序。

vector<int> v = { 1,4,2,7 };
 
sort(v.begin(), v.end());                  //默认从小到大排序
sort(v.begin(), v.end(), less<int>());     //从小到大排序
sort(v.begin(), v.end(), greater<int>());  //从大到小排序 

2)random_shuffle:

指定范围内的元素随机调整次序。是一种洗牌算法,想要真正随机必须加随机数种子。

vector<int> v = { 1,4,4,7 };
srand((unsigned int)time(NULL));
random_shuffle(v.begin(), v.end());

3)merge:

两个容器元素合并,并存储到另一容器中。注:两个容器必须是有序的,合并之后的容器依然是有序的。

 vector<int> v1 = { 1,3,5,7 };
 vector<int> v2 = { 2,4,6,8 };
 
 vector<int> d;
 d.resize(v1.size() + v2.size());   //分配空间
 merge(v1.begin(),v1.end(), v2.begin(), v2.end(),d.begin());

4)reverse:

将容器内的元素反转。

vector<int> v = { 1,3,5,7 };  
reverse(v.begin(), v.end());

3.拷贝 & 替换算法

1)copy:

容器内指定范围内的元素,拷贝到另一容器中。

vector<int> s = { 1,3,5,7 };  
vector<int> d;
d.resize(s.size());                 //分配空间
copy(s.begin(), s.end(), d.begin());

2)replace:

容器内指定范围内的元素,修改为新的元素。

vector<int> v = { 1, 3, 5, 3, 7 };                
replace(v.begin(), v.end(), 3, 30); //将v中所有3修改为30

3)replace_if:

容器内指定范围内满足条件的元素,修改为新的元素。

vector<int> v = { 1, 3, 5, 3, 7 }; 
replace_if(v.begin(), v.end(), [](int val) {     //将v中所有小于5的元素修改为30
    return val < 5;
}, 30); 

4)swap:

互换两个容器的元素。

vector<int> v1 = { 1, 3, 5, 7, 9}; 
vector<int> v2 = { 2, 4, 6, 8 };
swap(v1,v2);

4.算数算法

1)accumulate:

计算区间内 容器元素累计总和。

vector<int> v = { 1, 3, 5, 7, 9 }; 
int sum = accumulate(v.begin(),v.end(), 0);  //初始值为0

2)fill:

向容器中填充指定的元素。

vector<int> v = { 1, 3, 5, 7, 9 }; 
fill(v.begin(), v.end(), 100);     //所有元素修改为100

3)set_intersection:

求两个集合的交集。注:两个集合必须是有序序列。

vector<int> v1 = { 1, 3, 5, 7, 9 }; 
vector<int> v2 = { 2, 4, 6, 8, 9 };
 
vector<int> target;
target.resize(min(v1.size(), v2.size()));  //分配空间,iterEnd是结束位置
auto iterEnd = set_intersection(v1.begin(),v1.end(), v2.begin(), v2.end(), target.begin());

4)set_union:

求两个集合的并集。注:两个集合必须是有序序列。

vector<int> v1 = { 1, 3, 5, 7, 9 }; 
vector<int> v2 = { 2, 4, 6, 8, 9 };
 
vector<int> target;
target.resize(v1.size() + v2.size());  //分配空间,iterEnd是结束位置
auto iterEnd = set_union(v1.begin(),v1.end(), v2.begin(), v2.end(), target.begin());

5)set_difference:

求两个集合的差集。注:两个集合必须是有序序列。

vector<int> v1 = { 1, 3, 5, 7, 9 }; 
vector<int> v2 = { 2, 4, 6, 8, 9 };
 
vector<int> target;
target.resize(max(v1.size(),v2.size()));  //分配空间,iterEnd是结束位置
auto iterEnd = set_difference(v1.begin(),v1.end(), v2.begin(), v2.end(), target.begin());
补充:

1)STL常用算法主要分布在,和中
2)定义了比较、交换、查找、遍历、复制、修改等操作。
3)定义了简单数学运算的模板函数。
4)定义了模板类,用以声明函数对象。

发布了13 篇原创文章 · 获赞 0 · 访问量 337

猜你喜欢

转载自blog.csdn.net/qq_35312463/article/details/103786324