C++ 删除向量中的字符串
我有一个充满字符串的向量 向量consistentWords包含4个字符串C++ 删除向量中的字符串,c++,string,vector,C++,String,Vector,我有一个充满字符串的向量 向量consistentWords包含4个字符串 德夫 eedf fedf hedf 现在我想删除所有单词不以字母d开头的字符串 然而,它最终只是删除了eedf和hedf,我留下的结果是 德夫 fedf 我的代码: for(int q=0; q<consistentWords.size(); q++) { string theCurrentWord = consistentWords[q]; if(theCurren
for(int q=0; q<consistentWords.size(); q++)
{
string theCurrentWord = consistentWords[q];
if(theCurrentWord[0] != 'd')
{
consistentWords.erase(consistentWords.begin()+q);
}
}
for(int q=0;q您正在跳过元素。假设您需要删除元素5,6:
当您删除元素5时,元素6将成为元素5,您将跳过它,因为q
已增加到6
更好的方法是手动增加q
,仅当您不删除某个元素时您正在跳过元素。假设您需要删除元素5,6:
当您删除元素5时,元素6将成为元素5,您将跳过它,因为q
已增加到6
更好的方法是手动增加q
,只有当你不删除一个元素时才这样做。当你擦除时,你不应该执行q++。然后你会错过一个元素。当你擦除时,你不应该执行q++。然后你会错过一个元素。要删除这个词:
consistentWords.erase(
std::remove(consistentWords.begin(), consistentWords.end(), theCurrentWord),
consistentWords.end()
);
要删除该词,请执行以下操作:
consistentWords.erase(
std::remove(consistentWords.begin(), consistentWords.end(), theCurrentWord),
consistentWords.end()
);
首先,字符串对应于以下索引:
dedf 0
eedf 1
fedf 2
hedf 3
假设您删除了eedf
(因此q==1
)
dedf 0
fedf 1
hedf 2
但是q
会增加到2,完全跳过fedf
。解决方法是稍微改变for
循环:
for(int q=0; q<consistentWords.size();)
{
string theCurrentWord = consistentWords[q];
if(theCurrentWord[0] != 'd')
{
consistentWords.erase(consistentWords.begin()+q);
}
else
{
q++;
}
}
对于(int q=0;q首先,字符串对应于以下索引:
dedf 0
eedf 1
fedf 2
hedf 3
假设您删除了eedf
(因此q==1
)
dedf 0
fedf 1
hedf 2
但是q
会增加到2,完全跳过fedf
。解决方法是稍微改变for
循环:
for(int q=0; q<consistentWords.size();)
{
string theCurrentWord = consistentWords[q];
if(theCurrentWord[0] != 'd')
{
consistentWords.erase(consistentWords.begin()+q);
}
else
{
q++;
}
}
for(int q=0;q问题是在同一次迭代中删除向量中的元素并增加索引q
。因此在for循环的第二次迭代中,从向量中删除“eedf”
,然后向量是[“dedf”、“fedf”、“hedf”]
和q=1
。但是当您循环回到for循环的开始时,q
会增加到2,因此您可以查看“hedf”
下一步,跳过“fedf”
。要解决这个问题,您可以在从数组中删除一个元素时减小q
,如下所示:
for(int q=0; q<consistentWords.size(); q++)
{
string theCurrentWord = consistentWords[q];
if(theCurrentWord[0] != 'd')
{
consistentWords.erase(consistentWords.begin()+q);
--q;
}
}
for(int q=0;q问题是在同一次迭代中删除向量中的元素并增加索引q
。因此在for循环的第二次迭代中,从向量中删除“eedf”
,然后向量是[“dedf”、“fedf”、“hedf”]
和q=1
。但是当您循环回到for循环的开始时,q
会增加到2,因此您可以查看“hedf”
下一步,跳过“fedf”
。要解决这个问题,您可以在从数组中删除一个元素时减小q
,如下所示:
for(int q=0; q<consistentWords.size(); q++)
{
string theCurrentWord = consistentWords[q];
if(theCurrentWord[0] != 'd')
{
consistentWords.erase(consistentWords.begin()+q);
--q;
}
}
for(int q=0;q问题已经得到了回答,但您应该查看以下内容:
例如:
consistentWords.erase(
std::remove_if(consistentWords.begin(), consistentWords.end(),
[](const std::string& s) -> bool { return (s[0] == 'd'); }),
consistentWords.end());
问题已经得到了回答,但您应该研究以下问题:
例如:
consistentWords.erase(
std::remove_if(consistentWords.begin(), consistentWords.end(),
[](const std::string& s) -> bool { return (s[0] == 'd'); }),
consistentWords.end());
即使使用迭代器,我们也必须小心。我记得,当您从向量中删除时,访问元素的迭代器将无效。在对向量或映射进行迭代时,请小心删除其中的元素。正确的算法并不明显,通常有一些微妙之处。感谢您的评论。我删除了“迭代器”答案的一部分。即使使用迭代器,我们也必须小心。我记得,当您从向量中删除时,访问元素的迭代器将无效。在遍历向量或映射时,请小心删除其中的元素。正确的算法不明显,通常有一些微妙之处。感谢您的评论。我删除了答案中的“迭代器”部分。您可能希望将当前单词
作为避免复制的参考。您可能希望将当前单词
作为避免复制的参考。这正是我要找的。谢谢您的详细解释和帮助。正是我要找的。谢谢您的详细解释和帮助。