最近我发现了很多例子,其中大部分都是关于C++ 98的,反正我已经创建了我的简单数组和循环(codepad):
#include <iostream>
using namespace std;
int main ()
{
string texts[] = {"Apple", "Banana", "Orange"};
for( unsigned int a = 0; a < sizeof(texts); a = a + 1 )
{
cout << "value of a: " << texts[a] << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
value of a: Apple value of a: Banana value of a: Orange Segmentation fault
它工作正常,除了最后的分段错误.
我的问题是,这个数组/循环是否完成了一个好方法?我正在使用C++ 11,所以我想确保它符合标准并且无法以更好的方式完成?