尝试使用chrono将时间存储到数组中
trying to store time into array using chrono
我正在测量我编写的算法的时间,并使用std::chrono以微秒为单位进行测量。然而,我也在尝试将这些经过的值存储到一个数组中,我不确定如何存储。我试过这个(我的数组是int类型)
std::chrono::duration_cast<std::chrono::microseconds>(end - start);
time_insertion_sort[i][j] = elapsed;
我得到以下错误:
error: cannot convert 'std::chrono::duration<long int, std::ratio<1l,
1000000l> >' to 'int' in assignment
time_insertion_sort[i][j] = elapsed;
我想,如果我将数组声明为long类型,也许它会起作用,但它仍然不起作用。有人能帮我吗?
当您声明您的数组的类型为int
时,错误是它无法将std::duration
类型转换为int
。因此,您需要获取原始值并将其存储或将std::duration
类型存储在数组中。
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// You should be able to store this raw value.
auto rawValue = elapsed.count();
注意,count
函数返回的类型是std::duration
的表示类型。您的错误消息指示表示类型为long int
,因此,如果sizeof(int)
与sizeof(long)
不同,则会发生溢出。
相关文章:
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 在c++中获取两个大int,并将它们存储在数组中
- 使用 Xcode 将双精度存储在数组C++中
- 访问表示 2D 矩阵的 1D 存储数组
- 从输入文件读取整数,仅存储数组C 中的唯一整数
- 如何在队列中存储数组
- 按列主要顺序迭代行大量存储数组
- 如何使存储数组的二进制搜索稳定
- 将行中的字段从textfile中更改为存储数组,而不是重新编写文件
- 存储数组索引值C++
- 存储数组的地方,如果我在返回后声明它
- 缺少存储数组地址的内存
- 使用static在函数中存储数组各个组件的值
- 如何存储数组的每个相邻子数组的和
- 在c++中存储数组映射
- 存储数组的地址列表,以解析原始的非终止文本
- 用于存储数组的字符串
- c++存储数组下一个位置的地址
- 在 for 循环中创建和存储数组