...
constexpr long long size = 100000000;
std::cout << std::endl;
std::vector<int> randValues;
randValues.reserve(size);
// random values
std::random_device seed;std::mt19937 engine(seed());
std::uniform_int_distribution<> uniformDist(1, 10);
const unsigned long long sum = std::accumulate(randValues.begin(), randValues.end(), 0);
...
...
void sumUp(unsigned long long& sum, const std::vector<int>& val,
unsigned long long beg, unsigned long long end){
for (auto it = beg; it < end; ++it){
std::lock_guard<std::mutex> myLock(myMutex);
sum += val[it];
}
}
...
...
void sumUp(std::atomic<unsigned long long>& sum, const std::vector<int>& val,
unsigned long long beg, unsigned long long end){
for (auto it = beg; it < end; ++it){
sum.fetch_add(val[it]);
}
}
...
...
void sumUp(unsigned long long& sum, const std::vector<int>& val,
unsigned long long beg, unsigned long long end){
unsigned long long tmpSum{};
for (auto i = beg; i < end; ++i){
tmpSum += val[i];
}
std::lock_guard<std::mutex> lockGuard(myMutex);
sum += tmpSum;
}
...
// userdefinedTypes.cpp
#include <iostream>
#include <ostream>
class MyInt {
public:
constexpr MyInt() = default;
constexpr MyInt(int fir, int sec) :myVal1(fir), myVal2(sec) {}
MyInt(int i) {
myVal1 = i - 2;
myVal2 = i + 3;
}
constexpr int getSum() const { return myVal1 + myVal2; }
friend std::ostream& operator<<(std::ostream& out, const MyInt& myInt) {
out << "(" << myInt.myVal1 << "," << myInt.myVal2 << ")";
return out;
}
private:
int myVal1 = 1998;
int myVal2 = 2003;
};
int main() {
std::cout << std::endl;
constexpr MyInt myIntConst1;
constexpr int sec = 2014;
constexpr MyInt myIntConst2(2011, sec);
std::cout << "myIntConst2.getSum(): " << myIntConst2.getSum() << std::endl;
int arr[myIntConst2.getSum()];
static_assert(myIntConst2.getSum() == 4025, "2011 + 2014 should be 4025");
std::cout << std::endl;
}
int powFunc(int m, int n){
if (n == 0) return 1;
return m * powFunc(m, n-1);
}
template<int m, int n>
struct PowMeta{
static int const value = m * PowMeta<m, n-1>::value;
};
template<int m>
struct PowMeta<m, 0>{
static int const value = 1;
};
constexpr int powConst(int m, int n){
int r = 1;
for(int k = 1; k <= n; ++k) r *= m;
return r;
}