文章目录
- 1. STL
- 1. 概览
- 2. 容器
- 2.1. 序列式容器vector、list、deque
-
- 2.2. 适配器stack、queue、priority_queue
-
- 2.3. 关联型容器map、set
- 插入元素
- 遍历-仿函数
- 查询-find函数
- 删除-erase函数
-
- 3. 仿函数
- 3.1 概念
- 3.2 排序代码示例
- C++ 原生函数
- C++ 泛型编程
- C++ 仿函数
- C++ 仿函数模板
- 4. 算法库
- 4.1 总览
- 4.2 `transform`和`lambda`表达式使用
- 4.3 容器统计与二分查找
- `count`统计指定元素出现次数
- `count_if`条件统计
- `binder2nd`&&`binder1st`比较函数
- `binary_search`二分查找
- `search`查找子序列
慕课网C++教程
1. STL
1. 概览
2. 容器
2.1. 序列式容器vector、list、deque
初始化
int iArr[] = { 1, 2,3,4,5 };
vector<int> iVector(iArr, iArr + 4);
list<int> iList(iArr, iArr + 4);
deque<int> iDeque(iArr, iArr + 4);
遍历-for_each函数
struct Display {
void operator()(int i)
{
cout << i << " ";
}
};
int main() {
for_each( iVector.begin(), iVector.end(), Display());
cout << endl;
for_each(iList.begin(), iList.end(), Display());
cout << endl;
for_each(iDeque.begin(), iDeque.end(), Display());
cout << endl;
return 0;
}
2.2. 适配器stack、queue、priority_queue
初始化
int iArr[] = { 1, 2,3,4,5 };
vector<int> iVector(iArr, iArr + 4);
list<int> iList(iArr, iArr + 4);
deque<int> iDeque(iArr, iArr + 4);
queue<int> iQueue(iDeque);
stack<int> iStack(iDeque);
priority_queue<int> iPQueue(iArr, iArr + 4);
访问方式
while ( !iQueue.empty() )
{
cout << iQueue.front() << " ";
iQueue.pop();
}
cout << endl;
while (!iStack.empty())
{
cout << iStack.top() << " ";
iStack.pop();
}
cout << endl;
while (!iPQueue.empty())
{
cout << iPQueue.top() << " ";
iPQueue.pop();
}
cout << endl;
2.3. 关联型容器map、set
插入元素
map<string, double> studentSocres;
studentSocres["LiMing"] = 95.0;
studentSocres["LiHong"] = 98.5;
studentSocres.insert(pair<string, double>("zhangsan", 100.0) );
studentSocres.insert(pair<string, double>("Lisi", 98.6));
studentSocres.insert(pair<string, double>("wangwu", 94.5));
studentSocres.insert(map<string, double>::value_type("zhaoliu", 95.5) );
遍历-仿函数
struct Display
{
void operator()(pair<string, double> info)
{
cout << info.first << ": " << info.second << endl;
}
};
int main() {
for_each(studentSocres.begin(),studentSocres.end(), Display());
cout << endl;
return 0;
}
查询-find函数
map<string, double>::iterator iter;
iter = studentSocres.find("zhaoliu");
if (iter != studentSocres.end())
{
cout << "Found the score is: " << iter->second << endl;
}
else
{
cout << "Didn't find the key." << endl;
}
删除-erase函数
- 注意迭代器失效问题,删除迭代器指针后,迭代器要向下个位置移动一位,继续判断下个位置的key,value
- 即,先清除临时迭代器中的内容,再将迭代器指向下个位置的内容
studentSocres.erase(iter++);
iter = studentSocres.begin();
while (iter != studentSocres.end())
{
if (iter->second < 98.0)
{
studentSocres.erase(iter++);
}
else
{
iter++;
}
}
for_each(studentSocres.begin(), studentSocres.end(), Display());
cout << endl;
用for循环
for (iter = studentSocres.begin(); iter != studentSocres.end(); iter++)
{
if (iter->second <= 98.5)
{
iter = studentSocres.erase(iter);
}
}
for_each(studentSocres.begin(), studentSocres.end(), Display());
cout << endl;
用find函数查找删除或直接删除
iter = studentSocres.find("LiHong");
studentSocres.erase(iter);
for_each(studentSocres.begin(), studentSocres.end(), Display());
int n = studentSocres.erase("LiHong1");
cout << n << endl;
for_each(studentSocres.begin(), studentSocres.end(), Display());
studentSocres.erase(studentSocres.begin(), studentSocres.end());
for_each(studentSocres.begin(), studentSocres.end(), Display());
cout << endl;
3. 仿函数
3.1 概念
3.2 排序代码示例
C++ 原生函数
bool MySort(int a, int b)
{
return a < b;
}
void Display(int a)
{
cout << a << " ";
}
int main() {
int arr[] = { 4, 3, 2, 1, 7 };
sort(arr, arr + 5, MySort);
for_each(arr, arr + 5, Display);
cout << endl;
return 0;
}
C++ 泛型编程
template<class T>
inline bool MySortT(T const& a, T const& b)
{
return a < b;
}
template<class T>
inline void DisplayT(T const& a)
{
cout << a << " ";
}
int main() {
int arr2[] = { 4, 3, 2, 1, 7 };
sort(arr2, arr2 + 5, MySortT<int>);
for_each(arr2, arr2 + 5, DisplayT<int>);
cout << endl;
return 0;
}
C++ 仿函数
struct SortF
{
bool operator() (int a, int b)
{
return a < b;
}
};
struct DisplayF
{
void operator() (int a)
{
cout << a << " ";
}
};
int main() {
int arr3[] = { 4, 3, 2, 1, 7 };
sort(arr3, arr3 + 5, SortF());
for_each(arr3, arr3 + 5, DisplayF());
cout << endl;
return 0;
}
C++ 仿函数模板
template<class T>
struct SortTF
{
inline bool operator() (T const& a, T const& b) const
{
return a < b;
}
};
template<class T>
struct DisplayTF
{
inline void operator() (T const& a) const
{
cout << a << " ";
}
};
int main() {
int arr4[] = { 4, 3, 2, 1, 7 };
sort(arr4, arr4 + 5, SortTF<int>() );
for_each(arr4, arr4 + 5, DisplayTF<int>());
cout << endl;
return 0;
}
4. 算法库
4.1 总览
4.2 transform
和lambda
表达式使用
int ones[] = { 1, 2, 3, 4, 5 };
int twos[] = { 10, 20, 30, 40, 50 };
int results[5];
transform(ones, ones + 5, twos, results, std::plus<int>());
for_each(results, results + 5,
[ ](int a)->void {cout << a << endl; } );
cout << endl;
4.3 容器统计与二分查找
count
统计指定元素出现次数
int arr[] = { 0, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 7, 8 };
int len = sizeof(arr) / sizeof(arr[0]);
cout << count(arr, arr + len, 6) << endl;
count_if
条件统计
int arr[] = { 0, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 7, 8 };
int len = sizeof(arr) / sizeof(arr[0]);
cout << count_if(arr, arr + len, bind2nd(less<int>(), 7) ) << endl;
cout << count_if(arr, arr + len, bind1st(less<int>(), 7) ) << endl;
binder2nd
&&binder1st
比较函数
binary_search
二分查找
int arr[] = { 0, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 7, 8 };
int len = sizeof(arr) / sizeof(arr[0]);
cout << binary_search(arr, arr + len, 9) << endl;
search
查找子序列
- 传入两个容器序列的起始位置,查找第二个序列在第一个序列中的位置
int arr[] = { 0, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 7, 8 };
int len = sizeof(arr) / sizeof(arr[0]);
vector<int> iA(arr + 2, arr + 6);
vector<int> iA2;
iA2.push_back(1);
iA2.push_back(9);
cout << *search(arr, arr + len, iA.begin(), iA.end()) << endl;
cout << *search(arr, arr + len, iA2.begin(), iA2.end()) << endl;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)