目的:去掉std::deque
中不符合条件的元素,在下面的代码中,是把大写转成小写,把其他符号去掉。
#include "iostream"
#include "deque"
using namespace std;
int main ()
{
deque<char> string;
char ch;
while((ch = getchar()) != EOF)
string.push_back(ch);
for (deque<char>::iterator it = string.begin(); it != string.end(); ++it)
{
if ('a' <= *it && *it <= 'z')
continue;
else if ('A' <= *it && *it <= 'Z')
*it = tolower(*it);
else
string.erase(it);
}
while (!string.empty())
{
cout << string.front();
string.pop_front();
}
}
输入:
das ;ds;a ;das; d;as
d;as ;das; ;das
输出:
dasdsadasdasdas;das das
请教下一为何会漏掉某些字符?
The iterator is invalid. To remove unmatched elements in the container, you can first move remove_if to the end of the container and then call the container's own erase to delete them in batches
If you don’t need to delete it, you can do this
The return value of remove_if and remove is the starting position of the element that can be deleted
Let me explain to you, in the place of two numbers, if it now points to the first one; then it meets the conditions for deletion erase(it). At this time it++, but at this time it no longer points to the second; but the following d. Because when you erase(it), the pointer after it will move forward. it already points to the second one; how can you still point to the second one in it++? That's why you use spaces and semicolons together. So your program itself is wrong, because it does not check every character. Every time you perform an erase(it) operation, the following characters will not be checked regardless of whether they are legal or not. For the correct writing method, you can refer to the following (for reference only):
After calling the erase function, the original iterator it becomes invalid, and the ++ operation on it is unpredictable.