feat: range based for
This commit is contained in:
parent
09a1efeedf
commit
f3c8cad317
4 changed files with 40 additions and 4 deletions
|
@ -1,12 +1,13 @@
|
|||
add_library(
|
||||
chapter_02
|
||||
src/constexp.cpp
|
||||
src/if_constexpr.cpp
|
||||
src/ifswitch.cpp
|
||||
src/initlist.cpp
|
||||
src/null.cpp
|
||||
src/constexp.cpp
|
||||
src/ifswitch.cpp
|
||||
src/range_based_for.cpp
|
||||
src/structured_binding.cpp
|
||||
src/type_inference.cpp
|
||||
src/if_constexpr.cpp)
|
||||
src/type_inference.cpp)
|
||||
|
||||
target_compile_features(chapter_02 PUBLIC cxx_std_20)
|
||||
|
||||
|
|
9
chapters/chapter_02/include/range_based_for.h
Normal file
9
chapters/chapter_02/include/range_based_for.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "chapter_interface.h"
|
||||
|
||||
class RangeBasedForTest : public ChapterTest {
|
||||
public:
|
||||
virtual void run() override;
|
||||
virtual const char *name() const override { return "Range based for test"; }
|
||||
};
|
24
chapters/chapter_02/src/range_based_for.cpp
Normal file
24
chapters/chapter_02/src/range_based_for.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "range_based_for.h"
|
||||
|
||||
void RangeBasedForTest::run() {
|
||||
std::vector<int> vals = {1, 2, 3, 4, 5};
|
||||
|
||||
std::cout << "vals: ";
|
||||
for (auto val : vals) {
|
||||
std::cout << val << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
for (auto &val : vals) {
|
||||
val *= 2;
|
||||
}
|
||||
|
||||
std::cout << "vals: ";
|
||||
for (auto val : vals) {
|
||||
std::cout << val << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
#include "ifswitch.h"
|
||||
#include "initlist.h"
|
||||
#include "null.h"
|
||||
#include "range_based_for.h"
|
||||
#include "structured_binding.h"
|
||||
#include "type_inference.h"
|
||||
#include "version.h"
|
||||
|
@ -26,6 +27,7 @@ int main(int argc, char **argv) {
|
|||
chapter_2_tests.emplace_back(std::make_unique<StructuredBindingTest>());
|
||||
chapter_2_tests.emplace_back(std::make_unique<TypeInferenceTest>());
|
||||
chapter_2_tests.emplace_back(std::make_unique<IfConstexprTest>());
|
||||
chapter_2_tests.emplace_back(std::make_unique<RangeBasedForTest>());
|
||||
|
||||
for (auto &test : chapter_2_tests) {
|
||||
std::cout << "\n" << test->name() << ":" << std::endl;
|
||||
|
|
Loading…
Reference in a new issue