I have an array class MyArray
with MyArray
.
For simplicity, this is the whole code. It works OK.
#include <iostream>
using namespace std;
template<class T> class MyArray;
template<class T>class MyIterator{
public: int index=0;
public: MyArray<T>* myArray;
public: MyIterator<T> operator++(){
index++;
return *this;
}
public: T& operator*(){
return myArray->database[index];
}
public: friend bool operator!=(MyIterator<T> b,MyIterator<T> c){
return b.index!=c.index;
}
};
template<class T>class MyArray{
public: T database[5];
public: MyArray(){
database[2]=3; //just fill with something to test
}
public: MyIterator<T> begin(){
MyIterator<T> r; r.index=0; r.myArray=this;
return r;
}
public: MyIterator<T> end(){
MyIterator<T> r; r.index=5; r.myArray=this;
return r;
}
};
Here is its usage :-
int main() {
MyArray<int> test;
for(int ele:test){
std::cout<<ele<<std::endl;
}
return 0;
}
I have specific classes, let's say their names are B
and C
.
I have a converter from B
to C
named convertBToC(B)
.
Now, I want a new datastructure (named MyArray2
) that :-
MyArray<B>
....operator*()
of MyArray2
's iterator return C
instead of B
C
that is returned, convert from B
using convertBToC(B)
.This is the usage that I wished for (#1
) :-
MyArray2 test;
//test.push_back(B()); //used like "MyArray<B>"
for(C c:test){ //"C", not "B"
.... logic about "c" ....
}
The above code would work as if I call it like :-
MyArray<B> arr;
for(B& b: arr){
C c= convertBToC(b); //<-- I want to hide this line
.... logic about "c" ....
}
Question: How to code MyArray2
?
I want a solution that :- (sorted by priority)
std::function
and its family)MyIterator
directly (because MyIterator
is an internal class)MyArray<T>
(if any)The most related question is here, but it mentions about std::vector
.
Create 2 classes :-
MyArray2
derived from MyArray<B>
Override: begin()
or end()
- return MyIterator2
.
MyIterator2
derived from MyIterator<B>
Override: operator*()
- return C
(using convertBToC()
).
Disadvantage:
MyArray2.h
code contains the word MyIterator<T>
.Create only 1 class :-
MyArray2
derived from MyArray<B>
iterate()
:-Here is the draft of iterate()
:-
template<typename F> MyArray2::iterate( F lamdbaFunction ){
for(B b: MyArray<B>){
C c= convertBToC(b);
lamdbaFunction(c);
}
}
The usage have to be changed from #1
to be ... (#2
)
MyArray2 arr;
auto lambdaF=[&](C c){
.... logic about "c" ....
}
arr.iterateElement(lambdaF);
Disadvantage
Another approach would be to use Boost range adaptors, specifically boost::adaptors::transformed
.
#include <boost/range/adaptor/transformed.hpp>
...
MyArray<B> test;
for(C ele : test | boost::adaptor::transformed(convertBToC)) {
// something...
}
Judge how much of an improvement it is yourself. Maybe better put the operator|
call inside a new MyArray
's member function.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments