C:迭代STL容器的正确方法
发布时间:2020-12-16 10:52:40 所属栏目:百科 来源:网络整理
导读:在我的游戏引擎项目中,我广泛使用了STL,主要是std :: string和std :: vector类. 在许多情况下,我必须遍历它们.现在,我这样做的方式是: for( unsigned int i = 0; i theContainer.size(); i ++ ){} 我是以正确的方式做到的吗? 如果没有,为什么,我应该做什么
在我的游戏引擎项目中,我广泛使用了STL,主要是std :: string和std :: vector类.
在许多情况下,我必须遍历它们.现在,我这样做的方式是: for( unsigned int i = 0; i < theContainer.size(); i ++ ) { } >我是以正确的方式做到的吗? 解决方法
C 11有一个新的容器知道循环语法,如果你的编译器支持新标准,可以使用它.
#include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<string> vs; vs.push_back("One"); vs.push_back("Two"); vs.push_back("Three"); for (const auto &s : vs) { cout << s << endl; } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |