enum class COLOR
{
Blue,
Red,
Green,
Purple,
First=Blue,
Last=Purple
};
COLOR operator++( COLOR& x ) { return x = (COLOR)(((int)(x) + 1)); }
COLOR operator*(COLOR c) {return c;}
COLOR begin(COLOR r) {return COLOR::First;}
// end iterator needs to return one past the end!
COLOR end(COLOR r) {return COLOR(int(COLOR::Last) + 1);}
int main()
{
for (const auto& color : COLOR()) std::cout << int(color); //0123
return 0;
}
I have taken this piece of code from SO link.
I was asked time complexity of similar piece of code. As per my understanding, it is O(n)
as all enumerator element are being iterated.
But right answer on some platforms says O(1)
without any explanation.
Can someone confirm, is it O(1)
and why?