您的位置:首页 > Web前端

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 of

elements (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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++