C++ 11 基于范围的for 循环,使用起来特别方便,但是 是否所有容器均适合使用? 如何判断某个对象是否 支持 基于范围 的for 循环?
XXX tempContainer;
for (auto &item : tempContainer) {
cout << item << endl;
}
这里的XXX 可以替换为任意的容器或者类类型,比如 map<int, int> tempContainer;
- map类型
int main()
{
std::map<int, int> tempMap{
{
1,2}, {
5,4}, {
2,3}};
for (auto &item : tempMap) {
cout << item.first << " " << item.second << endl;
}
return 0