Back
Close

Range-based for loop

Anonymous
1,057 views

Range-based for loop

#include<iostream>
using namespace std;
template<typename T, int sizeOfArray>
class MyCustomType
{
private:
T *data;
int indx;
public:
MyCustomType(){
data = new T[sizeOfArray];
indx = -1;
}
~MyCustomType(){
delete []data;
}
void addData(T newVal){
data[++indx] = newVal;
}
//write definition for begin() and end()
//these two method will be used for "ranged based loop idiom"
T* begin(){
return &data[0];
}
T* end(){
return &data[sizeOfArray];
}
};
int main()
{
MyCustomType<double, 2> numberList;
numberList.addData(20.25);
numberList.addData(50.12);
for(auto val: numberList){
cout<<val<<endl;
}
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants