ex01: init

This commit is contained in:
starnakin 2023-09-21 13:03:30 +02:00
parent d65e637f00
commit 23311296d0
3 changed files with 61 additions and 0 deletions

26
ex01/Makefile Normal file
View File

@ -0,0 +1,26 @@
CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
SRCDIR := src
OBJDIR := obj
NAME := ex01
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p $(OBJDIR)
$(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

10
ex01/src/iter.hpp Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <stddef.h>
template<typename T, typename U>
void iter(T* arr, size_t len, void(*f)(U))
{
for (size_t i = 0; i < len; i++)
f(arr[i]);
}

25
ex01/src/main.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "iter.hpp"
#include <iostream>
template<typename T>
void print(T& t)
{
std::cout << t << std::endl;
}
int main()
{
int array[] = {0, 1, 2, 3, 4, 5, 6};
iter(array, 7, print<int>);
std::string strs[] = {
"Hello World",
"Bonjour Monde",
"Hallo Welt",
"Hola Mundo"
};
iter(strs, 4, print<std::string>);
}