我是靠谱客的博主 冷艳心锁,这篇文章主要介绍c++for循环的多种用法,现在分享给大家,希望可以做个参考。

转自http://blog.csdn.net/code_star_one/article/details/70162108

第一种用法:最原始的语法(用下标)
for (size_t i = 0; i < vecNum.size(); ++i)  
{  
    strText.Format("%d", nArray[i]);  
    AfxMessageBox(strText);  
}  

第二种用法:最原始的语法(用迭代器)
for (auto it = vecNum.begin(); it != vecNum.end(); ++it)  
{  
    strText.Format("%d", *it);  
    AfxMessageBox(strText);  
}  

第三种用法:简化数组遍历语法(从vs2008开始支持)
for each(auto item in vecNum)  
{  
    strText.Format("%d", item);  
    AfxMessageBox(strText);  
}  

第四种用法:STL函数
std::for_each(vecNum.begin(), vecNum.end(), [](int item)
{  
       CString strText;  
       strText.Format("%d", item);  
       AfxMessageBox(strText);  
};  

第五种用法:C++11新增加的(VS2012支持)
for(auto item : vecNum)  
{  
    strText.Format("%d", item);  
    AfxMessageBox(strText);  
}  

最后

以上就是冷艳心锁最近收集整理的关于c++for循环的多种用法的全部内容,更多相关c++for循环内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(133)

评论列表共有 0 条评论

立即
投稿
返回
顶部