Skip to content
Snippets Groups Projects
Commit 02774a29 authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Chapter 5 examples

parent a0106d24
No related branches found
No related tags found
No related merge requests found
cmake_minimum_required(VERSION 3.28)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(cxx_course_examples CXX)
FILE (GLOB sources ./ *.cc)
foreach(source ${sources})
get_filename_component(withoutext "${source}" NAME_WE)
add_executable("${withoutext}" "${source}")
endforeach()
#include "Vbose.hh"
auto main() -> int
{
std::cout << "Declaring lambda function {\n";
Vbose locvar{ "dinosaur" };
auto lambda = [=](int i) {
std::cout << "\nIn lambda function, captured value of locvar is : "
<< locvar.getval() << "\n";
return i * i * i;
};
std::cout << "Declaring lambda function }\n";
std::cout << "Calling lambda function {\n";
std::cout << 5 << " -> " << lambda(5) << "\n";
std::cout << "Calling lambda function }\n";
}
#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
auto main() -> int
{
using namespace std;
vector<unsigned long> v, w;
generate_n(back_inserter(v), 100, [i = 0UL]() mutable {
++i;
return i * i;
});
// v = [1, 4, 9, 16 ... ]
std::cout << " v = \n";
for (auto el : v)
std::cout << el << "\n";
generate_n(back_inserter(w), 50, [ i = 0UL, j = 1UL ]() mutable {
i = std::exchange(j, j + i);
return i;
});
// w = [1, 1, 2, 3, 5, 8, 11 ...]
std::cout << " w = \n";
for (auto&& el : w)
std::cout << el << "\n";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment