下面的方法是跟踪特定数字从不同数字集的分组中出现的次数
void build_prob_distro(const std::vector<Foo>& num_sets, std::map<int, int>& prob_distro){
int key;
Foo cur_foo;
for(unsigned int foo_num = 0; foo_num<num_sets.size(); foo_num++){
cur_foo = num_sets.at(foo_num);
key = 0;
int val;
for(int cur_foo_num=0; cur_foo_num<cur_foo.get_foo_length(); cur_foo_num++){
std::cout << cur_foo.get_num_at(cur_foo_num)*std::pow(10, cur_foo.get_foo_length()-cur_foo_num-1) << std::endl;
val = cur_foo.get_num_at(cur_foo_num)*std::pow(10, cur_foo.get_foo_length()-cur_foo_num-1);
std::cout << val << std::endl;
key = key + cur_foo.get_num_at(cur_foo_num)*std::pow(10, cur_foo.get_foo_length()-cur_foo_num-1);
}
prob_distro[key] += 1;
}
}
我遇到的问题是,当我使用 std::pow() 方法计算我的 map 的键值时,任何超过 100 的值都会减去 -1(即 100 变为 99,103 变为 102,等等。 ).当我用 std::cout 打印出计算结果时,结果是正确的,但是一旦我将值赋给一个 int 变量,它就会得到 -1 错误。我一遍又一遍地查看代码,没有立即发现任何错误。关于可能导致此问题的原因和原因有什么建议吗?
我不认为 foo 类对于这个示例/问题太重要,但我会发布它以防万一它确实是某些问题的原因。
//Foo.h
#ifndef FOO_H
#define FOO_H
#include <string>
#include <vector>
class Foo
{
public:
Foo();
Foo(const std::vector<int>& nums);
int get_num_at(int pos) const;
int get_foo_length() const;
std::string to_string() const;
private:
std::vector<int> nums;
};
#endif // Foo_H
//Foo.cpp
#include "Foo.h"
#include <string>
Foo::Foo(const std::vector<int>& nums){
for(int i=0; i<nums.size(); i++){
this->nums.push_back(nums.at(i));
}
}
Foo::Foo(){}
/* SETTERS & GETTERS */
int Foo::get_num_at(int pos) const{
if(nums.size() != 0){
return nums[pos];
}
return -1;
}
int Foo::get_foo_length() const{
return nums.size();
}
/* END SETTERS & GETTERS */
std::string Foo::to_string() const{}
编辑:我知道有些人会立即指出在 vector 中使用比 Foo 类更简单的东西,但我还有其他功能需要与每个集合结合使用,所以这是我能想出的最好的方法来保持我的相关代码在一起,并允许它表示我感兴趣的任何长度整数值(即 foo 可以表示 1 就像它可以表示 10000 一样容易)。
最佳答案
您可能遇到舍入错误,
因此,您可以尝试 std::lround
作为:
key += cur_foo.get_num_at(cur_foo_num) * std::lround(std::pow(10, cur_foo.get_foo_length() - cur_foo_num - 1));
或编写您自己的pow_int
函数以避免使用float
:
constexpr int pow_int(int x, unsigned int n)
{
// x ** (2n + 1) == ((x * x) ** n) * x
// x ** 2n == (x * x) ** n
// x ** 0 == 1
return (((n >> 1) == 0) ? 1 : pow_int(x * x, n >> 1)) * (((n & 1) == 0) ? 1 : x);
}
或(线性版本)
int pow_int(int x, unsigned int n)
{
int res = 1;
for (unsigned int i = 0; i != n; ++i) {
res *= x;
}
return res;
}
关于c++ - cmath std::pow 函数在分配给变量时给出错误的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20756701/