起始算法有很多,或者说太多,这里不写了,主要写一写在 vector deque stack queue set map 中出现过的算法,其他算法,以后在此补充!
这些算法使用时候,包含:#include<algorithm>
其余算法参考:https://blog.csdn.net/tick_tock97/article/details/71316372
在线手册:http://www.cplusplus.com/reference/algorithm
首先,总结下,博客中各个容器自带的算法:
string: http://www.cplusplus.com/reference/string/string/?kw=string
erase find copy data
vector:http://www.cplusplus.com/reference/vector/vector/
erase clear
stack queue 略
list: http://www.cplusplus.com/reference/list/list/remove/
erase clear sort remove (removeif unique merge)
set / multiset : http://www.cplusplus.com/reference/set/set/
erase clear find count
map / multimap: http://www.cplusplus.com/reference/map/map/
erase clear find count
一、sort
没有返回值;
1 std::sort 2 default (1) 3 template <class RandomAccessIterator> 4 void sort (RandomAccessIterator first, RandomAccessIterator last); 5 custom (2) 6 template <class RandomAccessIterator, class Compare> 7 void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1 // sort algorithm example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::sort 4 #include <vector> // std::vector 5 6 bool myfunction (int i,int j) { return (i<j); } 7 8 struct myclass { 9 bool operator() (int i,int j) { return (i<j);} 10 } myobject; 11 12 int main () { 13 int myints[] = {32,71,12,45,26,80,53,33}; 14 std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33 15 16 // using default comparison (operator <): 17 std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 18 19 // 仿函数不必多言 20 std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) 21 22 // using object as comp 23 std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) 24 25 // print out content: 26 std::cout << "myvector contains:"; 27 for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) 28 std::cout << ' ' << *it; 29 std::cout << ' '; 30 31 return 0; 32 } 33 34 Output: 35 myvector contains: 12 26 32 33 45 53 71 80
二、find
返回改元素迭代器【没找到则返回 .end()】
自带该算法的容器有:
string【还带有rfind】: https://www.cnblogs.com/winslam/p/9405673.html
1 std::find 2 template <class InputIterator, class T> 3 InputIterator find (InputIterator first, InputIterator last, const T& val);
可以看到,find的返回值是一个同输入类型的迭代器。一般地,如果找到了该元素,就会返回该元素地泛型指针,如果没找到就默认返回该容器的 .end()。
find算法的底层实现如下:
1 template<class InputIterator, class T> 2 InputIterator find (InputIterator first, InputIterator last, const T& val) 3 { 4 while (first!=last) { 5 if (*first==val) return first; 6 ++first; 7 } 8 return last; 9 }
例如:
1 vector<int> vec; 2 it = find(vec.begin(), vec.end(), val); 3 if(it != vec.end()) // 代表找到了 4 { 5 cout << "*found" << endl; 6 } 7 else // 代表 it = vec.end() 也就是没找到 8 { 9 cout << "not found" << endl; 10 }
1 // find example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::find 4 #include <vector> // std::vector 5 6 int main () { 7 // using std::find with array and pointer: 8 int myints[] = { 10, 20, 30, 40 }; 9 int * p; 10 11 // <1>在数组中查找 12 p = std::find (myints, myints+4, 30); 13 if (p != myints+4) // 注意这里写法 14 std::cout << "在数组中找到元素:" << *p << ' '; 15 else 16 std::cout << "Element not found in myints "; 17 18 // <2>在容器中查找 19 std::vector<int> myvector (myints,myints+4);//数组初始化容器 20 std::vector<int>::iterator it; 21 22 it = find (myvector.begin(), myvector.end(), 30); 23 if (it != myvector.end()) 24 std::cout << "在容器中找到元素: " << *it << ' '; 25 else 26 std::cout << "容器中没有这个元素 "; 27 28 return 0; 29 }
三、count
返回元素出现的次数
1 std::count 2 template <class InputIterator, class T> 3 typename iterator_traits<InputIterator>::difference_type 4 count (InputIterator first, InputIterator last, const T& val);
实现如下:
1 template <class InputIterator, class T> 2 typename iterator_traits<InputIterator>::difference_type 3 count (InputIterator first, InputIterator last, const T& val) 4 { 5 typename iterator_traits<InputIterator>::difference_type ret = 0; 6 while (first!=last) { 7 if (*first == val) ++ret; 8 ++first; 9 } 10 return ret; 11 }
1 // count algorithm example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::count 4 #include <vector> // std::vector 5 6 int main () { 7 // 统计元素在数组中出现次数 8 int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements 9 int mycount = std::count (myints, myints+8, 10); 10 std::cout << "10 appears " << mycount << " times. "; 11 12 // 统计元素在容器中出现次数 13 std::vector<int> myvector (myints, myints+8); 14 mycount = std::count (myvector.begin(), myvector.end(), 20); 15 std::cout << "20 appears " << mycount << " times. "; 16 return 0; 17 }
四、remove(参考《STL源码剖析》Page356)
自带该算法的容器有:
list https://www.cnblogs.com/winslam/p/9416335.html
1、执行remove之后,对象size不变,【不是和erase一样真的删除了】
2、remove是将相同元素,移动到容器后端;例如:
vec : 10 20 30 20 30 10
执行:vector<int>::iterator pend = remove(vec.begin(), vec.end(), 20);
vec: 10 30 30 10 ? ?
^
|
itr指向---------------------
例如:
1 // remove algorithm example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::remove 4 #include<vector> 5 6 using namespace std; 7 8 int main() { 9 int myints[] = { 10,20,30,30,20,10,10,20 }; // 10 20 30 30 20 10 10 20 10 11 vector<int> vec(myints, myints + sizeof(myints)/sizeof(int)); 12 // bounds of range: 13 //int* pbegin = myints; 14 //int* pend = pbegin + sizeof(myints) / sizeof(int); 15 16 //pend = std::remove(pbegin, pend, 20); // 10 30 30 10 10 ? ? ? 17 //std::cout << "range contains:"; 18 //for (int* p = pbegin; p != pend; ++p) 19 // std::cout << ' ' << *p; 20 //std::cout << ' '; 21 22 //================================================================ 23 24 vector<int>::iterator pend = remove(vec.begin(), vec.end(), 20); 25 // 执行remove之后 vec的size不变 26 // vec = 10 30 30 10 10 ? ? ? 至于后面三个,是残余数据不确定的 27 for(vector<int>::iterator itr = vec.begin(); itr != pend; itr++ ) 28 { 29 cout << *itr << " "; // 10 30 30 10 10 30 } 31 cout << endl; 32 vec.erase(pend, vec.end()); // 你可以再加一步,完成删除操作 10 30 30 10 10
33 34 return 0; 35 }
有关 remove_copy remove_if remove_copy_if 暂时不更新;用什么学什么,就是这个原则。