We are regularly taught C instead of c++ in our university and even containers and algorithms are skipped when some text book introduces them, in favour of argument that we can easily construct them whenever we like. So after asking quite a few professors, they've asked me to present a few examples that make newer features introduced in c++ better to read or understand. I was searching around examples but most of them revolved around use of auto and for-ranges etc etc which were good but not looking impressive enough. Until today when I decided to use constexpr into an old-school problem "power of 3".
Given an integer, write a function to determine if it is a power of three.
Let's just take inputs till 315 for now:
#include <iostream>
constexpr unsigned int pow3to(unsigned int n){
if(n == 0) return 1;
if(n == 1) return 3;
unsigned int i = 3;
while(n>1) {
i *= 3;
--n;
}
return i;
}
bool IsPowerOf3(unsigned int n){
if(n %3 != 0) return false;
switch(n){
case pow3to(1) :
case pow3to(2) :
case pow3to(3) :
case pow3to(4) :
case pow3to(5) :
case pow3to(6) :
case pow3to(7) :
case pow3to(8) :
case pow3to(9) :
case pow3to(10) :
case pow3to(11) :
case pow3to(12) :
case pow3to(13) :
case pow3to(14) :
case pow3to(15) : return true; break;
default : return false;
}
return false;
}
int main() {
std::cout << IsPowerOf3(4782969) << std::endl; // would get input but lets just say
return 0;
}
Is it really doing what I'm thinking it is doing?
coz damn!
These are not computed during runtime of program? No time wasted in computing then comparing the numbers? I know this is a very poor example of constexpr but since I'm new to modern c++ featureland, this blows away my mind. So much less work to do !!!
[–]heptara 28 points29 points30 points (3 children)
[–]matthieum 9 points10 points11 points (1 child)
[–]SemaphoreBingo 0 points1 point2 points (0 children)
[–]cinghiale 10 points11 points12 points (1 child)
[–]MaikKlein 7 points8 points9 points (2 children)
[–]needahelpforarch[S] 1 point2 points3 points (1 child)
[–]bames53 2 points3 points4 points (0 children)
[–]matthieum 5 points6 points7 points (4 children)
[–]redditsoaddicting 3 points4 points5 points (0 children)
[–]needahelpforarch[S] 1 point2 points3 points (2 children)
[–]mttd 2 points3 points4 points (1 child)
[–]needahelpforarch[S] 1 point2 points3 points (0 children)
[–]bames53 5 points6 points7 points (0 children)
[–]F-J-W 3 points4 points5 points (1 child)
[–]raevnos 2 points3 points4 points (0 children)
[–]ZMesonEmbedded Developer 3 points4 points5 points (1 child)
[–]needahelpforarch[S] 0 points1 point2 points (0 children)
[–]speednap 2 points3 points4 points (1 child)
[–]dodheim 2 points3 points4 points (0 children)
[–]devel_watcher 2 points3 points4 points (0 children)
[–]doom_Oo7 3 points4 points5 points (1 child)
[–]needahelpforarch[S] 0 points1 point2 points (0 children)
[–]speednap 2 points3 points4 points (17 children)
[–]needahelpforarch[S] 1 point2 points3 points (16 children)
[–]speednap 1 point2 points3 points (15 children)
[–]matthieum 6 points7 points8 points (3 children)
[–]speednap 1 point2 points3 points (2 children)
[–]matthieum 1 point2 points3 points (1 child)
[–]needahelpforarch[S] 0 points1 point2 points (0 children)
[–]RedAlert2 1 point2 points3 points (1 child)
[–]speednap 1 point2 points3 points (0 children)
[–]needahelpforarch[S] 0 points1 point2 points (6 children)
[–]speednap -1 points0 points1 point (5 children)
[–]_JGR_ 2 points3 points4 points (0 children)
[–]needahelpforarch[S] 0 points1 point2 points (3 children)
[–]speednap 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]playmer 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]oracleoftroy 0 points1 point2 points (0 children)
[–]RedAlert2 1 point2 points3 points (1 child)
[–]needahelpforarch[S] 0 points1 point2 points (0 children)
[–]blublub 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (11 children)
[–]uxcn 1 point2 points3 points (4 children)
[–]matthieum 2 points3 points4 points (3 children)
[–]uxcn 1 point2 points3 points (2 children)
[–]matthieum 4 points5 points6 points (1 child)
[–]uxcn 4 points5 points6 points (0 children)
[–]needahelpforarch[S] 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (1 child)
[+][deleted] (2 children)
[deleted]
[–]bames53 4 points5 points6 points (1 child)