Convert vector<int> to integer

家住魔仙堡 提交于 2019-12-04 09:55:37

Using C++ 11:

reverse(v.begin(), v.end());
int decimal = 1;
int total = 0;
for (auto& it : v)
{
    total += it * decimal;
    decimal *= 10;
}

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

int VectorToInt(vector<int> v)
{
    reverse(v.begin(), v.end());
    int decimal = 1;
    int total = 0;
    for (auto& it : v)
    {
        total += it * decimal;
        decimal *= 10;
    }
    return total;
}

If elements of vector are digits:

int result = 0;
for (auto d : v)  
{
    result = result * 10 + d;
}

If not digits:

stringstream str;
copy(v.begin(), v.end(), ostream_iterator<int>(str, ""));
int res = stoi(str.str());

One liner with C++11 using std::accumulate():

auto rz = std::accumulate( v.begin(), v.end(), 0, []( int l, int r ) {
    return l * 10 + r; 
} );

live example

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:

// VecToIntToVec.cpp

#include <iostream>
#include <vector>

// function prototypes
int vecToInt(const std::vector<int> &vec);
std::vector<int> intToVec(int num);

int main(void)
{
  std::vector<int> vec = { 3, 4, 2, 5, 8, 6 };

  int num = vecToInt(vec);

  std::cout << "num = " << num << "\n\n";

  vec = intToVec(num);

  for (auto &element : vec)
  {
    std::cout << element << ", ";
  }

  return(0);
}

int vecToInt(std::vector<int> vec)
{
  std::reverse(vec.begin(), vec.end());

  int result = 0;

  for (int i = 0; i < vec.size(); i++)
  {
    result += (pow(10, i) * vec[i]);
  }

  return(result);
}

std::vector<int> intToVec(int num)
{
  std::vector<int> vec;

  if (num <= 0) return vec;

  while (num > 0)
  {
    vec.push_back(num % 10);
    num = num / 10;
  }

  std::reverse(vec.begin(), vec.end());

  return(vec);
}

Working solution for negative numbers too!

#include <iostream>
#include <vector>
using namespace std;

template <typename T> int sgn(T val) {
    return (T(0) < val) - (val < T(0));
}

int vectorToInt(vector<int> v) {
  int result = 0;
  if(!v.size()) return result;
  result = result * 10 + v[0];
  for (size_t i = 1; i < v.size(); ++i) {
    result = result * 10 + (v[i] * sgn(v[0]));
  }
  return result;
}

int main(void) {
  vector<int> negative_value = {-1, 9, 9};
  cout << vectorToInt(negative_value) << endl;

  vector<int> zero = {0};
  cout << vectorToInt(zero) << endl;

  vector<int> positive_value = {1, 4, 5, 3};
  cout << vectorToInt(positive_value) << endl;
  return 0;
}

Output:

-199
0
1453

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!