将 int 数组转换为带有小数C++的双精度数组
Cast an Int Array into a Double Array C++ with Decimals
例如,我有
int 结果[11][2],我需要将这些值转换为具有指定十进制值的双精度 d[11][2],例如左侧 4 位。我似乎已经通过在数组外部抛出一个(双精度(来投射双精度,在我的一个算法之前。我似乎无法弄清楚如何移动十进制值占位符,并且已经搜索了这个网站和C++图书馆试图找到答案。我在向量上看到了一些东西,但我不确定这些是什么。我的一些最终答案是整数,例如 100,有些是小数,例如 13.33。
我冒昧地将注释中的发现放在一些代码中:
#include <iostream>
#include <iomanip>
#include <algorithm>
// only for dirty random numbers:
#include <ctime>
#include <cstdlib>
int main()
{
std::srand(static_cast<unsigned>(std::time(nullptr)));
int foo[11][2];
// some data:
for (auto &f : foo)
for (auto &i : f)
i = rand();
// copy to an array of arrays of doubles:
double bar[11][2];
std::copy(&foo[0][0], &foo[0][0] + 11 * 2, &bar[0][0]);
// some calculation:
for (auto &b : bar)
for (auto &d : b)
d *= 100. / rand();
// output:
for (auto &b : bar) {
for (auto &d : b)
std::cout << std::fixed << std::setw(8) << std::setprecision(4) << d << 't';
std::cout.put('n');
}
}
示例输出:
255.3750 155.6058
45.7636 164.7974
50.4983 99.8548
108.2106 90.2723
42.8402 86.1336
241.4214 802.7950
175.9784 86.9273
11.9571 741.2245
95.0048 225.1073
84.7645 389.8197
39.6910 3.0909
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 声明后,gcc 的动态大小数组是否与标准数组有效相同?
- 将 int 数组转换为带有小数C++的双精度数组
- 如何将大括号初始值设定项用于动态大小数组和新数组?
- 对未知大小数组的引用的列表初始化:它是否应该推断数组大小?
- 返回指向固定大小数组C++的数组的指针
- 如何将小数数组转换为二进制数数组
- 对指向固定大小数组的指针数组的内存分配的解释
- 在 C++ 中创建固定大小的 int 数组的动态大小数组