相关疑难解决方法(0)

为什么无符号整数容易出错?

我正在看这个视频.Bjarne Stroustrup无符号整数容易出错并导致错误.所以,你应该只在你真正需要的时候使用它们.我还读过有关Stack Overflow的问题之一(但我不记得哪一个)使用无符号整数会导致安全漏洞.

它们如何导致安全漏洞?有人可以通过给出一个合适的例子来清楚地解释它

c++ unsigned-integer

60
推荐指数
5
解决办法
6503
查看次数

Why is std::ssize being forced to a minimum size for its signed size type?

In C++20, std::ssize is being introduced to obtain the signed size of a container for generic code. (And the reason for its addition is explained here.)

Somewhat peculiarly, the definition given there (combining with common_type and ptr_diff_t) has the effect of forcing the return value to be "either ptrdiff_t or the signed form of the container's size() return value, whichever is larger".

P1227R1 indirectly offers a justification for this ("it would be a disaster for std::ssize() to …

c++ language-lawyer c++20

6
推荐指数
1
解决办法
191
查看次数

C++ 中“!= data + arraySize”是什么意思?

我正在寻找一种在数组中找到给定 int 的方法,我找到了这个解决方案

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int data[] = {23, 45, 56, 12, 34, 56};
  int target = 56;

  int arraySize = sizeof(data) / sizeof(*data);

  bool isPresent = std::find(data, data + arraySize, target) != data + arraySize;

  if (isPresent) {
    cout << "The element is present";
  } else {
    cout << "The element is not present";
  }
  return 0;
Run Code Online (Sandbox Code Playgroud)

现在我测试了并且它有效,但我想知道为什么在 find() 之后有这个 != data + arraySize ?希望得到解释

c++ arrays algorithm iterator pointer-arithmetic

-2
推荐指数
1
解决办法
153
查看次数