std::vector<int> v;
v.push_back(0);
std::vector<int>::iterator vi = v.begin();
while(vi != v.end())
{
if(*vi == 0)
{
v.erase(vi++);
continue;
}
++vi;
}
以上這段code在VC2003上run是正常的, 用gcc 3.2.2的話會造成segmentation fault。
正確的寫法應該是 vi = v.erase(vi);
大山可以挪開,小山可以遷移, 但主的慈愛永不雜開你。
Though the mountains be shaken and the hills be removed, yet my unfailing love for you will not be shaken nor my covenant of peace be removed,
std::vector<int> v;
v.push_back(0);
std::vector<int>::iterator vi = v.begin();
while(vi != v.end())
{
if(*vi == 0)
{
v.erase(vi++);
continue;
}
++vi;
}
0 comments:
Post a Comment