Checking if a double (or float) is NaN in C++

Is there an isnan() function? PS.: I'm in MinGW (if that makes a difference). I had this solved by using isnan() from <math.h> , which doesn't exist in <cmath> , which I was #include ing at first. According to the IEEE standard, NaN values have the odd property that comparisons involving them are always false. That is, for a float f, f != f will be true only if f is NaN

检查C ++中的双精度(或浮点数)是否为NaN

是否有一个isnan()函数? PS:我在MinGW(如果这有所作为)。 我通过使用<math.h> isnan()解决了这个问题,该解决方案不存在于<cmath> ,我首先是#include 。 根据IEEE标准,NaN值具有奇怪的特性,即涉及它们的比较总是错误的。 也就是说,对于浮点数f,只有当f是NaN时, f != f才会成立。 请注意,正如下面的一些评论所指出的那样,并不是所有编译器在优化代码时都会尊重这一点。 对于声称使用IEEE

When do Iterators in the following cases become invalidated

This question already has an answer here: Iterator invalidation rules 4 answers 1-If the 50th element is removed in a vector all the next elements will move one step up since a vector is a dynamic array and its contiguous. So iterators before 50th index would be valid and iterators greater than or equal to 50 would be invalidated after the remove Correct. (Source) 2-If the container is

在下列情况下,迭代器何时失效

这个问题在这里已经有了答案: 迭代器失效规则4个答案 1-如果在矢量中删除第50个元素,则所有下一个元素将向上移动一个矢量,因为矢量是一个动态数组并且是连续的。 因此,第50个索引之前的迭代器将是有效的,大于或等于50的迭代器将在删除之后失效 正确。 (资源) 2 - 如果容器是一个列表(双链表),并且第50个索引被删除,那么只有第50个索引之后的迭代器会受到影响。 不正确。 只有指向第50个索引的迭代器失

Why does the same code run differently in Visual Studio and Dev

This question already has an answer here: Iterator invalidation rules 4 answers This is due to undefined behavior when you use the iterator after the call to v.assign() as assign invalidates iterators and so using an iterator after that call is a bad idea. Interestingly VS does reuse the same underlying memory after the call to assign (its still got the same address with capacity 10 but new

为什么相同的代码在Visual Studio和Dev中以不同的方式运行

这个问题在这里已经有了答案: 迭代器失效规则4个答案 这是因为在调用v.assign()之后使用迭代器来分配无效迭代器时发生未定义的行为,因此在调用之后使用迭代器是一个坏主意。 有趣的是,VS在分配调用之后重用了相同的底层内存(它仍然获得了容量为10的相同地址,但是新的大小为3),但是它有一个名为Debug Iterators的功能。 启用此功能时,由于它是默认的Debug版本,所以它存储有效迭代器的列表,因此知道您的迭代器已

Erase all even numbers from vector using find

This question already has an answer here: Iterator invalidation rules 4 answers When using the erase(it); function the iterator changes so you need to set the iterator again to the new one returned by the erase function. In your code, you are checking for the end if(q == myVec.end()) and then using erase this will throw an error as.end() does not point to data, and to be able to erase an i

使用find从矢量中清除所有偶数

这个问题在这里已经有了答案: 迭代器失效规则4个答案 使用erase(it); 函数迭代器发生了变化,所以您需要将迭代器再次设置为由擦除函数返回的新迭代器。 在你的代码中, if(q == myVec.end())检查结束,然后使用擦除将会抛出一个错误,因为end()不指向数据,并且能够从迭代器需要有效的向量。 因此,通过将if(q == myVec.end())更改为if(q == (myVec.end()-1)) ,它将允许您删除最后一个元素,以防成对出现。 std::v

Can iterators become invalid after operations?

Possible Duplicate: Iterator invalidation rules Imagine that I have a map<int, int> . Somehow I retrieved an iterator pointing to an entry pair<35,37> in the map. I save this iterator as map<int, int>::iterator _my_iterator3537 . After that, I did a lot of insertion to the map. Is _my_iterator3537 still pointing to the pair<35,37> ? from documentation: Map has

操作之后,迭代器会失效吗?

可能重复: 迭代器失效规则 想象一下,我有一张map<int, int> 。 不知何故,我检索了一个迭代器,指向映射中的一个入口pair<35,37> 。 我将这个迭代器保存为map<int, int>::iterator _my_iterator3537 。 之后,我在地图上做了很多插入。 _my_iterator3537是否仍然指向pair<35,37> ? 来自文档: Map具有将新元素插入到映射中的重要属性不会使指向现有元素的迭代器无效。 从地图擦除元素

Keeping separate pointers to container items

This question already has an answer here: Iterator invalidation rules 4 answers For a std::vector , you can use the reserve member function to ensure that a contiguous block of memory is preallocated for the maximum number of elements you require. This means that the elements are not copied around unless you exceed the reserved capacity. All pointers to elements in a std::deque are also in

保留单独的指向容器项目的指针

这个问题在这里已经有了答案: 迭代器失效规则4个答案 对于std::vector ,您可以使用reserve成员函数来确保为您需要的最大数量的元素预分配连续的内存块。 这意味着除非您超过保留容量,否则元素不会被复制。 所有指向std::deque中元素的指针在插入或删除元素时也都是无效的。 但是,除了指向要删除的元素的指针之外, std::list保证不会使这些情况下的任何指针失效。 列表是序列容器,允许您插入和擦除任何地方并在两

Is it safe to store a pointer to an item in an std::set?

This question already has an answer here: Iterator invalidation rules 4 answers Yes, both set and unordered_set are safe in this regard. If references are not invalidated, your pointers also remain valid. It's an easy property for the node-based collections to maintain; unlike vector there's no need for them to move values around in memory.

将指针存储在std :: set中安全吗?

这个问题在这里已经有了答案: 迭代器失效规则4个答案 是的, set和unordered_set在这方面是安全的。 如果引用没有失效,你的指针也保持有效。 对于基于节点的集合来说,这是一个简单的属性; 与vector不同,它们不需要在内存中移动值。

Can vector iterators in c++ be corrupted when you add elements?

This question already has an answer here: Iterator invalidation rules 4 answers Yes. Adding elements to a vector can cause reallocation, which will invalidate all iterators and pointers to the vector elements. Yes, this can happen. If the vector resizes old iterators are no longer valid. This is not true for all collections. Take a look at the invalidation rules.

添加元素时,c ++中的矢量迭代器是否会损坏?

这个问题在这里已经有了答案: 迭代器失效规则4个答案 是。 向矢量添加元素可能导致重新分配,这会使所有迭代器和指向矢量元素的指针无效。 是的,这可能发生。 如果矢量调整旧的迭代器不再有效。 这不适用于所有收藏。 看看失效规则。

The address of my object in a vector changes

This question already has an answer here: Iterator invalidation rules 4 answers You are calling vec.push_back(a) within your for loop. Therefore the vector may re-allocate the underlying array if it runs out of space. Therefore the address of the previous elements are no longer valid if they were copied to the new memory location. For example say you allocated 3 elements and stored their

我的对象在矢量中的地址发生了变化

这个问题在这里已经有了答案: 迭代器失效规则4个答案 您正在for循环中调用vec.push_back(a) 。 因此,如果空间不足,向量可能会重新分配基础数组。 因此,如果先前元素的地址被复制到新的内存位置,则其地址不再有效。 比如说你分配了3个元素并存储了他们的地址。 在推回第四个元素之后,矢量必须重新分配。 这意味着前3个元素将被复制到一个新的位置,然后第4个将被添加。 因此,您为前3个地址存储的地址现在无效。

Rules for Iterator Invalidation

This question already has an answer here: Iterator invalidation rules 4 answers These rules are container specific. In fact, these are important criteria for deciding which container you use. For instance, iterators to an std::vector can get invalidated when an object is inserted (depends in where the object is inserted and if reallocation takes place), and they get invalidated when an obj

迭代器失效规则

这个问题在这里已经有了答案: 迭代器失效规则4个答案 这些规则是特定于容器的。 实际上,这些是决定使用哪个容器的重要标准。 例如,当插入一个对象时(取决于对象被插入的位置以及是否发生重新分配),对std::vector迭代器可能会失效,并且在迭代器之前删除对象时它们将失效。 一个std::list没有这个问题。 插入和删除对象(迭代器指向的对象除外)不会使迭代器失效。 SGI提供了很好的文档。 无效规则来自用于实