CPP07 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/07 21:15:51 by kamitsui #+# #+# */
9/* Updated: 2025/08/07 22:21:55 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "iter.hpp"
19#include <string>
20
25void addExclamation(std::string &s) { s += "!"; }
26
35int main(void) {
36 // --- Test 1: int array (non-const) ---
37 std::cout << "--- Test 1: int array (non-const) ---" << std::endl;
38 int intArray[] = {0, 1, 2, 3, 4};
39 size_t intArrayLength = 5;
40
41 std::cout << "Original: ";
42 iter(intArray, intArrayLength, printElement<int>);
44
46
47 std::cout << "After increment: ";
50
51 // --- Test 2: const float array ---
52 std::cout << "--- Test 2: const float array ---" << std::endl;
53
54#ifndef DEBUG_MODE // Normal mode build : only 'make'
55 const float floatArray[] = {1.1f, 2.2f, 3.3f};
56 size_t floatArrayLength = 3;
57
58 std::cout << "Printing const array: ";
59 // The const-overloaded version of iter() is called here.
61#endif
62
63#ifdef DEBUG_MODE // Debug mode build : 'make debug'
64 const float floatArray[] = {1.1f, 2.2f, 3.3f};
65 size_t floatArrayLength = 3;
66
67 std::cout << "Printing const array: ";
69
70 // Note: The following line would cause a compile error. (which is correct)
72
73 std::cout << "After increment: ";
75#endif
76
78
79 // --- Test 3: string array (non-const) ---
80 std::cout << "--- Test 3: string array (non-const) ---" << std::endl;
81 std::string stringArray[] = {"Hello", "World", "42"};
82 size_t stringArrayLength = 3;
83
84 std::cout << "Original: ";
87
89
90 std::cout << "After modification: ";
93
94 return 0;
95}
T endl(T... args)
int main(void)
The main entry point of the program.
Definition main.cpp:29
void addExclamation(std::string &s)
A helper function that adds an exclamation mark to a string.
Definition main.cpp:25
Contains a template function to apply a function to each element of an array.
void printElement(const T &element)
Prints an element to the standard output.
Definition iter.hpp:65
void iter(T *array, size_t length, void(*f)(T &))
Applies a function to each element of an array. This version is for non-const arrays and allows modif...
Definition iter.hpp:32