The complete code: ###example.cpp
example.cpp
The complete code: ###example.cpp
The complete code:
example.cpp
- 87.7k
- 14
- 104
- 325
I am currently working on a bigger project were i came towhere I found a part of it which seems very ugly to me from the code.
TypeA
, TypeB
and TypeC
are all different objects with different sizes in the real programm. The comman part is they all have a nono
:
All Containers are ordered by ascending nono
:
All Numbers are unique, e. Thers nog. there's no 1no==1
in a and bb
or c
. The index numbers come consecutivelyare consecutive and fill the data structures.
I want to iterate over the whole struct Data
by the nono
of the different data members and do sthsomething with the additional shared things the containers have.
To empathizeemphasize the ugliness i one time, I sometimes print data1data1
and the other time data2data2
but the iteration is exactly the same.
My idea would be writing an custom iterator for DataData
which iterates by the nono
. But i dontI don't know what is the best approach.
ImI'm open forto any suggestions.
The complete code: example.cpp###example.cpp
#include <vector>
#include <limits>
#include <iostream>
#include <string>
struct TypeA {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
struct TypeB {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
struct TypeC {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
// In reality TypeA, TypeB and TypeC are not exactly the same
struct Data {
std::vector<TypeA> a;
std::vector<TypeB> b;
std::vector<TypeC> c;
// In reality there are not only 3 values
// but > 20 to print consecutively by no
void print_data_in_order_of_no();
void print_data2_in_order_of_no();
};
void Data::print_data_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data << '\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
void Data::print_data2_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data2 <<'\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data2 << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data2 << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data2_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
int main()
{
Data d;
d.a.push_back(TypeA{1, "1" , "11"});
d.a.push_back(TypeA{ 4, "4", "14" });
d.b.push_back(TypeB{ 3, "3", "13" });
d.b.push_back(TypeB{ 6, "6", "16" });
d.c.push_back(TypeC{ 2, "2", "12" });
d.c.push_back(TypeC{ 5, "5", "15" });
d.print_data_in_order_of_no();
d.print_data2_in_order_of_no();
std::cin.get();
}
void Data::print_data_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data << '\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
void Data::print_data2_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data2 <<'\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data2 << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data2 << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data2_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
int main()
{
Data d;
d.a.push_back(TypeA{1, "1" , "11"});
d.a.push_back(TypeA{ 4, "4", "14" });
d.b.push_back(TypeB{ 3, "3", "13" });
d.b.push_back(TypeB{ 6, "6", "16" });
d.c.push_back(TypeC{ 2, "2", "12" });
d.c.push_back(TypeC{ 5, "5", "15" });
d.print_data_in_order_of_no();
d.print_data2_in_order_of_no();
std::cin.get();
}
The code for review is the Data
struct and its members.
I am currently working on a bigger project were i came to a part of it which seems very ugly to me from the code.
TypeA
, TypeB
and TypeC
are all different objects with different sizes in the real programm. The comman part is they all have a no:
All Containers are ordered by ascending no:
All Numbers are unique. Thers no no 1 in a and b. The index numbers come consecutively and fill the data structures.
I want to iterate over the whole struct Data
by the no of the different data members and do sth with the additional shared things the containers have.
To empathize the ugliness i one time print data1 and the other time data2 but the iteration is exactly the same.
My idea would be writing an custom iterator for Data which iterates by the no. But i dont know what is the best approach.
Im open for any suggestions.
The complete code: example.cpp
#include <vector>
#include <limits>
#include <iostream>
#include <string>
struct TypeA {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
struct TypeB {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
struct TypeC {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
// In reality TypeA, TypeB and TypeC are not exactly the same
struct Data {
std::vector<TypeA> a;
std::vector<TypeB> b;
std::vector<TypeC> c;
// In reality there are not only 3 values
// but > 20 to print consecutively by no
void print_data_in_order_of_no();
void print_data2_in_order_of_no();
};
void Data::print_data_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data << '\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
void Data::print_data2_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data2 <<'\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data2 << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data2 << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data2_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
int main()
{
Data d;
d.a.push_back(TypeA{1, "1" , "11"});
d.a.push_back(TypeA{ 4, "4", "14" });
d.b.push_back(TypeB{ 3, "3", "13" });
d.b.push_back(TypeB{ 6, "6", "16" });
d.c.push_back(TypeC{ 2, "2", "12" });
d.c.push_back(TypeC{ 5, "5", "15" });
d.print_data_in_order_of_no();
d.print_data2_in_order_of_no();
std::cin.get();
}
I am currently working on a bigger project where I found a part of it which seems very ugly to me from the code.
TypeA
, TypeB
and TypeC
are all different objects with different sizes in the real programm. The comman part is they all have a no
:
All Containers are ordered by ascending no
:
All Numbers are unique, e.g. there's no no==1
in b
or c
. The index numbers are consecutive and fill the data structures.
I want to iterate over the whole struct Data
by the no
of the different data members and do something with the additional shared things the containers have.
To emphasize the ugliness, I sometimes print data1
and the other time data2
but the iteration is exactly the same.
My idea would be writing an custom iterator for Data
which iterates by the no
. But I don't know what is the best approach.
I'm open to any suggestions.
The complete code: ###example.cpp
#include <vector>
#include <limits>
#include <iostream>
#include <string>
struct TypeA {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
struct TypeB {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
struct TypeC {
std::size_t no;
std::string data;
std::string data2;
// more data member etc...
};
// In reality TypeA, TypeB and TypeC are not exactly the same
struct Data {
std::vector<TypeA> a;
std::vector<TypeB> b;
std::vector<TypeC> c;
// In reality there are not only 3 values
// but > 20 to print consecutively by no
void print_data_in_order_of_no();
void print_data2_in_order_of_no();
};
void Data::print_data_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data << '\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
void Data::print_data2_in_order_of_no()
{
enum class It_out {
none,
a,
b,
c,
};
auto it_a = a.begin();
auto it_b = b.begin();
auto it_c = c.begin();
std::size_t current_no = 0;
auto it_out = It_out::none;
do {
auto possible_new_no = std::numeric_limits<size_t>::max();
it_out = It_out::none;
if (it_a != a.end() &&
it_a->no > current_no &&
it_a->no < possible_new_no) {
possible_new_no = it_a->no;
it_out = It_out::a;
}
if (it_b != b.end() &&
it_b->no > current_no &&
it_b->no < possible_new_no) {
possible_new_no = it_b->no;
it_out = It_out::b;
}
if (it_c != c.end() &&
it_c->no > current_no &&
it_c->no < possible_new_no) {
possible_new_no = it_c->no;
it_out = It_out::c;
}
current_no = possible_new_no;
switch (it_out) {
case It_out::a:
std::cout << it_a->data2 <<'\n';
++it_a;
break;
case It_out::b:
std::cout << it_b->data2 << '\n';
++it_b;
break;
case It_out::c:
std::cout << it_c->data2 << '\n';
++it_c;
break;
case It_out::none:
break;
default:
throw std::runtime_error(
"void print_data2_in_order_of_no()"
"impossible enum type"
);
}
} while (it_out != It_out::none);
}
int main()
{
Data d;
d.a.push_back(TypeA{1, "1" , "11"});
d.a.push_back(TypeA{ 4, "4", "14" });
d.b.push_back(TypeB{ 3, "3", "13" });
d.b.push_back(TypeB{ 6, "6", "16" });
d.c.push_back(TypeC{ 2, "2", "12" });
d.c.push_back(TypeC{ 5, "5", "15" });
d.print_data_in_order_of_no();
d.print_data2_in_order_of_no();
std::cin.get();
}
The code for review is the Data
struct and its members.