From 066627f5e24db91522d66e62a52d5b7b6a5c511f Mon Sep 17 00:00:00 2001 From: denes Date: Thu, 3 Oct 2019 16:45:47 +0200 Subject: [PATCH] Taking out the 'logic' to a header --- CMakeLists.txt | 5 ++++- dummy_lib.hpp | 17 +++++++++++++++++ main.cpp | 11 +++-------- 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 dummy_lib.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0df3d49..06b711a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,6 @@ cmake_minimum_required (VERSION 3.10) project (PerformanceMeasuring) -add_executable(performance_measuring main.cpp) + +set(HEADER_FILES dummy_lib.hpp) + +add_executable(performance_measuring main.cpp ${HEADER_FILES}) diff --git a/dummy_lib.hpp b/dummy_lib.hpp new file mode 100644 index 0000000..e421ce1 --- /dev/null +++ b/dummy_lib.hpp @@ -0,0 +1,17 @@ +#ifndef DUMMY_LIB_HPP +#define DUMMY_LIB_HPP + +#include +#include + +namespace performance_measuring { + +int compute(const std::vector& v) +{ + int min = *std::min_element(v.begin(), v.end()); + return std::count(v.begin(), v.end(), min); +} + +} + +#endif diff --git a/main.cpp b/main.cpp index d654b7c..8ca3223 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,8 @@ #include #include -#include + +#include "dummy_lib.hpp" std::vector generate_random_numbers(int n, int min, int max) @@ -15,19 +16,13 @@ std::vector generate_random_numbers(int n, int min, int max) return v; } -int compute(const std::vector& v) -{ - int min = *std::min_element(v.begin(), v.end()); - return std::count(v.begin(), v.end(), min); -} - int main(int /*argc*/, char* /*argv*/[]) { std::vector v = generate_random_numbers(10000, 0, 100); // How many times does the smallest element occurst in the vector? - int r = compute(v); + int r = performance_measuring::compute(v); (void)r;