summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-13 06:45:41 +0100
committerkdx <kikoodx@paranoici.org>2023-01-13 06:45:41 +0100
commit0ebc9b2bce403762759b83803b4253e59e69ac79 (patch)
treeaa9ed00cf81bbfaaaaf03bc8f9ef633497c8f23a
parent1d56888259cb7047898d678204580cc840c12cd0 (diff)
download42-containers-0ebc9b2bce403762759b83803b4253e59e69ac79.tar.gz
comparaison operators
-rw-r--r--main.cpp2
-rw-r--r--vector.hpp58
2 files changed, 53 insertions, 7 deletions
diff --git a/main.cpp b/main.cpp
index 7909b6b..bd8c700 100644
--- a/main.cpp
+++ b/main.cpp
@@ -6,6 +6,7 @@ int main()
ft::vector<int> vec(10);
ft::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;
@@ -18,5 +19,6 @@ int main()
other.pop_back();
for (ft::vector<int>::size_type i = 0; i < other.size(); i++)
std::cout << other.at(i) << std::endl;
+ std::cout << (vec == other) << std::endl;
return 0;
}
diff --git a/vector.hpp b/vector.hpp
index b43d994..b0c0d52 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -288,23 +288,67 @@ namespace ft {
};
template<class T, class Alloc>
void swap(vector<T,Alloc>& lhs,
- vector<T,Alloc>& rhs);
+ vector<T,Alloc>& rhs)
+ {
+ lhs.swap(rhs);
+ }
+
template<class T, class Alloc>
bool operator==(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs);
+ const vector<T,Alloc>& rhs)
+ {
+ if (lhs.size() != rhs.size())
+ return false;
+ for (typename vector<T,Alloc>::size_type i = 0; i < lhs.size(); i++)
+ if (lhs[i] != rhs[i])
+ return false;
+ return true;
+ }
+
template<class T, class Alloc>
bool operator!=(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs);
+ const vector<T,Alloc>& rhs)
+ {
+ return !(lhs == rhs);
+ }
+
template<class T, class Alloc>
bool operator<(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs);
+ const vector<T,Alloc>& rhs)
+ {
+ const typename vector<T,Alloc>::size_type end
+ (lhs.size() < rhs.size() ? lhs.size() : rhs.size());
+ for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
+ if (lhs[i] < rhs[i])
+ return true;
+ }
+ return lhs.size() < rhs.size();
+ }
+
template<class T, class Alloc>
bool operator<=(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs);
+ const vector<T,Alloc>& rhs)
+ {
+ return !(lhs > rhs);
+ }
+
template<class T, class Alloc>
bool operator>(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs);
+ const vector<T,Alloc>& rhs)
+ {
+ const typename vector<T,Alloc>::size_type end
+ (lhs.size() < rhs.size() ? lhs.size() : rhs.size());
+ for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
+ if (lhs[i] > rhs[i])
+ return true;
+ }
+ return lhs.size() > rhs.size();
+ }
+
template<class T, class Alloc>
bool operator>=(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs);
+ const vector<T,Alloc>& rhs)
+ {
+ return !(lhs < rhs);
+ }
}