I was looking for pre-defined function for converting a vector of integers into a normal integer but i din't find one.
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
Need this:
int i=123 //directly converted from vector to int
Is there a possible way to achieve this?
Working solution for negative numbers too!
Output:
Live Demo
The other answers (as of May '19) seem to assume positive integers only (maybe 0 too). I had negative inputs, thus, I extended their code to take into account the sign of the number as well.
Using C++ 11:
EDIT: Now it should be the right way.
EDIT 2: See DAle's answer for a shorter/simpler one.
For the sake of wrapping it into a function to make it re-usable. Thanks @Samer
In conjunction with the answer provided by
deepmax
in this post Converting integer into array of digits and the answers provided by multiple users in this post, here is a complete test program with a function to convert an integer to a vector and a function to convert a vector to an integer:One liner with C++11 using std::accumulate():
live example
If elements of vector are digits:
If not digits: