8.0 开篇

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
#include <generator>
#include <iostream>
#include <coroutine>
#include <ranges>

class Values
{
public:

std::generator<int> operator()()
{
co_yield 1;
co_yield 2;
co_yield 3;
}

};

int main(void)
{
Values v;
for (auto i : v())
std::cout << i << ' ';

return 0;
}

8.1 支持数组形式的属性

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
57
#include <iostream>
#include <algorithm>

class Creatrue
{
int strength, agility, intelligence;
public:
int get_strength() const
{
return strength;
}

void set_strength(int strength)
{
Creature::strength = strength;
}
int get_agility() const
{
return agility;
}

void set_agility(int agility)
{
Creature::agility = agility;
}

int get_intelligence() const
{
return intelligence;
}

void set_intelligence(int intelligence)
{
Creature::intelligence = intelligence;
}

int sum() const
{
return strength + agility + intelligence;
}

double average() const
{
return sum() / 3.0;
}

int max() const
{
return std::max(std::max(strength, agility), intelligence);
}
};

int main(void)
{

return 0;
}
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
57
58
59
60
61
#include <iostream>
#include <algorithm>
#include <array>
#include <numeric>

class Creatrue
{
//int strength, agility, intelligence;
enum Abilities { str, agl, intl, count };
std::array<int, count> abilities;
public:
int get_strength() const
{
return abilities[str];
}

void set_strength(int value)
{
abilities[str] = value;
}
int get_agility() const
{
return abilities[agl];
}

void set_agility(int value)
{
abilities[agl] = value;
}

int get_intelligence() const
{
return abilities[intl];
}

void set_intelligence(int value)
{
abilities[intl] = value;
}

int sum() const
{
return std::accumulate(abilities.begin(), abilities.end(), 0);
}

double average() const
{
return sum() / (double)count;
}

int max() const
{
return *std::max_element(abilities.begin(), abilities.end());
}
};

int main(void)
{

return 0;
}

8.2 组合图形对象

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
#include <iostream>
#include <string>
#include <vector>

struct GraphicObject
{
virtual void draw() = 0;
};

struct Circle : GraphicObject
{
void draw() override
{
std::cout << "Circle" << std::endl;
}
};

struct Group : GraphicObject
{
std::string name;

explicit Group(const std::string& name)
: name{name} {}

void draw() override
{
std::cout << "Group " << name.c_str() << " contains:" << std::endl;
for (auto&& o : objects)
o->draw();
}

std::vector<GraphicObject*> objects;
};

int main(void)
{
Group root("root");
Circle c1, c2;
root.objects.push_back(&c1);

Group subgroup("sub");
subgroup.objects.push_back(&c2);

root.objects.push_back(&subgroup);
root.draw();

return 0;
}

8.3 神经网络

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
#include <memory>

struct Neuron;

template <typename Self>
struct SomeNeurons : std::vector<Neuron>
{
template <typename T> void connect_to(T& other)
{
for (Neuron& from : *static_cast<Self*>(this))
{
std::cout << typeid(T).name() << std::endl;
std::cout << typeid(other.begin()).name() << std::endl;
for (Neuron& to : other)
{
from.out.push_back(&to);
to.in.push_back(&from);
}
}
}

virtual Neuron* begin() = 0;
virtual Neuron* end() = 0;

virtual ~SomeNeurons() {};
};

struct Neuron : public SomeNeurons<Neuron>
{
std::vector<Neuron*> in, out;
unsigned int id;

Neuron()
{
static int id = 1;
this->id = id++;
}

Neuron* begin() override { return this; }
Neuron* end() override { return this + 1; }
};
/*
template<> void connect_to<Neuron>(Neuron& other)
{
out.push_back(&other);
other.in.push_back(this);
}
*/

struct NeuronLayer : std::vector<Neuron>, public SomeNeurons<std::vector<Neuron>>
{
std::vector<Neuron> Neurons;
NeuronLayer(int count = 0)
{
while (count --> 0)
Neurons.emplace_back(Neuron{});
}

Neuron* begin() override { return &(*Neurons.begin()); }
Neuron* end() override { return &(*Neurons.end()); }
};

int main(void)
{
Neuron neuron, neuron2;
NeuronLayer layer, layer2;

neuron.connect_to(neuron2);
neuron.connect_to(layer);
layer.connect_to(neuron);
layer.connect_to(layer2);

return 0;
}

8.3.1 封装组合模式

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
#include <memory>

struct Neuron;

template <typename Self>
struct SomeNeurons : std::vector<Neuron>
{
template <typename T> void connect_to(T& other)
{
for (Neuron& from : *static_cast<Self*>(this))
{
std::cout << typeid(T).name() << std::endl;
std::cout << typeid(other.begin()).name() << std::endl;
for (Neuron& to : other)
{
from.out.push_back(&to);
to.in.push_back(&from);
}
}
}

virtual Neuron* begin() = 0;
virtual Neuron* end() = 0;

virtual ~SomeNeurons() {};
};

template <typename T> class Scalar : public SomeNeurons<T>
{
public:
T* begin() override {return reinterpret_cast<T*>(this);}
T* end() override {return reinterpret_cast<T*>(this) + 1;}
};

struct Neuron : public Scalar<Neuron>
{
std::vector<Neuron*> in, out;
unsigned int id;

Neuron()
{
static int id = 1;
this->id = id++;
}

//Neuron* begin() override { return this; }
//Neuron* end() override { return this + 1; }
};

struct NeuronLayer : std::vector<Neuron>, public SomeNeurons<std::vector<Neuron>>
{
std::vector<Neuron> Neurons;
NeuronLayer(int count = 0)
{
while (count --> 0)
Neurons.emplace_back(Neuron{});
}

Neuron* begin() override { return &(*Neurons.begin()); }
Neuron* end() override { return &(*Neurons.end()); }
};

int main(void)
{
Neuron neuron, neuron2;
NeuronLayer layer, layer2;

neuron.connect_to(neuron2);
neuron.connect_to(layer);
layer.connect_to(neuron);
layer.connect_to(layer2);

return 0;
}

8.3.2 概念上的改进

(下述代码无法运行,书中也有说概念上的改进存在问题 Scalar类本身是可迭代的,就要求Neuron也可以迭代, 但Neuron本身是不可迭代的)

#include <iostream>
#include <vector>
#include <memory>
#include <concepts>

template <typename T> concept Iterable =
        requires(T& t)
        {
                t.begin();
                t.end();
        } || requires(T& t)
        {
                begin(t);
                end(t);
        };

struct Neuron;

template <Iterable Self>
struct SomeNeurons //: std::vector<Neuron>
{
        //template <typename T>
        template <Iterable T>
        void connect_to(T& other)
        {
                for (Neuron& from : *static_cast<Self*>(this))
                {
                        std::cout << typeid(T).name() << std::endl;
                        std::cout << typeid(other.begin()).name() << std::endl;
                        for (Neuron& to : other)
                        {
                                from.out.push_back(&to);
                                to.in.push_back(&from);
                        }
                }
        }

        virtual Neuron* begin() = 0;
        virtual Neuron* end() = 0;

        virtual ~SomeNeurons() {};
};

template <Iterable T> class Scalar : public SomeNeurons<T>
{
public:
        T* begin() override {return reinterpret_cast<T*>(this);}
        T* end() override {return reinterpret_cast<T*>(this) + 1;}
};

struct Neuron : public Scalar<Neuron>
{
        std::vector<Neuron*> in, out;
        unsigned int id;

        Neuron()
        {
                static int id = 1;
                this->id = id++;
        }

        //Neuron* begin() override { return this; }
        //Neuron* end() override { return this + 1; }
};

struct NeuronLayer : std::vector<Neuron>, public SomeNeurons<std::vector<Neuron>>
{
        std::vector<Neuron> Neurons;
        NeuronLayer(int count = 0)
        {
                while (count --> 0)
                        Neurons.emplace_back(Neuron{});
        }

        Neuron* begin() override { return &(*Neurons.begin()); }
        Neuron* end() override { return &(*Neurons.end()); }
};

int main(void)
{
        Neuron neuron, neuron2;
        NeuronLayer layer, layer2;

        neuron.connect_to(neuron2);
        neuron.connect_to(layer);
        layer.connect_to(neuron);
        layer.connect_to(layer2);

        return 0;
}
、、、