-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector.cpp
56 lines (43 loc) · 1.31 KB
/
vector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: BSL-1.0
#undef NDEBUG
#include <etl/cassert.hpp>
#include <etl/vector.hpp>
struct Person {
constexpr Person(int a, int e) noexcept
: age{a}
, experience{e}
{
}
friend constexpr auto operator==(Person lhs, Person rhs) noexcept -> bool = default;
int age{};
int experience{};
};
auto main() -> int
{
// Unlike a std::vector you will have to decide which maximum capacity you
// need. Apart from that it behaves almost the same as the standard version.
etl::static_vector<Person, 32> people{};
assert(people.empty());
assert(people.capacity() == 32);
// You can push_back/emplace_back into the vector
people.push_back(Person{20, 0});
assert(people.size() == 1);
assert(people.back().age == 20);
people.emplace_back(90, 100);
assert(people.size() == 2);
assert(people.back().age == 90);
// You can make copies.
auto const copy = people;
// You can compare vectors
assert(copy == people);
// You can apply algorithms.
auto levelUp = [](auto p) {
p.experience += 1;
return p;
};
etl::transform(begin(people), end(people), begin(people), levelUp);
assert(people[0].experience == 1);
assert(people[1].experience == 101);
assert(copy != people);
return 0;
}