cuốn sách gpt4 ai đã làm

c++ - Reinterpret_cast trên Prior_queue có lặp lại vectơ cơ bản không chuẩn không?

In lại Tác giả: Walker 123 更新时间:2023-11-28 02:09:20 28 4
mua khóa gpt4 Nike

下面的代码编译和运行得很好,但我一直在阅读 reinterpret_cast 并且我无法确定它是否符合标准并且可移植。在我看来,这应该是因为我们明确指定了 priority_queue 的底层容器,但我无法得到直接的答案,所以 SO-wizards 可能对这篇文章有一些了解。

它所做的基本上是创建一个使用 vector 处理整数的 priority_queue。然后它将队列重新解释为 vector 指针,以便可以迭代队列的元素(因为 priority_queue 本身不包含该功能)。

#include 
#include
#include


int chính() {
std::priority_queue< int, std::vector > pq;

pq.push(100);
pq.push(32);
pq.push(1);

auto o = reinterpret_cast<> *>(&pq);
for (std::vector::iterator it = (*o).begin(); it != (*o).end(); it++) {
std::cout << (*it) << std::endl;
}

trả về 0;
}

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress