2020.11.18 STL迭代器篇
一、内置迭代器
-------------------------------------------------
类中类 类对象模仿指针的行为 作用:遍历容器
vector list map set 向量 双向链表 映射 集合
内置迭代器:
1.正向迭代器:iterator
2.反向迭代器:reverse_iterator
3.常正向/常反向
const_iterator
const_reverse_iterator
begin() end()
rbegin() rend()
cbegin() cend()
crbegin() crend()
-------------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main()
{
vector<int> array(3);
for (int i = 0; i < 3; i++)
{
array[i] = i;
}
vector<int>::iterator iter = array.begin();
while (iter != array.end())
{
cout << *iter << "\t";
iter++;
}
cout << endl;
list<string> myList;
myList.push_back("I");
myList.push_back("love");
myList.push_back("you");
list<string>::iterator listIter = myList.begin();
for (;listIter!=myList.end(); listIter++)
{
cout << *listIter << "\t";
}
cout << endl;
for (list<string>::reverse_iterator rIter = myList.rbegin(); rIter != myList.rend(); rIter++)
{
cout << *rIter << "\t";
}
cout << endl;
cout << *myList.begin() << endl;
cout << *myList.rbegin() << endl;
for (list<string>::iterator dIter = myList.begin(); dIter != myList.end(); dIter++)
{
if (*dIter == "Love")
{
dIter = myList.erase(dIter);
}
}
for (listIter = myList.begin(); listIter != myList.end(); listIter++)
{
cout << *listIter << "\t";
}
return 0;
}
二、迭代器的简单实现
#include <iostream>
using namespace std;
template<typename type>
struct Node
{
type data;
Node<type>* next;
Node(type data):data(data),next(nullptr){}
};
template<class type>
class list
{
public:
list():firstNode(nullptr),backNode(nullptr){}
void push_back(type data);
void push_front(type data);
Node<type>* begin()
{
return firstNode;
}
Node<type>* end()
{
return nullptr;
}
public:
class iterator
{
public:
iterator() { pMove = nullptr; }
iterator(Node<type>* pMove)
{
this->pMove = pMove;
}
void operator=(Node<type>* pMove)
{
this->pMove = pMove;
}
bool operator!=(Node<type>* pMove)
{
return this->pMove != pMove;
}
iterator operator++(int)
{
iterator object(this->pMove);
this->pMove = this->pMove->next;
return object;
}
type operator*()
{
return pMove->data;
}
protected:
Node<type>* pMove;
};
protected:
Node<type>* firstNode;
Node<type>* backNode;
};
template<class type>
void list<type>::push_back(type data)
{
Node<type>* newNode = new Node<type>(data);
if (backNode == nullptr)
{
firstNode = newNode;
backNode = newNode;
}
else
{
backNode->next = newNode;
backNode = newNode;
}
}
template<class type>
void list<type>::push_front(type data)
{
Node<type>* newNode = new Node<type>(data);
if (firstNode == nullptr)
{
firstNode = newNode;
backNode = newNode;
}
else
{
newNode->next = firstNode;
firstNode = newNode;
}
}
int main()
{
list<int> intList;
for (int i = 0; i < 3; i++)
{
intList.push_back(i);
}
list<int>::iterator iter;
for (iter = intList.begin(); iter != intList.end(); iter++)
{
cout << *iter << "\t";
}
cout << endl;
return 0;
}
三、仿函数
-----------------------------------------------------------
仿函数:让类模仿函数调用的行为
函数名();
类名();
仿函数的实现:
必须实现()的重载
作用:排序准则
算法:
1.sort(iterator begin,iterator end,比较准则);
迭代器 连续的地址空间 排序算法
2.for_each(iterator begin,iterator end,函数指针);
遍历算法
从开始的位置到结束的位置所有元素
按照函数指针指向所指向的函数去执行
-----------------------------------------------------------
#include <iostream>
#include <functional>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct MM
{
string name;
int age;
int num;
bool operator<(MM object)
{
return this->name < object.name;
}
bool operator>(MM object)
{
return this->name > object.name;
}
};
ostream& operator<<(ostream& out, MM object)
{
out << object.name << "\t" << object.age << "\t" << object.num << endl;
return out;
}
bool compareAge(MM one, MM two)
{
return one.age > two.age;
}
bool compareNum(MM one, MM two)
{
return one.num > two.num;
}
template<class type>
void print(type data)
{
cout << data;
}
template<class type>
class sortRule
{
public:
bool operator()(type a, type b)const
{
return a > b;
}
protected:
};
int main()
{
vector<int> array(3);
array[0] = 3;
array[1] = 2;
array[2] = 1;
sort(array.begin(), array.end());
sort(array.begin(), array.end(), less<int>());
for_each(array.begin(), array.end(), print<int>);
cout << endl;
sort(array.begin(), array.end(), greater<int>());
for_each(array.begin(), array.end(), print<int>);
int intArray[5] = { 1,3,2,6,4 };
sort(intArray, intArray + 5);
cout << endl;
MM mmArray[3] = { "MM1",18,1001,"MM3",25,1005,"MM3",79,1004 };
cout << (mmArray[0] < mmArray[1]) << endl;
sort(mmArray, mmArray + 3);
for_each(mmArray, mmArray + 3, print<MM>);
cout << "按年龄排序" << endl;
sort(mmArray, mmArray + 3, compareAge);
for_each(mmArray, mmArray + 3, print<MM>);
cout << "按照编号排序" << endl;
sort(mmArray, mmArray + 3, compareNum);
for_each(mmArray, mmArray + 3, print<MM>);
cout << "自己写的从大到小:" << endl;
sort(mmArray, mmArray + 3, sortRule<MM>());
for_each(mmArray, mmArray + 3, print<MM>);
cout << "库中提供的从大到小:" << endl;
sort(mmArray, mmArray + 3, greater<MM>);
for_each(mmArray, mmArray + 3, print<MM>);
return 0;
}
四、流型迭代器
-------------------------------------------------
拷贝算法
copy(iterator begin,iterator end,iterator newBegin);
流形迭代器:了解内容
1.1 输出流型迭代器
ostream_iterator<type>(ostream obejct);
ostream_iterator<type>(ostream obejct,char* str);
对象=value 实际含义是打印
1.2 输入流型迭代器
istream_iterator<type> obejct;
istream_iterator<type>(istream object);
*istream_iterator_object 等效 cin>>操作
-------------------------------------------------
#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <vector>
using namespace std;
void print(int data)
{
cout << data << "\t";
}
int main()
{
int array[3] = { 1,3,2 };
vector<int> vec(3);
copy(array, array + 3, vec.begin());
for_each(vec.begin(), vec.end(), print);
ostream_iterator<int> object(cout);
object = 1001;
cout << endl;
copy(array, array + 3, ostream_iterator<int>(cout, ","));
cout << endl;
string a = "...";
copy(array, array + 3, ostream_iterator<int>(cout, a.c_str()));
cout << endl;
vector<int> inputData;
istream_iterator<int> endOfStream;
istream_iterator<int> inputIter(cin);
while (inputIter != endOfStream)
{
inputData.push_back(*inputIter);
++inputIter;
}
cout << "inputData" << endl;
copy(inputData.begin(), inputData.end(), ostream_iterator<int>(cout));
cout << endl;
return 0;
}