【问题标题】:c++ convert vector of integers to an integerc ++将整数向量转换为整数
【发布时间】:2018-06-26 17:18:14
【问题描述】:

正在网上搜索简单指南,但找不到。

我构建了一个从十进制到二进制的转换器,并将 1 和 0 保存在向量中。我现在想把那个向量变成一个整数。 我找到了各种转换为字符串而不是整数的方法(我不太了解),可以避免吗? 我想让它尽可能简单。

这是我的代码:

c.hpp:

#ifndef C_HPP
#define C_HPP
#include <vector>


class Dualrechnung{
private:
    std::vector<int> vec;
    int z = 123456; //Eingegebene Zahl
    int t_z = z; //temp Zahl

public:
    Dualrechnung();
    ~Dualrechnung();
};

#endif

c.cpp:

#include <vector>
#include <cmath>
#include <sstream>
#include "c.hpp"
#include <iostream>


Dualrechnung::Dualrechnung()
{
    int b;
        for (int i=0; (t_z-(pow(2,i))) >= 0; i++) //finds biggest power of 2 in z
        {
            b= i;
        }
        t_z-=pow(2,b); //whats left after the biggest power 
        vec.push_back(1);
    for(int i = b; i > 0; --i) //checks for every power of 2 multiples smaller than b if they exist in z. if they are, saves 1 for that power in the vector and if not, saves 0.
    {
        b--;
        if((t_z-pow(2,b)) >= 0)
            {
                vec.push_back(1);
                t_z-=pow(2,b);
            }
        else{vec.push_back(0);}
    }

// here I'd like that integer with the information from the vector

    int bin; //binary value of z
        std::cout << z << " in Dual ist " << bin;

}


Dualrechnung::~Dualrechnung(){}

c_test.cpp:

#include "c.cpp"
#include <iostream>


int main(){

    Dualrechnung *d = new Dualrechnung();
    delete d;
    return 0;
}

【问题讨论】:

  • 您不应将 pow() 与整数一起使用。
  • “将 1 和 0 保存在向量中”看起来你只需要 std::bitset
  • 与@drescherjm 的评论无关的扩展:pow 是为真正讨厌的情况而设计的,比如 e 到 pi 的幂,这可以使它用于相对简单的事情,比如 2 到 x 严重昂贵的。使用简单的乘法可能会更好,可能是循环或位移 (1 &lt;&lt; n)。
  • 这样的循环会是什么样子?可以举个简单的例子吗?

标签: c++ vector converter


【解决方案1】:

我为你写了这样的东西:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> myNumInVect = {1, 1, 0, 0, 1};
    int myNumInInteger = 0;
    int baseOfMyNumInVector = 2;
    for (int i : myNumInVect) {
        myNumInInteger = myNumInInteger * baseOfMyNumInVector + i;
    }
    cout << myNumInInteger;
    return 0;
}

(此代码的输出如预期的那样为 25 (11001(2)=25(10))

它使用Horner's method,所以操作数'*' = vector.size()。它很容易适应其他基于位置数字系统的向量(通过更改 baseOfMyNumInVector)。

-----------@edit-----------

我知道你有它,但我也想告诉你在没有任何 pow 的情况下转换 dec2givenBase 是多么容易,或者像这样:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int myNumInInteger = 111;
    int baseOfMyNumInVector = 2;
    vector<int> myNumInVect;

    while (myNumInInteger > 0) { // using Horner Scheme for switching base
        myNumInVect.push_back(myNumInInteger % baseOfMyNumInVector);
        myNumInInteger /= baseOfMyNumInVector;
    }

    std::reverse(myNumInVect.begin(), myNumInVect.end()); // reverse (bcs there is no push_front() method in vector)

    if (myNumInVect.empty()) { // handling the 0 case
        myNumInVect.push_back(0);
    }

    for (auto digit : myNumInVect) { // write the result
        cout << digit;
    }
    return 0;
}

【讨论】:

  • 哇,非常感谢 Mariusz 的帮助。我想知道,这 (int i : vector) 到底是什么意思?是继承吗?还是只用于 for 循环的语法?
  • 表示向量中的每个 int i。在这种情况下,您可以将其替换为:for (int i = 0; i &lt; myNumInVect.size(); ++i) { myNumInInteger = myNumInInteger * baseOfMyNumInVector + myNumInVect[i]; } 我们可以为所有我们可以迭代思想但关心 bcs for(type name : container) {} != for(type &name : container) {} 如果您想了解更多信息,则称为“基于范围”
  • *通过 ... Here 你有一篇关于它的好文章,一切都解释了。
【解决方案2】:

如果我理解正确,您需要将int 转换为一组位,反之亦然。只需使用std::bitset,它具有来自unsigned long 的构造函数和将其转换回来的方法(您也可以将其转换为/从0 和1 的字符串转换)。

【讨论】:

  • std::bitset 没有迭代器。
  • 如果是的话,他们不是biterators吗?
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 2017-08-21
  • 2020-12-08
  • 2016-07-10
  • 1970-01-01
  • 2013-09-27
  • 2011-10-31
  • 1970-01-01
相关资源
最近更新 更多