summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx.42@42l.fr>2023-01-16 09:15:00 +0100
committerkdx <kdx.42@42l.fr>2023-01-16 09:16:36 +0100
commitfa60aae63b4e2c743627741bb4ca10eb4f12bb80 (patch)
tree3ce4eff7df322517b1de992c4b7e465ea16820b6
parentd62bb1700590b1c8179ba39e8962836e97ee3cbf (diff)
download42-containers-fa60aae63b4e2c743627741bb4ca10eb4f12bb80.tar.gz
iterators
-rw-r--r--main.cpp31
-rw-r--r--vector.hpp122
2 files changed, 134 insertions, 19 deletions
diff --git a/main.cpp b/main.cpp
index bd8c700..c9fd62e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,24 +1,33 @@
#include "vector.hpp"
+#include <vector>
#include <iostream>
+#include <algorithm>
+
+using namespace ft;
+
+void print(const int& n)
+{
+ std::cout << n << std::endl;
+}
int main()
{
- ft::vector<int> vec(10);
- ft::vector<int> other(vec);
+ vector<int> vec(10);
+ vector<int> other(vec);
vec = other;
- std::cout << (vec == other) << std::endl;
- other.assign(ft::vector<int>::size_type(5), 3);
- for (ft::vector<int>::size_type i = 0; i < other.size(); i++)
- std::cout << other.at(i) << std::endl;
- std::cout << std::endl;
+ other.assign(vector<int>::size_type(5), 3);
other.push_back(6);
other.push_back(8);
- for (ft::vector<int>::size_type i = 0; i < other.size(); i++)
- std::cout << other.at(i) << std::endl;
+ std::for_each(other.begin(), other.end(), print);
+ std::cout << std::endl;
+ const vector<int> constvec(other);
+ std::for_each(constvec.rbegin(), constvec.rend(), print);
std::cout << std::endl;
other.pop_back();
- for (ft::vector<int>::size_type i = 0; i < other.size(); i++)
- std::cout << other.at(i) << std::endl;
+ std::for_each(other.begin(), other.end(), print);
+ std::cout << std::endl;
+ std::cout << "comparaisons" << std::endl;
+ std::cout << (vec == other) << std::endl;
std::cout << (vec == other) << std::endl;
return 0;
}
diff --git a/vector.hpp b/vector.hpp
index b63d6b8..1dc82d6 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -25,8 +25,8 @@ namespace ft {
bool operator<=(const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs);
template<class T, class Alloc>
- bool operator>(const ft::vector<T,Alloc>& lhs,
- const ft::vector<T,Alloc>& rhs);
+ bool operator>(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs);
template<class T, class Alloc>
bool operator>=(const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs);
@@ -41,11 +41,77 @@ public:
typedef value_type& reference;
typedef const value_type& const_reference;
typedef typename Allocator::pointer pointer;
- /* TODO */
- typedef vector* iterator;
- typedef vector* const_iterator;
- typedef vector* reverse_iterator;
- typedef vector* const_reverse_iterator;
+ typedef T* iterator;
+ typedef T* const_iterator;
+ class reverse_iterator {
+ private:
+ T* _ptr;
+ public:
+ reverse_iterator(T* ptr) : _ptr(ptr)
+ {
+ }
+ reference operator*()
+ {
+ return *_ptr;
+ }
+ T* operator->()
+ {
+ return _ptr;
+ }
+ reverse_iterator& operator++()
+ {
+ _ptr--;
+ return *this;
+ }
+ reverse_iterator& operator++(int)
+ {
+ reverse_iterator tmp (*this);
+ _ptr--;
+ return tmp;
+ }
+ reverse_iterator operator--()
+ {
+ _ptr++;
+ return *this;
+ }
+ reverse_iterator operator--(int)
+ {
+ reverse_iterator tmp (*this);
+ _ptr++;
+ return tmp;
+ }
+ reverse_iterator& operator+=(difference_type n)
+ {
+ *this = *this - n;
+ return *this;
+ }
+ reverse_iterator operator+(difference_type n)
+ {
+ reverse_iterator tmp (*this);
+ tmp += n;
+ return tmp;
+ }
+ reverse_iterator operator-(difference_type n)
+ {
+ reverse_iterator tmp (*this);
+ tmp -= n;
+ return tmp;
+ }
+ reverse_iterator& operator-=(difference_type n)
+ {
+ *this = *this + n;
+ return *this;
+ }
+ bool operator==(const reverse_iterator& other) const
+ {
+ return _ptr == other._ptr;
+ }
+ bool operator!=(const reverse_iterator& other) const
+ {
+ return !(*this == other);
+ }
+ };
+ typedef const reverse_iterator const_reverse_iterator;
private:
size_type _size;
size_type _capacity;
@@ -199,7 +265,47 @@ public:
return _data;
}
- /* ITERATORS TODO */
+ /* ITERATORS */
+ iterator begin()
+ {
+ return _data;
+ }
+
+ const_iterator begin() const
+ {
+ return _data;
+ }
+
+ iterator end()
+ {
+ return _data + _size;
+ }
+
+ const_iterator end() const
+ {
+ return _data + _size;
+ }
+
+ reverse_iterator rbegin()
+ {
+ return _data + _size - 1;
+ }
+
+ const_reverse_iterator rbegin() const
+ {
+ return _data + _size - 1;
+ }
+
+ reverse_iterator rend()
+ {
+ return _data - 1;
+ }
+
+ const_reverse_iterator rend() const
+ {
+ return _data - 1;
+ }
+
/* CAPACITY */
bool empty() const
{