IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

STL可能的误用-find_first_of和erase

Vimer 2011-02-27 22:55:26 累计浏览 2,912 次
本机暂存

一.string中find_first_of的误用
STL中提供的string可以说极大方便了对字符串的操作,但是很多函数由于样子上很相似,所以导致很容易理解错误,find_first_of和find就是一个很好的例子。
我们先来看一下string提供的查找相关的函数列表:

1
2
3
4
5
find_first_of() 查找第一个与value中的某值相等的字符
find_first_not_of() 查找第一个与value中的所有值都不相等的字符
find_last_of() 查找最后一个与value中的某值相等的字符
find_last_not_of() 查找最后一个与value中的所有值都不相等的字符
rfind() 查找最后一个与value相等的字符(逆向查找)

如此简洁的说明,其实完全没有把他们最重要的区别描述出来,请务必记住:
对于find和rfind:

  • 匹配的是整个被查找串

对于find_first_of,find_first_not_of,find_last_of,find_last_not_of:

  • 匹配的是被查找串中的任意字符

我们来测试一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
int main(int argc, const char *argv[])
{
    string src = "vimer.cn";
 
    string str1 = "mer";
    string str2 = "sre";
 
    size_t pos;
 
    pos = src.find(str1);
    cout<<pos<<endl;
 
    pos = src.find(str2);
    cout<<pos<<endl;
 
    pos = src.find_first_of(str1);
    cout<<pos<<endl;
 
    pos = src.find_first_of(str2);
    cout<<pos<<endl;
 
    return 0;
}

运行结果如下:

2
4294967295
2
3

结果中4294967295即string::npos,代表没有找到。而在find_first_of(str2)的时候返回的是3,即字符'e',证明了我们之前的说法。

二.erase函数的误用
STL的容器一般都会提供erase方法,而又有很多朋友需要在for循环中对容器进行erase,其实本来很简单的一个问题,现在却被搞得很复杂。
为了不给大家造成混淆,这里只列出两种正确的方法,大家按照这种方法来写就绝对没有问题,也不用考虑容器上的区别。
1.简短型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
int main(int argc, const char *argv[])
{
    map<unsigned int,int> myMap;
    int count = 10;
    for (int i = 0; i < count; i++)
    {
        myMap[i]=i;
    }
    for(map<unsigned int, int>::iterator it = myMap.begin(); it != myMap.end(); )
    {
        if (it->first == 3 || it->first == 9)
        {
            myMap.erase(it++);
        }
        else
        {
            it++;
        }
    }
    for(map<unsigned int, int>::iterator it = myMap.begin(); it != myMap.end(); ++it)
    {
        cout<<it->second<<endl;
    }
    return 0;
}

2.易读型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
int main(int argc, const char *argv[])
{
    map<unsigned int,int> myMap;
    int count = 10;
    for (int i = 0; i < count; i++)
    {
        myMap[i]=i;
    }
    for(map<unsigned int, int>::iterator it = myMap.begin(); it != myMap.end(); )
    {
        map<unsigned int, int>::iterator tempit = it;
        it++;
 
        if (tempit->first == 3 || tempit->first == 9)
        {
            myMap.erase(tempit);
        }
    }
    for(map<unsigned int, int>::iterator it = myMap.begin(); it != myMap.end(); ++it)
    {
        cout<<it->second<<endl;
    }
    return 0;
}

运行结果都为:

0
1
2
4
5
6
7
8

对于第一种方法,千万不要理解等同于:

1
2
3
4
5
6
//这样是错误的,不要模仿!
if (it->first == 3 || it->first == 9)
{
    myMap.erase(it);
}
it++;

对于笔者本人来说,更倾向第二种方法,因为虽然文章中是直接调用

1
myMap.erase(tempit);

但实际情况可能是调用一个函数,而在这个函数里面会有一堆逻辑出来判断是否要删除这个元素,这种情况下只有用第二种方法能够满足。

OK,STL的强大和危险性是成正比的,所以要熟练运用还是要深入理解才行,希望能和大家一起进步~

同分类推荐文章

  1. 对基本有序的序列排序算法 (2026-06-11 17:46:49)
  2. Four Levels Of Customer Understanding (2026-05-22 21:00:00)
  3. 除法的意义 (2026-04-12 20:52:17)

查看更多 算法 文章 →

建议继续学习

  1. 如何学好C++语言 (累计阅读 10,448)
  2. Emacs配置C/C++-mode的代码智能提示和自动补全 (累计阅读 10,411)
  3. colortail,让 tail 命令绚丽起来 (累计阅读 10,259)
  4. 在C++中实现foreach循环,比for_each更简洁! (累计阅读 9,499)
  5. 几个内存相关面试题(c/c++) (累计阅读 9,445)
  6. 关于使用STL的红黑树map还是hashmap的问题 (累计阅读 8,875)
  7. 浅析C++多线程内存模型 (累计阅读 8,802)
  8. C++ 多线程编程总结 (累计阅读 8,097)
  9. 使用gdb调试运行时的程序小技巧 (累计阅读 7,208)
  10. 在C++里写一个不能被继承的类 (累计阅读 6,580)