CPP08 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/21 23:40:55 by kamitsui #+# #+# */
9/* Updated: 2025/08/22 00:02:16 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
25#include "easyfind.hpp"
26#include <iostream>
27#include <list>
28#include <vector>
29
36template <typename T> void printContainer(const T &container, const std::string &name) {
37 std::cout << name << " contains: ";
38 for (typename T::const_iterator it = container.begin(); it != container.end(); ++it) {
39 std::cout << *it << " ";
40 }
42}
43
49int main() {
50 // --- Test with std::vector ---
51 std::cout << "--- Testing with std::vector ---" << std::endl;
52 std::vector<int> myvector;
53 for (int i = 1; i <= 5; i++)
54 myvector.push_back(i * 10); // myvector: 10 20 30 40 50
55 printContainer(myvector, "myvector");
56
57 // Test 1: Find an existing element
58 std::cout << "\nSearching for 30..." << std::endl;
59 try {
60 std::vector<int>::iterator it = easyfind(myvector, 30);
61 std::cout << "Found value: " << *it << " at address " << &(*it) << std::endl;
62 } catch (const std::exception &e) {
63 std::cerr << "Error: " << e.what() << std::endl;
64 }
65
66 // Test 2: Find a non-existing element
67 std::cout << "\nSearching for 99..." << std::endl;
68 try {
69 easyfind(myvector, 99);
70 } catch (const std::exception &e) {
71 std::cerr << "Correctly caught exception for 99: " << e.what() << std::endl;
72 }
73
74 // --- Test with std::list ---
75 std::cout << "\n--- Testing with std::list ---" << std::endl;
76 std::list<int> mylist;
77 for (int i = 1; i <= 5; i++)
78 mylist.push_back(i * 11); // mylist: 11 22 33 44 55
79 printContainer(mylist, "mylist");
80
81 // Test 3: Find an existing element
82 std::cout << "\nSearching for 44..." << std::endl;
83 try {
84 std::list<int>::iterator it = easyfind(mylist, 44);
85 std::cout << "Found value: " << *it << " at address " << &(*it) << std::endl;
86 } catch (const std::exception &e) {
87 std::cerr << "Error: " << e.what() << std::endl;
88 }
89
90 // Test 4: Find a non-existing element
91 std::cout << "\nSearching for 1..." << std::endl;
92 try {
93 easyfind(mylist, 1);
94 } catch (const std::exception &e) {
95 std::cerr << "Correctly caught exception for 1: " << e.what() << std::endl;
96 }
97
98 return 0;
99}
Provides template functions for searching elements within containers.
T::iterator easyfind(T &container, int value)
T endl(T... args)
void printContainer(const T &container, const std::string &name)
Helper function to display container elements to standard output.
Definition main.cpp:36
int main()
Program entry point.
Definition main.cpp:49
T push_back(T... args)
T what(T... args)