【问题标题】:Convert vector<int> to integer将向量<int> 转换为整数
【发布时间】:2017-08-21 22:32:16
【问题描述】:

我一直在寻找用于将整数向量转换为普通整数的预定义函数,但我没有找到。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

需要这个:

int i=123 //directly converted from vector to int

有没有可能的方法来实现这一点?

【问题讨论】:

  • 我想要的是如果我的向量 v 包含的元素为 123456,那么如果我取整数 i,它的值必须直接为 123456(通过库中的某些函数/任何其他方式)。我不想使用 pow() 来提取向量的每个元素并将其分配给整数 i

标签: c++ stl stdvector


【解决方案1】:

使用 C++ 11:

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

编辑:现在应该是正确的方式。

编辑 2:请参阅 DAle 的答案以获得更短/更简单的答案。

为了将其包装成一个函数以使其可重用。谢谢@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;
}

【讨论】:

  • 为了完成为什么不把它包装成一个函数。
  • 不需要倒车。查看其他答案。
【解决方案2】:

如果向量的元素是数字:

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

如果不是数字:

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

【讨论】:

  • 类似的解决方案是for_each(v.begin(), v.end(), [&amp;](auto&amp; d){result = result * 10 + d;});
【解决方案3】:

使用 std::accumulate() 的 C++11 内衬:

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

live example

【讨论】:

    【解决方案4】:

    结合deepmax在这篇文章Converting integer into array of digits提供的答案以及在这篇文章中多个用户提供的答案,这里有一个完整的测试程序,其中包含一个将整数转换为向量的函数和一个函数将向量转换为整数:

    // 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);
    }
    

    【讨论】:

      【解决方案5】:

      负数的有效解决方案!

      #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;
      }
      

      输出:

      -199
      0
      1453
      

      Live Demo

      其他答案(截至 19 年 5 月)似乎只假设 整数(也可能为 0)。我有负面输入,因此,我扩展了他们的代码以考虑sign of the number

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-27
        • 2012-05-10
        • 2014-09-19
        • 1970-01-01
        • 2018-06-26
        • 2011-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多