【发布时间】:2012-02-02 23:22:37
【问题描述】:
我有一个充满字符串的向量
向量consistentWords包含4个字符串
- ddf
- edf
- 美联储
- hedf
现在我想删除所有单词不以字母d开头的字符串
但它最终只是删除了 eedf 和 hedf,我留下的结果是
- ddf
- 美联储
我的代码:
for(int q=0; q<consistentWords.size(); q++)
{
string theCurrentWord = consistentWords[q];
if(theCurrentWord[0] != 'd')
{
consistentWords.erase(consistentWords.begin()+q);
}
}
有什么想法吗?我只是不明白为什么它没有删除所有不以 d 开头的字符串。
【问题讨论】:
-
您可能希望将
theCurrentWord设为引用以避免复制。