文章目录
- 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++ 仿函数模板
慕课网C++教程
1. STL
1. 概览



2. 容器

2.1. 序列式容器vector、list、deque
初始化

int iArr[] = { 1, 2,3,4,5 };
vector<int> iVector(iArr, iArr &#43; 4);
list<int> iList(iArr, iArr &#43; 4);
deque<int> iDeque(iArr, iArr &#43; 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[] &#61; { 1, 2,3,4,5 };
vector<int> iVector(iArr, iArr &#43; 4);
list<int> iList(iArr, iArr &#43; 4);
deque<int> iDeque(iArr, iArr &#43; 4);
queue<int> iQueue(iDeque);
stack<int> iStack(iDeque);
priority_queue<int> iPQueue(iArr, iArr &#43; 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"] &#61; 95.0;
studentSocres["LiHong"] &#61; 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 &#61; studentSocres.find("zhaoliu");
if (iter !&#61; studentSocres.end())
{
cout << "Found the score is: " << iter->second << endl;
}
else
{
cout << "Didn&#39;t find the key." << endl;
}
删除-erase函数

- 注意迭代器失效问题&#xff0c;删除迭代器指针后&#xff0c;迭代器要向下个位置移动一位&#xff0c;继续判断下个位置的key&#xff0c;value
- 即&#xff0c;先清除临时迭代器中的内容&#xff0c;再将迭代器指向下个位置的内容
studentSocres.erase(iter&#43;&#43;);
iter &#61; studentSocres.begin();
while (iter !&#61; studentSocres.end())
{
if (iter->second < 98.0)
{
studentSocres.erase(iter&#43;&#43;);
}
else
{
iter&#43;&#43;;
}
}
for_each(studentSocres.begin(), studentSocres.end(), Display());
cout << endl;

用for循环
for (iter &#61; studentSocres.begin(); iter !&#61; studentSocres.end(); iter&#43;&#43;)
{
if (iter->second <&#61; 98.5)
{
iter &#61; studentSocres.erase(iter);
}
}
for_each(studentSocres.begin(), studentSocres.end(), Display());
cout << endl;
用find函数查找删除或直接删除
iter &#61; studentSocres.find("LiHong");
studentSocres.erase(iter);
for_each(studentSocres.begin(), studentSocres.end(), Display());
int n &#61; 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&#43;&#43; 原生函数
bool MySort(int a, int b)
{
return a < b;
}
void Display(int a)
{
cout << a << " ";
}
int main() {
int arr[] &#61; { 4, 3, 2, 1, 7 };
sort(arr, arr &#43; 5, MySort);
for_each(arr, arr &#43; 5, Display);
cout << endl;
return 0;
}

C&#43;&#43; 泛型编程
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[] &#61; { 4, 3, 2, 1, 7 };
sort(arr2, arr2 &#43; 5, MySortT<int>);
for_each(arr2, arr2 &#43; 5, DisplayT<int>);
cout << endl;
return 0;
}
C&#43;&#43; 仿函数
struct SortF
{
bool operator() (int a, int b)
{
return a < b;
}
};
struct DisplayF
{
void operator() (int a)
{
cout << a << " ";
}
};
int main() {
int arr3[] &#61; { 4, 3, 2, 1, 7 };
sort(arr3, arr3 &#43; 5, SortF());
for_each(arr3, arr3 &#43; 5, DisplayF());
cout << endl;
return 0;
}
C&#43;&#43; 仿函数模板
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[] &#61; { 4, 3, 2, 1, 7 };
sort(arr4, arr4 &#43; 5, SortTF<int>() );
for_each(arr4, arr4 &#43; 5, DisplayTF<int>());
cout << endl;
return 0;
}