与size_t比较,返回int?

发布于 2024-10-08 12:29:35 字数 727 浏览 5 评论 0原文

我正在编写“如何用 C++ 像计算机科学家一样思考”中的一些代码示例,其中一个是关于处理扑克牌类型的对象和牌组的。我面临这种情况:

int Card::find(const std::vector<Card>& deck) const {
    size_t deckSize = deck.size();
    for (size_t i=0; i<deckSize; i++)
        if (equals(*this, deck[i])) return i;

    return -1;
}

我无法像文本中那样在 Visual Studio 2010 中的 C++ 向量上使用“.length()”,而必须使用 .size() 来返回(我相信)std::尺寸_类型。我想我可以使用 size_t 并摆脱它,以避免不同架构上的问题,正如我一直在阅读的那样,但我想知道我是否返回 i,但它比整数大,我会让程序崩溃吗?

[编辑以更具体地表达我的问题:] 一旦我开始将向量用于比卡片更大的东西,我考虑使用 unsigned int 因为编译器不匹配警告,但我觉得返回 unsigned int 或 int 有一些问题:1)int 不会采用足够大的向量索引。 2)返回unsigned int不会让我返回-1。 3) unsigned int 并不等于所有架构上的 size_t (我也在 ARM Cortex-M3 上进行微控制器编程)。

如果我有足够大的向量我该怎么办?

I'm writing some code examples from "How to Think Like a Computer Scientist in C++", and this one is about handling playing-card type objects and decks. I'm facing this situation:

int Card::find(const std::vector<Card>& deck) const {
    size_t deckSize = deck.size();
    for (size_t i=0; i<deckSize; i++)
        if (equals(*this, deck[i])) return i;

    return -1;
}

I couldn't use ".length()" on a vector in C++ in Visual Studio 2010 as in the text, and instead had to use .size() which returns (I believe) std::size_type. I figured I could use size_t and get away with it in order to avoid problems on different architectures, as I've been reading, but I'm wondering if I return i, but it's bigger than an integer, will I crash the program?

[Edited to be more specific in my question:]
Once I start using vectors for larger things than cards, I considered using unsigned int because of a compiler mismatch warning, but I feel returning an unsigned int or int has a few issues: 1) int will not take a sufficiently large vector index. 2) returning unsigned int will not let me return -1. 3) unsigned int isn't equal to size_t on all architectures (I'm also doing microcontroller programming on an ARM Cortex-M3).

What should I do if I ever have a large enough vector?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨后咖啡店 2024-10-15 12:29:35

size_t 转换为 int 不会让你的程序“崩溃”,但这是一个糟糕的做法。另一方面,STL 包含很好的 find 算法为了你正在做的事情。

Casting from size_t to int will not "crash" your program, but it's a bad bad practice. On the other hand, STL includes nice find algorithm for what you are doing.

老旧海报 2024-10-15 12:29:35

int 在 32 / 64 位 Windows 和 Linux 上是 32 位。如果第 31 日大于 2,我将被截断。你可以使用 unsigned int 并且你的程序会很好,除非在向量中存储超过 4 G 的元素:)

int is 32 bit on 32 / 64 bit Windows and Linux. i will get truncated if greater than two at the 31st. you could use unsigned int and your program will be fine unless storing more than 4 G elements in the vector :)

友欢 2024-10-15 12:29:35

您还可以返回 std::pair,类似于 std::map insert()。第二个模板参数意味着成功或失败。

如果您同意,您还可以使用 boost ::可选

You can also return std::pair<size_t, bool>, similar to std::map insert(). Second template argument means success or fail.

If you ok with this, you could also use boost::optional

空城之時有危險 2024-10-15 12:29:35

size_t 通常是一个 unsigned int 但您不能依赖它。如果它比 int 更大,你不会崩溃,只会溢出到一个(可能是负数)数字。

假设您不会在一个向量中包含数万张卡片,我很乐意返回 int

size_t is typically an unsigned int but you can't rely on that. If it's bigger than an int you won't crash, you'll just overflow into a (probably negative) number.

Assuming you're not going to have several tens of thousands of cards in one vector, I'd be happy returning the int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文