代码改变世界

STL——容器,算法和迭代器的基本概念

2018-10-23 20:26  mengjuanjuan1994  阅读(466)  评论(0编辑  收藏  举报

 

容器:序列式容器;关联式容器。

迭代器:是一个类,类中封装一个指针,可以当成指针来用。

算法:解决某一个问题的有限步骤。

算法与容器是分离的,迭代器起到了连接算法与迭代器的作用:

#include <iostream>
using namespace std;

//算法
int mycount(int* pBegin, int* pEnd, int val)
{
int num = 0;
while (pBegin != pEnd)
{
if (*pBegin == val)
{
num++;
}
pBegin++;
}
return num;
}

int main()
{
//容器
int arr[] = {0, 7, 5, 4, 9, 2, 0};

//迭代器
int* pBegin = arr;
int* pEnd = &(arr[sizeof(arr) / sizeof(int)]);

cout << 1 << "在数组中的个数为:" << mycount(pBegin, pEnd, 1) << endl;
system("pause");
return 0;
}