add: ex00

This commit is contained in:
Camille Chauvet 2023-09-25 13:19:15 +00:00
commit 55f136f3e7
4 changed files with 68 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*
!/**/
!*.*
!/**/Makefile
*.o

26
ex00/Makefile Normal file
View File

@ -0,0 +1,26 @@
CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
SRCDIR := src
OBJDIR := obj
NAME := ex00
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CXXFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)
fclean: clean
rm -fr $(NAME)
re: fclean
@make --no-print-directory all

9
ex00/src/easyfind.hpp Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <algorithm>
template<typename T>
typename T::iterator easyfind(T& container, int to_find)
{
return std::find(container.begin(), container.end(), to_find);
}

28
ex00/src/main.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "easyfind.hpp"
#include <string>
#include <iostream>
#include <vector>
int main()
{
std::cout << "------------ std::string -------------" << std::endl;
std::string str("bozoman");
std::cout << "find b in " << str << ":" << *easyfind(str, 'b') << std::endl;
std::cout << "find r in " << str << ":" << *easyfind(str, 'r') << std::endl;
std::cout << "find n in " << str << ":" << *easyfind(str, 'n') << std::endl;
std::cout << std::endl;
std::cout << "------------ std::vector -------------" << std::endl;
std::vector<int> vec;
vec.push_back(0);
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
std::cout << "find 0 in " << str << ":" << *easyfind(vec, 0) << std::endl;
std::cout << "find 14 in " << str << ":" << *easyfind(vec, 14) << std::endl;
std::cout << "find 5 in " << str << ":" << *easyfind(vec, 5) << std::endl;
}