Repository navigation
Expand file tree
/
Copy pathCustomPair.cpp
More file actions
157 lines (137 loc) · 7.4 KB
/
Copy pathCustomPair.cpp
File metadata and controls
157 lines (137 loc) · 7.4 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <algorithm> // For std::sort
// C++11 compatible macro to define a custom pair-like structure
#define DefineCustomPair(StructName, FirstType, FirstName, SecondType, SecondName) \
struct StructName : public std::pair<FirstType, SecondType> \
{ \
typedef std::pair<FirstType, SecondType> Base; \
\
FirstType &FirstName; \
SecondType &SecondName; \
\
StructName() \
: Base(), FirstName(this->first), SecondName(this->second) {} \
\
StructName(const FirstType &inFirst, const SecondType &inSecond) \
: Base(inFirst, inSecond), FirstName(this->first), SecondName(this->second) {} \
\
StructName(FirstType &&inFirst, SecondType &&inSecond) \
: Base(std::move(inFirst), std::move(inSecond)), \
FirstName(this->first), SecondName(this->second) {} \
\
StructName(const StructName &other) \
: Base(other), FirstName(this->first), SecondName(this->second) {} \
\
StructName(StructName &&other) \
: Base(std::move(other)), FirstName(this->first), SecondName(this->second) {} \
\
StructName &operator=(const StructName &other) \
{ \
Base::operator=(other); \
return *this; \
} \
\
StructName &operator=(StructName &&other) \
{ \
Base::operator=(std::move(other)); \
return *this; \
} \
\
private: \
using Base::first; \
using Base::second; \
};
/******************************************************************/
/*************************** TEST *********************************/
// Definition of the custom pair structure NameAgePair
DefineCustomPair(NameAgePair, std::string, name, int, age)
int main()
{
std::cout << "--- Construction Test ---" << std::endl;
NameAgePair p1("Alice", 30);
std::cout << "p1: " << p1.name << ", " << p1.age << std::endl;
std::cout << "\n--- Copy Test ---" << std::endl;
NameAgePair p2 = p1;
std::cout << "p2 (copy of p1): " << p2.name << ", " << p2.age << std::endl;
std::cout << "\n--- Move Test ---" << std::endl;
NameAgePair p3 = std::move(p1);
std::cout << "p3 (moved from p1): " << p3.name << ", " << p3.age << std::endl;
// Note: p1 might be in an indeterminate state after the move
std::cout << "\n--- Assignment Test (Copy) ---" << std::endl;
NameAgePair p4;
p4 = p2;
std::cout << "p4 (assigned from p2): " << p4.name << ", " << p4.age << std::endl;
std::cout << "\n--- Modification Through Alias Test ---" << std::endl;
p4.name = "Bob";
p4.age = 40;
std::cout << "p4 (modified): " << p4.name << ", " << p4.age << std::endl;
std::cout << "\n--- Usage with std::vector Test ---" << std::endl;
std::vector<NameAgePair> people;
people.emplace_back("Charlie", 25);
people.push_back(NameAgePair("David", 35));
NameAgePair eve("Eve", 28);
people.push_back(eve);
std::cout << "Elements in the vector:" << std::endl;
for (const auto &person : people)
{
std::cout << person.name << ", " << person.age << std::endl;
}
std::cout << "\n--- Modification of a Vector Element Test ---" << std::endl;
if (!people.empty())
{
people[0].name = "Charles";
people[0].age = 26;
std::cout << "First element modified: " << people[0].name << ", " << people[0].age << std::endl;
}
std::cout << "\n--- Comparison Test (Operator == inherited from std::pair) ---" << std::endl;
NameAgePair person1("Alice", 30);
NameAgePair person2("Alice", 30);
NameAgePair person3("Bob", 25);
if (person1 == person2)
{
std::cout << "person1 and person2 are equal." << std::endl;
}
else
{
std::cout << "person1 and person2 are different." << std::endl;
}
if (person1 == person3)
{
std::cout << "person1 and person3 are equal." << std::endl;
}
else
{
std::cout << "person1 and person3 are different." << std::endl;
}
std::cout << "\n--- Comparison with std::vector Test (Search) ---" << std::endl;
std::vector<NameAgePair> more_people;
more_people.emplace_back("Charlie", 25);
more_people.emplace_back("David", 35);
more_people.emplace_back("Charlie", 25);
NameAgePair search_person("Charlie", 25);
int count = 0;
for (const auto &person : more_people)
{
if (person == search_person)
{
count++;
}
}
std::cout << "There are " << count << " people with name 'Charlie' and age 25 in the vector." << std::endl;
std::cout << "\n--- Sorting Test with std::sort (Operator < inherited from std::pair) ---" << std::endl;
std::vector<NameAgePair> unsorted_people;
unsorted_people.emplace_back("Bob", 25);
unsorted_people.emplace_back("Alice", 30);
unsorted_people.emplace_back("Charlie", 20);
unsorted_people.emplace_back("Alice", 25); // Same name, different age
std::sort(unsorted_people.begin(), unsorted_people.end());
std::cout << "Sorted people (by name, then by age):" << std::endl;
for (const auto &person : unsorted_people)
{
std::cout << person.name << ", " << person.age << std::endl;
}
return 0;
}