STL可能的误用-find_first_of和erase
2011-07-28 14:54
323 查看
一.string中find_first_of的误用STL中提供的string可以说极大方便了对字符串的操作,但是很多函数由于样子上很相似,所以导致很容易理解错误,find_first_of和find就是一个很好的例子。
我们先来看一下string提供的查找相关的函数列表:
如此简洁的说明,其实完全没有把他们最重要的区别描述出来,请务必记住:
对于find和rfind:匹配的是整个被查找串
对于find_first_of,find_first_not_of,find_last_of,find_last_not_of:匹配的是被查找串中的任意字符
我们来测试一下:
运行结果如下:
STL的容器一般都会提供erase方法,vector,list等的erase方法都会返回下一个元素的指针,所以在for循环中对容器进行erase时,代码一般如下即可:
但是偏偏stl的map的erase不会返回下一个的指针,所以使用起来很让人困惑。其实本来很简单的一个问题,现在却被搞得很复杂。
为了不给大家造成混淆,这里只列出两种正确的方法,大家按照这种方法来写就绝对没有问题,也不用考虑不同的stl的map实现。
1.简短型
2.易读型
运行结果都为:
对于笔者本人来说,更倾向第二种方法,因为虽然文章中是直接调用
但实际情况可能是调用一个函数,而在这个函数里面会有一堆逻辑出来判断是否要删除这个元素,这种情况下只有用第二种方法能够满足。原文链接地址 : http://www.vimer.cn/?p=2008
我们先来看一下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 56 | #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方法,vector,list等的erase方法都会返回下一个元素的指针,所以在for循环中对容器进行erase时,代码一般如下即可:
1 2 3 4 56 | for(vector<uint32_t>::iterator it = myVec.begin(); it != myVec.end();) { if (it->data == data) { it = myVec.erase(it); } else { ++it; } } |
为了不给大家造成混淆,这里只列出两种正确的方法,大家按照这种方法来写就绝对没有问题,也不用考虑不同的stl的map实现。
1.简短型
1 2 3 4 56 | #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; } |
1 2 3 4 56 | #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 56 | //这样是错误的,不要模仿! if (it->first == 3 || it->first == 9) { myMap.erase(it); } it++; |
1 | myMap.erase(tempit); |
相关文章推荐
- stl:find _first_of与search用法
- STL_算法_查找算法(find_first_of、find_last_of)
- 输入1,2,3,4....输出1 2 3 4并且输出结果要换行(STL Find_first_of)
- (原創) 如何使用find_first_of() algorithm? (C/C++) (STL)
- C++的STL之find_first_of
- c++之STL(13) STL 算法 - 查找算法(4)find_first_of(b,e,sb,se) find_first_of(b,e,sb,se,bp)
- C++STL库中string类的find()和findfirstof()函数比较
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- STL 之find,find_if,find_end,find_first_of
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- C++ STL 算法:查找算法(4)find_first_of
- STL 之find,find_if,find_end,find_first_of
- find_first_of (STL Sample)
- strrchr与strchr函数 find_first_of与find_last_of
- C++ string::find_first_of
- find ,find_first_of , find_if , adjacent_find的使用
- 泛型算法系列7:find_first_of()
- string中的find(),find_first_of(),find_first_not_of()之间区别
- STL中remove_if()/find_if()/replace_if()的使用方法(The usages of remove_if()/find_if()/replace_if() )
- 算法之旅,直奔<algorithm>之十七 find_first_of