2799 Simple Life @myLove (eden)
2017-05-29 14:16
316 查看
Description
An iterator is any object that, pointing to some element in a range ofelements (such as an array or a container), has the ability to iterate through
the elements of that range using a set of operators (with at least the
increment (++) and dereference (*) operators).
The most obvious form of iterator is a pointer: A pointer can point to
elements in an array, and can iterate through them using the increment
operator (++). But other kinds of iterators are possible. For example, each
container type (such as a list) has a specific iterator type designed to
iterate through its elements.
Details about iterator:
C PLUS PLUS
WIKIPEDIA
Now we are going to make an iterator and find fun!
Carson has written the “simple_vector”, it realizes a little of the functions
in STL_Vector, he invites all of you to design the “simple_iterator.h”.
What is simple_vector:
It is a data structure to store data dynamically.
member: char* data; long long int size, capacity;
function: pushBack(char); Begin(); End(); rBegin(); rEnd();
What is simple_iterator:
member: char* pointer;
function: char& base(), C++11 prev(), next(), begin(), end();
overload: * / ++ / – / == / != / < / > / <= / >= / + / - ;
Note: all the input data are from ‘a’ to ‘z’, and the vector’s size is no less than 5 in this test.
From: 林楚庭
Provided Codes
main.cpp
#include <iostream> #include "simple_vector.h" using namespace std; void test_iterator() { int t, i; char c; simple_vector ivec; simple_vector::iterator iter, iter1, iter2; cin >> t; for (i = 0; i < t; i++) { cin >> c; ivec.pushBack(c); } cout << "Foward test: "; for (iter = ivec.Begin(); iter != ivec.End(); ++iter) cout << *iter; cout << "\n"; cout << "Backward test: "; for (iter = ivec.rBegin(); iter != ivec.rEnd(); --iter) cout << *iter; cout << "\n"; cout << "Self-Foward test: "; iter1 = ivec.Begin(); iter2 = ++iter1; cout << *iter2; iter1 = ivec.Begin(); iter2 = iter1++; cout << *iter2 << "\n"; cout << "Self-Backward test: "; iter1 = ivec.rBegin(); iter2 = --iter1; cout << *iter2; iter1 = ivec.rBegin(); iter2 = iter1--; cout << *iter2 << "\n"; cout << "Compare test#1: "; iter1 = ivec.Begin(); iter2 = ivec.rBegin(); cout << (iter1 < iter2); cout << (iter1 > iter2); cout << (iter1 <= iter2); cout << (iter1 >= iter2); cout << (iter1 == iter1); cout << (iter1 != iter2) << "\n"; cout << "Compare test#2: "; iter1 = ivec.Begin(); iter2 = ivec.rBegin(); cout << (iter2 < iter1); cout << (iter2 > iter1); cout << (iter1 <= iter1); cout << (iter2 >= iter2); cout << (iter1 == iter2); cout << (iter1 != iter1) << "\n"; cout << "Move test: "; iter1 = ivec.Begin(); iter2 = iter1 + 4; cout << *iter2; iter2 = iter2 - 3; cout << *iter2; iter2 = iter2 + 2; cout << *iter2 << "\n"; cout << "C++11 test: "; iter1 = ivec.Begin(); iter2 = ivec.rBegin(); ++iter1; cout << *(iter1.begin()); cout << *(iter1.end()); cout << *(iter1.prev()); cout << *(iter1.next()); --iter2; cout << *(iter2.begin()); cout << *(iter2.end()); cout << *(iter2.prev()); cout << *(iter2.next()); } int main() { test_iterator(); return 0; }
simple_vector.h
#ifndef SIMPLE_VECTOR_H #define SIMPLE_VECTOR_H #include <iostream> #include <cstring> #include "simple_iterator.h" using namespace std; static long long int MAX = 9999999; class simple_vector { char* data; long long int size; long long int capacity; public: typedef simple_iterator iterator; simple_vector() : size(0), capacity(0) { data = new char[MAX]; data[0] = '\0'; memset(data, '\0', MAX); } ~simple_vector() { clear(); } void clear() { delete [] data; size = capacity = 0; } long long int getSize() { return size; } long long int getCapacity() { return capacity; } void pushBack(char c) { if (capacity == 0) capacity = 1; else if (size == capacity) capacity = capacity * 2; data[++size] = c; } simple_iterator Begin() { return iterator(data + 1); } simple_iterator End() { return iterator((data + size + 1)); } simple_iterator rBegin() { return iterator((data + size)); } simple_iterator rEnd() { return iterator(data); } }; #endif
Submission
simple_iterator.h
#ifndef SIMPLE_ITERATOR_H #define SIMPLE_ITERATOR_H class simple_iterator{ public: simple_iterator(){} simple_iterator(const simple_iterator& a){ p=a.p; } simple_iterator prev(){ simple_iterator tem; tem.p=p; --tem.p; return tem; } simple_iterator next(){ simple_iterator tem; tem.p=p; ++tem.p; return tem; } simple_iterator operator++(int){ simple_iterator tem; tem.p=p; ++p; return tem; } simple_iterator& operator++(){ ++p; return *this; } simple_iterator operator--(int){ simple_iterator tem; tem.p=p; --p; return tem; } simple_iterator& operator--(){ --p; return *this; } char operator*(){ return *p; } bool operator==(const simple_iterator& a){ return *p==*a.p; } bool operator!=(const simple_iterator& a){ return *p!=*a.p; } bool operator<(const simple_iterator& a){ return *p<*a.p; } bool operator>(const simple_iterator& a){ return *p>*a.p; } bool operator<=(const simple_iterator& a){ return *p<=*a.p; } bool operator>=(const simple_iterator& a){ return *p>=*a.p; } simple_iterator operator+(int n){ simple_iterator tem; tem.p=p+n; return tem; } simple_iterator operator-(int n){ simple_iterator tem; tem.p=p-n; return tem; } simple_iterator& operator=(const simple_iterator& a){ p=a.p; return *this; } simple_iterator begin(){ simple_iterator tem; tem.p=p; while(*(tem.p)<256&&*(tem.p)>0) --tem.p; ++tem.p; return tem; } simple_iterator end(){ simple_iterator tem; tem.p=p; while(*(tem.p)<256&&*(tem.p)>0) ++tem.p; --tem.p; return tem; } simple_iterator(char* a){ p=a; } private: char* p; }; #endif
相关文章推荐
- SPM:( homework 1)A simple project in my real life
- [英语歌曲]我的至爱:Love of my life
- in my life ,i love three thing
- Beginning the life of my CSDN blogs
- It's my life
- I lay my love on you
- Story of my programming life
- Where's my love(歌词)
- This is my life,if i don't fight for it,who will?
- What can I learn right now in just 10 minutes that could be useful for the rest of my life?
- Love Your Life》—— 热爱生活
- five stances in my life
- 人生中的低谷(who can save my life——only myself)
- My researches during my college life.
- 把字符串中的每个空格替换程“%20”,例如输入“I love my hometown”,输出为”I%20love%20my%20hometown”
- The Simple Life
- My life style
- 英文经典好歌《Nothing's Gonna Change My Love For You》没有什么能够改变我对你的爱-英译中
- keywords in my life
- my Android life - 1