Skip to content

Commit da0aa4e

Browse files
Andor GunczerAndor Gunczer
Andor Gunczer
authored and
Andor Gunczer
committed
iterator implementation left
1 parent c5a80f8 commit da0aa4e

File tree

11 files changed

+413
-2
lines changed

11 files changed

+413
-2
lines changed

cpp07/ex02/Array.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/04/11 13:06:35 by agunczer #+# #+# */
9-
/* Updated: 2022/04/11 16:48:59 by agunczer ### ########.fr */
9+
/* Updated: 2022/04/11 17:10:26 by agunczer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -45,7 +45,7 @@ class Array {
4545
return *this;
4646
}
4747

48-
T& operator[](int i) {
48+
T& operator[](int i) { // this thing
4949
if (i >= arraySize) {
5050
std::cout << "Index out of bounds, first value returned" << std::endl;
5151
return array[0];

cpp08/ex00/Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# **************************************************************************** #
2+
# #
3+
# ::: :::::::: #
4+
# Makefile :+: :+: :+: #
5+
# +:+ +:+ +:+ #
6+
# By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ #
7+
# +#+#+#+#+#+ +#+ #
8+
# Created: 2022/04/01 18:10:56 by agunczer #+# #+# #
9+
# Updated: 2022/04/13 09:30:45 by agunczer ### ########.fr #
10+
# #
11+
# **************************************************************************** #
12+
13+
NAME = serialize
14+
15+
CC = clang++
16+
17+
CFLAGS = -Wall -Wextra -Werror -std=c++98
18+
19+
SRC = main.cpp
20+
21+
OBJ = $(SRC:.cpp=.o)
22+
23+
HEAD = easyfind.hpp
24+
all : $(NAME)
25+
26+
$(NAME) : $(SRC) $(HEAD)
27+
$(CC) $(CFLAGS) -c $(SRC)
28+
$(CC) $(CFLAGS) $(SRC) -o $(NAME)
29+
30+
clean :
31+
rm -rf $(OBJ)
32+
33+
fclean :
34+
rm -rf $(OBJ)
35+
rm -rf $(NAME)
36+
37+
re :
38+
make fclean
39+
make $(NAME)

cpp08/ex00/easyfind.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* easyfind.hpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/04/11 17:15:13 by agunczer #+# #+# */
9+
/* Updated: 2022/04/13 09:49:54 by agunczer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include <iostream>
14+
#include <vector>
15+
#include <list>
16+
#include <array>
17+
#include <algorithm>
18+
#include <string>
19+
20+
template <typename T>
21+
typename T::iterator easyfind(T cont, const int& toFind) {
22+
typename T::iterator it = std::find(cont.begin(), cont.end(), toFind);
23+
24+
if (it == cont.end())
25+
{
26+
std::cout << "not found" << std::endl;
27+
return it;
28+
}
29+
else
30+
return it;
31+
}

cpp08/ex00/main.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* main.cpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/04/11 17:15:15 by agunczer #+# #+# */
9+
/* Updated: 2022/04/13 09:40:08 by agunczer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "easyfind.hpp"
14+
15+
int main(void) {
16+
{
17+
std::vector<int> vect;
18+
vect.push_back(10);
19+
vect.push_back(20);
20+
vect.push_back(30);
21+
22+
std::vector<int>::iterator it = easyfind(vect, 9);
23+
std::vector<int>::iterator it2 = easyfind(vect, 30);
24+
25+
std::cout << &(*it) << std::endl;
26+
std::cout << &(*it2) << std::endl;
27+
28+
// return 0;
29+
}
30+
31+
{
32+
std::list<int> list;
33+
34+
list.push_back(40);
35+
list.push_back(50);
36+
list.push_back(60);
37+
38+
std::list<int>::iterator it = easyfind(list, 62);
39+
std::list<int>::iterator it2 = easyfind(list, 60);
40+
41+
std::cout << &(*it) << std::endl;
42+
std::cout << &(*it2) << std::endl;
43+
44+
// return 0;
45+
}
46+
47+
{
48+
std::array<int, 3> array = {1, 2 ,3};
49+
50+
std::array<int, 3>::iterator it1 = easyfind(array, 0);
51+
std::array<int, 3>::iterator it2 = easyfind(array, 1);
52+
std::array<int, 3>::iterator it3 = easyfind(array, 2);
53+
std::array<int, 3>::iterator it4 = easyfind(array, 3);
54+
std::array<int, 3>::iterator it5 = easyfind(array, 4);
55+
56+
std::cout << "address of it1 at: " << &(*it1) << std::endl;
57+
std::cout << "address of it2 at: " << &(*it2) << std::endl;
58+
std::cout << "address of it3 at: " << &(*it3) << std::endl;
59+
std::cout << "address of it4 at: " << &(*it4) << std::endl;
60+
std::cout << "address of it5 at: " << &(*it5) << std::endl;
61+
62+
// return 0;
63+
}
64+
65+
// {
66+
// std::vector<int[3]> vect;
67+
// int k[3] = {1, 2, 3};
68+
// vect.push_back(k);
69+
// vect.push_back(k);
70+
// vect.push_back(k);
71+
72+
// std::vector<int[3]>::iterator it = easyfind(vect, 1);
73+
// std::vector<int[3]>::iterator it2 = easyfind(vect, 30);
74+
75+
// std::cout << &(*it) << std::endl;
76+
// std::cout << &(*it2) << std::endl;
77+
78+
// // return 0;
79+
// }
80+
}

cpp08/ex01/Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# **************************************************************************** #
2+
# #
3+
# ::: :::::::: #
4+
# Makefile :+: :+: :+: #
5+
# +:+ +:+ +:+ #
6+
# By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ #
7+
# +#+#+#+#+#+ +#+ #
8+
# Created: 2022/04/01 18:10:56 by agunczer #+# #+# #
9+
# Updated: 2022/04/13 15:37:01 by agunczer ### ########.fr #
10+
# #
11+
# **************************************************************************** #
12+
13+
NAME = serialize
14+
15+
CC = clang++
16+
17+
CFLAGS = -Wall -Wextra -Werror -std=c++98
18+
19+
SRC = main.cpp Span.cpp
20+
21+
OBJ = $(SRC:.cpp=.o)
22+
23+
HEAD = Span.hpp
24+
all : $(NAME)
25+
26+
$(NAME) : $(SRC) $(HEAD)
27+
$(CC) $(CFLAGS) -c $(SRC)
28+
$(CC) $(CFLAGS) $(SRC) -o $(NAME)
29+
30+
clean :
31+
rm -rf $(OBJ)
32+
33+
fclean :
34+
rm -rf $(OBJ)
35+
rm -rf $(NAME)
36+
37+
re :
38+
make fclean
39+
make $(NAME)

cpp08/ex01/Span.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* Span.cpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/04/13 15:37:09 by agunczer #+# #+# */
9+
/* Updated: 2022/04/13 19:04:36 by agunczer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "Span.hpp"
14+
15+
Span::Span(unsigned int N) : _N(N) {}
16+
Span::~Span() {}
17+
Span::Span( const Span& obj ) {
18+
this->_N = obj._N;
19+
std::copy(obj._vect.begin(), obj._vect.end(), this->_vect.begin());
20+
}
21+
Span& Span::operator=( const Span& obj ) {
22+
this->_N = obj._N;
23+
std::copy(obj._vect.begin(), obj._vect.end(), this->_vect.begin());
24+
return *this;
25+
}
26+
27+
void Span::addNumber( int number ) {
28+
if (this->_vect.size() < _N) _vect.push_back(number);
29+
else throw std::runtime_error("You Failed\n");
30+
}
31+
32+
int Span::shortestSpan() {
33+
std::vector<int>::iterator i1;
34+
std::vector<int>::iterator j1;
35+
int shortest = INT_MAX;
36+
37+
for ( i1 = _vect.begin(); i1 != _vect.end(); i1++)
38+
{
39+
for (j1 = std::next(i1, 1); j1 != _vect.end(); j1++)
40+
{
41+
// std::cout << abs(*j1 - *i1) << std::endl;
42+
if (abs(*j1 - *i1) < shortest) shortest = abs(*j1 - *i1);
43+
}
44+
45+
}
46+
return shortest;
47+
}
48+
49+
int Span::longestSpan() {
50+
std::vector<int>::iterator i1;
51+
std::vector<int>::iterator j1;
52+
int longest = 0;
53+
54+
for ( i1 = _vect.begin(); i1 != _vect.end(); ++i1)
55+
{
56+
for (j1 = std::next(i1, 1); j1 != _vect.end(); ++j1)
57+
{
58+
if (abs(*j1 - *i1) > longest) longest = abs(*j1 - *i1);
59+
}
60+
}
61+
return longest;
62+
}
63+
64+
void Span::fillSpan() {
65+
_vect.assign(_N, true);
66+
}

cpp08/ex01/Span.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* Span.hpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/04/13 15:37:07 by agunczer #+# #+# */
9+
/* Updated: 2022/04/13 18:57:14 by agunczer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef SPAN_HPP
14+
#define SPAN_HPP
15+
16+
#include <vector>
17+
#include <algorithm>
18+
#include <limits.h>
19+
#include <iostream>
20+
21+
class Span {
22+
private:
23+
unsigned int _N;
24+
25+
public:
26+
std::vector<int> _vect;
27+
Span(unsigned int N);
28+
~Span();
29+
Span( const Span& obj );
30+
Span& operator=( const Span& obj );
31+
32+
// const int& getVect( int index );
33+
// void setVect( int index, int value );
34+
35+
void addNumber( int number );
36+
int shortestSpan();
37+
int longestSpan();
38+
void fillSpan();
39+
};
40+
41+
#endif

cpp08/ex01/main.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* main.cpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: agunczer <agunczer@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/04/13 15:37:12 by agunczer #+# #+# */
9+
/* Updated: 2022/04/13 19:03:21 by agunczer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "Span.hpp"
14+
15+
16+
int main(void) {
17+
Span *var = new Span(5);
18+
19+
var->fillSpan();
20+
var->addNumber(7);
21+
22+
std::cout << var->_vect[0] << std::endl;
23+
}
24+
25+
// int main()
26+
// {
27+
// Span sp = Span(5);
28+
// sp.addNumber(6);
29+
// sp.addNumber(3);
30+
// sp.addNumber(17);
31+
// sp.addNumber(9);
32+
// sp.addNumber(11);
33+
// std::cout << sp.shortestSpan() << std::endl;
34+
// std::cout << sp.longestSpan() << std::endl;
35+
// return 0;
36+
// }

0 commit comments

Comments
 (0)