赞
踩
- #include<iostream>
- #include<vector>
- using namespace std;
-
- int main()
- {
- vector<int>v;
- for (int n = 0; n < 5; ++n)
- v.push_back(n);
- vector<int>::iterator i;
- for (i = v.begin(); i < v.end(); ++i) {
- cout << *i << " ";
- *i *= 2;
- }
- cout << endl;
- for (vector<int>::reverse_iterator j = v.rbegin(); j != v.rend(); ++j)
- cout << *j << " ";
- return 0;
- }
- int main() {
- vector<int>v(100);
- for (int i = 0; i < v.size(); ++i)
- cout << v[i];
- vector<int>::iterator i;
- for (i = v.begin(); i < v.end(); ++i)
- cout << *i;
- for (i = v.begin(); i != v.end(); ++i)
- cout << *i;
- i = v.begin();
- while (i < v.end()) {
- cout << *i;
- i += 2;
- }
- return 0;
- }
主要有三个函数模板用于操作迭代器:
1,advance(p,n) 用于迭代器前后移动n个元素
2,distance(p,q) 用于计算两个迭代器之间的距离
3, iter_swap(p,q) 交换两个迭代器p,q指向的值
注意,使用这些辅助函数,需要include头文件<algorithm>
- int main() {
- int a[5] = { 1,2,3,4,5 };
- list<int>lst(a, a + 5);
- list<int>::iterator p = lst.begin();
- advance(p, 2);
- cout << "1)" << *p << endl;
- advance(p, -1);
- cout << "2)" << *p << endl;
- list<int>::iterator q = lst.end();
- q--;
- cout << "3)" << distance(p,q) << endl;
- cout << "4)" << *q << endl;
- for (p = lst.begin(); p != lst.end(); ++p) {
- cout << *p << endl;
- }
- return 0;
- }
以上程序为迭代器的辅助函数示例
首先用a数组给双向链表对象:lst赋值。
然后正向迭代器list<int>::iterator p指向链表的首地址。
迭代器p首先通过advance函数模板,后向移动2个单位:指向a[2],即输出3
而后p向后移动一个单位,指向a[1],输出2
然后定义了迭代器q,指向lst的最后一个元素的后一个位置的迭代器(一定要注意,end()是彻底抄底),然后进行q--,指向最后一个元素a[4]。
此时输出distance(p,q)也就是a[1]->a[4]的距离:3
1) 3
2) 2
3) 3
4) 5
1
2
3
4
5
- template<class InIt, class T>
- InIt find(InIt first, InIt last, const T&val)
- int main() {
- int a[10] = { 10,20,30,40 };
- vector<int>v;
- v.push_back(1); v.push_back(2);
- v.push_back(3); v.push_back(4);
- vector<int>::iterator p;
- p = find(v.begin(), v.end(), 3);
- if (p != v.end())
- cout << "1)" << *p << endl;
- p = find(v.begin(), v.end(), 9);
- if (p == v.end())
- cout << "not found" << endl;
- p = find(v.begin() + 1, v.end() - 1, 4);
- cout << "2)" << *p << endl;
- int *pp = find(a, a + 4, 20);
- if (pp == a + 4)
- cout << "not found" << endl;
- else
- cout << "3)" << *pp << endl;
- return 0;
- }
- class CAverage {
- public:
- double operator()(double a1, double a2, double a3) {
- return double(a1 + a2 + a3) / 3;
- }
- };
- int main() {
- CAverage a;
- cout << a(2, 3, 4) << endl;
- return 0;
- }
- template<class T>
- void PrintInterval(T first, T last) {
- for (; first != last; ++first)
- cout << *first << " ";
- cout << endl;
- }
- int SumSquares(int total, int value) {
- return total + value*value;
- }
- template<class T>
- class SumPowers {
- private:
- int power;
- public:
- SumPowers(int p):power(p){}
- const T operator()(const T&total, const T&value) {
- T v = value;
- for (int i = 0; i < power - 1; ++i)
- v = v*value;
- return total + v;
- }
- };
-
- int main() {
- const int SIZE = 10;
- int a1[] = { 1,2,3,4,5,6,7,8,9,10 };
- vector<int>v(a1, a1 + SIZE);
- cout << "1)";
- PrintInterval(v.begin(), v.end());
- int result = accumulate(v.begin(), v.end(), 0, SumSquares);
- cout << "2)平方和:" << result << endl;
- result = accumulate(v.begin(), v.end(), 0, SumPowers<int>(3));
- cout << "3)立方和:" << result << endl;
- return 0;
- }
- template<class InIt, class T, class Pred>
- T accumulate(InIt first,InIt last, T init, Pred op){
- for(; first!=last;++first)
- init=op(init,*first);
- return init;
- }
- int SumSquares(int init, int value) {
- return init + value*value;
- }
- template<class T>
- class SumPowers {
- private:
- int power;
- public:
- SumPowers(int p) :power(p) {};
- };
- T operator()(const T &init, const T &value) {
- T val = value;
- for (int i = 0; i < power-1; ++i)
- val = val*value;
- return init + val;
- }
- template<class _RandIt>
- void sort(_RandIt first,_RandIt last)
- template<class _RandIt,class Pred>
- void sort(_RandIt first, _RandIt last,Pred op)
- using namespace std;
- template<class T>
- void PrintInterval(T first, T last) {
- for (; first != last; ++first)
- cout << *first << " ";
- cout << endl;
- }
- class A {
- public:
- int v;
- A(int n):v(n){}
- };
- bool operator<(const A&a1, const A&a2) {
- return a1.v < a2.v;
- }
- bool GreatA(const A&a1, const A&a2) {
- return a1.v > a2.v;
- }
- struct LessA {
- bool operator()(const A&a1, const A&a2) {
- return (a1.v % 10) < (a2.v % 10);
- }
- };
- ostream &operator<<(ostream&o, const A&a) {
- o << a.v;
- return o;
- }
- int main() {
- int a1[4] = { 5,2,4,1 };
- A a2[5] = { 13,12,84,9,13 };
- sort(a1, a1 + 4);
- cout << "1)"; PrintInterval(a1, a1 + 4);
- sort(a2, a2 + 5);
- cout << "2)"; PrintInterval(a2, a2 + 5);
- sort(a2, a2 + 5,GreatA);
- cout << "3)"; PrintInterval(a2, a2 + 5);
- sort(a2, a2 + 5, LessA());
- cout << "4)"; PrintInterval(a2, a2 + 5);
- return 0;
- }
- class A{
- public:
- int v;
- A(int n):v(n){}
- };
- bool operator<(A &a1,A &a2) {
- return a1.num < a2.num;
- }
- template<class T>
- void PrintInterval(T first, T last) {
- for (; first != last; ++first)
- cout << *first << " ";
- cout << endl;
- }
- ostream& operator << (ostream&o,const A&a) {
- o << a.num;
- return o;
- }
- bool GreatA(A&a1,A&a2) {
- return a1.num > a2.num;
- }
- struct LessA {
- bool operator()(const A&a1, const A&a2) {
- return (a1.num % 10) < (a2.num % 10);
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。