我有一段代码:
auto time_point_a = std::chrono::high_resolution_clock::now();
while (true) {
auto time_point_b = std::chrono::high_resolution_clock::now();
auto counter_ms = std::chrono::duration_cast<std::chromo::milliseconds(time_point_b - time_point_a);
// more code
std::cont << counter_m
我目前正在为C++开发一个安全的整数库。我在实现减法时遇到了一些问题。
以下是我的开场白:
#include <limits>
#include <stdexcept>
template<typename I>
class safe_int
{
I val;
public:
typedef I value_type;
static constexpr I max = std::numeric_limits<I>::max();
static constexpr I min = std::numeric_limi
bool multiply_overflow_double(double a,double b)
{
double v = a * b;
if (std::isfinite(v))
return (v > std::numeric_limits<uint64_t>::max());
return true;
}
我得到了两个双倍,我需要检查它是否会溢出一个uint64_t。我确实考虑过用传统的除法,但这看起来更简单。
考虑到a和b将大于0,与传统的方法相比,有什么明显的问题?
所有人!
拜托你能解释一件奇怪的事吗。numeric_std库包含一个函数"+",加法。我插入了函数的描述。
- Id: A.3函数"+“(L,R:无符号)返回无符号;-结果子类型:无符号(最大(L‘’LENGTH,R‘’LENGTH)-1降至0)。-结果:添加两个可能长度不同的无符号向量。
例如,我们有:
a : in unsigned(2 downto 0);
b : in unsigned(1 downto 0);
result : out unsigned(2 downto 0);
因此,如果a为"111“
我想检查在什么情况下overflow_error和underflow_error异常将在c++中生成,所以我编写了一个示例来尝试生成overflow_error异常,但失败了:
#include <iostream>
#include <stdexcept>
using std::cout; using std::endl;
int main() {
try {
int a = std::pow(2, 31) - 1;
cout << a << endl;
int b = 1;
如果我有课
class Point
{
public:
Point() {}
Point(int _col, int _row) : row(_row), col(_col) {}
int row, col;
};
如何使用std::find()来检查点是否已经在向量中?我必须让operator==超载吗?
我正试着做这个
#include <algorithm>
if(std::find(v.begin(), v.end(), x) != v.end()) {
/* v contains x */
} else {
/* v does not con
我试着找出每周的秒数,如果我错了,我试着用下面的方式纠正我:
//tp is time precision and dp is day precision.
auto tp = floor<seconds>(system_clock::now());
auto dp = floor<days>(tp);
dp -= weekday{dp} - Monday;
int WeekSeconds=tp - dp;
以下是我遇到的错误:
jdoodle.cpp: In function ‘int main()’:
jdoodle.cpp:6:2
我想为不同类型的整数编写一组函数,其签名是
INT safe_product(INT a, INT b, bool& error);
它接受两个整数a和b,如果a*b不溢出,则返回a*b,并返回0,如果a*b溢出,则将错误设置为true。我也希望这个功能高效,我希望它能在32位和64位平台上运行。
我正在考虑使用std::int32_t、std::uint32_t、std::int64_t、std::uint64_t等来重载std::int64_t。我相信std::int64_t并不总是用32位编译器来定义的。是否有一种方法可以在编译时知道它是否已被定义?
此外,如果我们在64位平台上
首先,我知道ostrstream已经过时了,但在这种情况下,重构到ostrstream并不是一个真正的选择。
目前,我只是简单地检查流是否在<<操作后设置了坏位:
示例(是的,非常简单):
char* buf = new char[10];
std::ostrstream tempOut(buf, 10);
tempOut << "To many charactors to fit in the buffer";
if (tempOut.bad()) {
// Do what needs to be done to allocate a big
使用decltype和std::is_const,可以从外部检测变量的一致性。,但是对象也有可能知道自己的一致性吗?的用法应该是这样的:
#include <type_traits>
#include <iostream>
#include <ios>
struct Test
{
Test() {}
bool print() const
{
// does not work as is explained in https://stackoverflow.com/q/9890218/819272
ret
下面的C++代码片段计算斐波那契数。对于32位整数,当n=47和b变为负值时,我们就会溢出.
int a=0, b=1, n=2;
do {
a += b; int c = a; a = b; b = c;
++n;
} while ( b>0 and n<50 );
std::cout << "Ended at n=" << n << " b=" << b << std::endl;
在g++ 4.9.1上编译很好;除非我使用-O2或-O3,否则循环一直运行到n=50。编译器是
#include <iostream>
using namespace std;
int getMax(numbers[], int size){
int max = numbers[0];
for(i=1; i<size; i++){
if(numbers[i] > max)
max = numbers[i];
}
return max;
}
int main(){
int numbers[6] = {31,23,45,6,7,-2};
cout << ge
你好,堆栈溢出用户!我在制作程序时遇到了问题,我问了一个问题,
这里有两个控制台程序。(我转到2017 C++ )
第一个节目名是"A",第二个节目名称是"B“。两个程序A和B都是控制台程序。。
程序"A“检查程序"B”是否正常运行。如果程序"B“终止,则强制执行程序"B”。
我在2号有个问题..。程序"B“必须作为独立于程序"A”的外部程序运行。但是现在,程序"B“运行在程序"A”控制台提示符中。
请告诉我如何作为一个单独的程序运行。谢谢。
我的代码是:
int main() //Program A