最后给大家整理个各种容器的意思及其用途


1-遍历算法.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(int x)
{
cout << x << endl;
}
class print
{
public:
void operator()(int x)
{
cout << x << endl;
}
};
int main()
{
int array[5] = {1, 2, 3, 4, 5};
vector<int> v(array, array + 5);
//for_each遍历过程中不能修改数据
for_each(v.begin(), v.end(), show); //回调函数形式遍历
for_each(v.begin(), v.end(), print()); //函数对象形式遍历
//transform遍历过程中可以修改数据
string s("helloworld");
transform(s.begin(), s.end(), s.begin(), ::toupper);
cout << s << endl;
return 0;
}
2-查找算法.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool GreaterTwo(int x)
{
return x > 2;
}
class GreaterThree
{
public:
bool operator()(int x)
{
return x > 3;
}
};
int main()
{
int array[6] = {1, 2, 2, 3, 4, 4};
vector<int> v(array, array + 6);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end())
{
cout << "不存在重复且相邻的" << endl;
}
else
{
cout << *it << endl;
}
bool ret = binary_search(v.begin(), v.end(), 4); //在有序的序列里面查找
if (ret)
{
cout << "元素存在" << endl;
}
else
{
cout << "元素不存在" << endl;
}
int num = count(v.begin(), v.end(), 2);
cout << num << endl;
num = count_if(v.begin(), v.end(), GreaterTwo); //一元谓词 回调函数
cout << num << endl;
it = find(v.begin(), v.end(), 3);
if (it == v.end())
{
cout << "元素不存在" << endl;
}
else
{
cout << *it << endl;
}
it = find_if(v.begin(), v.end(), GreaterThree()); //函数对象
if (it == v.end())
{
cout << "元素不存在" << endl;
}
else
{
cout << *it << endl;
}
return 0;
}
3-排序算法.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
void show(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v1;
vector<int> v2;
srand(time(NULL));
for (int i = 0; i < 5; i++)
{
v1.push_back(rand() % 10);
v2.push_back(rand() % 10);
}
sort(v1.begin(), v1.end(), less<int>());
sort(v2.begin(), v2.end(), less<int>());
cout << "v1:" << endl;
for_each(v1.begin(), v1.end(), show);
cout << endl;
cout << "v2:" << endl;
for_each(v2.begin(), v2.end(), show);
cout << endl;
vector<int> v3;
v3.resize(10);
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), show);
cout << endl;
random_shuffle(v1.begin(), v1.end());
cout << "v1:" << endl;
for_each(v1.begin(), v1.end(), show);
cout << endl;
reverse(v3.begin(), v3.end());
for_each(v3.begin(), v3.end(), show);
cout << endl;
return 0;
}
4-拷贝替换.cpp
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void show(int x)
{
cout << x << " ";
}
int main()
{
vector<int> v1(5, 1);
vector<int> v2(5, 2);
v2.resize(6);
copy(v1.begin(), v1.end(), ++(v2.begin()));
for_each(v2.begin(), v2.end(), show);
cout << endl;
swap(v1, v2);
for_each(v2.begin(), v2.end(), show);
cout << endl;
return 0;
}
5-函数适配器.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool Equal(string str)
{
return str == "bb";
}
int main()
{
vector<string> v;
v.push_back("aa");
v.push_back("bb");
v.push_back("cc");
v.push_back("dd");
//vector<string>::iterator it = find_if(v.begin(), v.end(), Equal);
vector<string>::iterator it = find_if(v.begin(), v.end(), bind1st(equal_to<string>(), "bb"));
if (it == v.end())
{
cout << "不存在" << endl;
}
else
{
cout << *it << endl;
}
return 0;
}

本文深入解析C++标准库中的关键算法,包括遍历、查找、排序、拷贝和函数适配器等,通过具体示例展示了如何在实际编程中应用这些算法,为读者提供了丰富的代码实践案例。

2640

被折叠的 条评论
为什么被折叠?



