ARTICLE AD BOX
One alternative to using std::string is to use the "shift + append" rule to concatenate the numbers:
#include <iostream> #include <cmath> #include <vector> unsigned long long combine(const std::vector<unsigned int>& arr) { unsigned long long result = 0; for (unsigned int num : arr) { unsigned int digits; //used to store the number of digits in the num //calculate the number of digits in each num if(num == 0) { digits = 1; } else { digits = std::log10(num) + 1; } //std::cout <<"digits in: " << num << " " << digits << std::endl; //print for debugging result = result * std::pow(10, digits) + num; //shift and append } return result; } int main() { std::cout << combine({10, 23, 4, 54, 6}) << '\n'; std::cout << combine({101, 203, 2, 0, 68}) << '\n'; }50.6k7 gold badges47 silver badges103 bronze badges
