C++ 使用迭代器c++;
第一次我开始使用迭代器时,我有一个任务来进行排序,对int类型的任何容器进行排序,然后对int类型的任何容器进行排序。所以,我很快就迷路了。 这是我的密码:C++ 使用迭代器c++;,c++,C++,第一次我开始使用迭代器时,我有一个任务来进行排序,对int类型的任何容器进行排序,然后对int类型的任何容器进行排序。所以,我很快就迷路了。 这是我的密码: #include <iostream> #include <algorithm> #include <iterator> using namespace std; //***************************************************** template <cl
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
//*****************************************************
template <class Type, class Iterator>
void Sort (Type arr[], Iterator beginning, Iterator ending)
{
int * intPointer;
intPointer = arr;
for(auto i=beginning; i<ending-1; i++)
{
for (auto j=i+1; j<ending; j++)
{
if (*intPointer>*intPointer++) // PROBLEM
{
swap(&intPointer, &intPointer); //AND HERE
}
}
}
};
//----------------------------------------------------
template <class Type>
void swap(Type *value1, Type *value2)
{
int temp = *value1;
*value1 = *value2;
*value2 = temp;
return;
}
//----------------------------------------------------
template <class Type, class Iterator>
void display(Type arr[], Iterator beginning, Iterator ending)
{
int * intPointer;
intPointer = arr;
for(auto i=beginning; i<ending; i++)
{
cout << *intPointer << " ";
intPointer++;
}
cout << endl;
};
//*****************************************************
int main()
{
int test[10] = {5,6,8,9,4,11,0,45,100,14};
Sort(test, begin(test), end(test));
display(test, begin(test), end(test));
}
#包括
#包括
#包括
使用名称空间std;
//*****************************************************
模板
无效排序(类型arr[],迭代器开始,迭代器结束)
{
int*int指针;
intPointer=arr;
对于(自动i=开始;i
使用迭代器时,第一个参数中不需要数组。可以删除它:
void Sort(Iterator beginning, Iterator ending);
void display(Iterator beginning, Iterator ending);
void Sort(Iterator beginning, Iterator ending);
void display(Iterator beginning, Iterator ending);