gpt4 book ai didi

c++ - 对于 GCC 和 GCC 版本中的每个

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:33 24 4
gpt4 key购买 nike

我如何在 GCC 中使用 for each 循环?

我如何获得 GCC 版本? (在代码中)

最佳答案

使用 lambda,例如

// C++0x only.
std::for_each(theContainer.begin(), theContainer.end(), [](someType x) {
// do stuff with x.
});

range-based for loop自 4.6 起受 GCC 支持。

// C++0x only
for (auto x : theContainer) {
// do stuff with x.
}

"for each" loop syntax 是 MSVC 扩展。它在其他编译器中不可用。

// MSVC only
for each (auto x in theContainer) {
// do stuff with x.
}

但你可以只使用 Boost.Foreach .它是可移植的,也可以在没有 C++0x 的情况下使用。

// Requires Boost
BOOST_FOREACH(someType x, theContainer) {
// do stuff with x.
}

参见 How do I test the current version of GCC ?关于如何获取 GCC 版本。

关于c++ - 对于 GCC 和 GCC 版本中的每个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3537335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com