C++ convert vector of digits into integer code example

Example 1: C++ convert vector of digits into integer

// Convert vector of digits to integer
long long vector_to_int(vector<int> num) { 
    long long n = 0;
    int N = num.size();
    for (int i = 0; i < N; i++) {
        n += num[i]*pow(10, N-i-1);
    }
    return n;
}

Example 2: C++ convert integer to digits, as vector

// Convert integer to vector of digits
vector<int> int_to_vector(long long n) { 
    vector<int> vec;
    while (n != 0) {
        vec.push_back(n%10);
        n /= 10;
    }
    reverse(vec.begin(), vec.end());
    return vec;
}

Tags:

Cpp Example