summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx.42@42l.fr>2023-01-17 11:44:39 +0100
committerkdx <kdx.42@42l.fr>2023-01-17 11:44:39 +0100
commitfc6f771143a0d60055f9a7ce0e6559089ab59349 (patch)
tree9962bd9dc7a13e68488c82e0420ced9d16d0945c
parentfa60aae63b4e2c743627741bb4ca10eb4f12bb80 (diff)
download42-containers-fc6f771143a0d60055f9a7ce0e6559089ab59349.tar.gz
iterator contructor
-rw-r--r--main.cpp2
-rw-r--r--vector.hpp14
2 files changed, 13 insertions, 3 deletions
diff --git a/main.cpp b/main.cpp
index c9fd62e..0f91611 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,7 +20,7 @@ int main()
other.push_back(8);
std::for_each(other.begin(), other.end(), print);
std::cout << std::endl;
- const vector<int> constvec(other);
+ const vector<int> constvec(other.rbegin(), other.rend());
std::for_each(constvec.rbegin(), constvec.rend(), print);
std::cout << std::endl;
other.pop_back();
diff --git a/vector.hpp b/vector.hpp
index 1dc82d6..e983b27 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -3,6 +3,7 @@
#include <limits>
#include <cstddef>
#include <stdexcept>
+#include <iostream>
namespace ft {
template <
@@ -145,7 +146,15 @@ public:
vector(InputIt first, InputIt last,
const Allocator& alloc = Allocator())
{
- /* TODO */
+ _alloc = alloc;
+ _size = 0;
+ while (first + _size != last)
+ _size += 1;
+ _capacity = _size;
+ if (_capacity > 0)
+ _data = _alloc.allocate(_capacity);
+ for (size_type i = 0; i < _size; i++)
+ _alloc.construct(_data + i, *(first + i));
}
vector(const vector& other) : _data(NULL)
@@ -190,12 +199,13 @@ public:
/* assign */
void assign(size_type count, const T& value)
{
- *this = vector(count, value);
+ *this = vector(count, value, _alloc);
}
template<class InputIt>
void assign(InputIt first, InputIt last)
{
+ *this = vector(first, last, _alloc);
/* TODO */
}