STL 算法分两大类:非变序算法与变序算法
算法 | 描述 |
---|---|
计数算法 | |
count() | 在指定范围内查找值与指定值匹配的所有元素 |
count_if() | 在指定范围内查找值满足指定条件的所有元素 |
搜索算法 | |
search() | 在目标范围内,根据元素相等性(即运算符==)或指定二元谓词搜索第一个满足条件的元素 |
search_n() | 在目标范围内搜索与指定值相等或满足指定谓词的n 个元素 |
find() | 在给定范围内搜索与指定值匹配的第一个元素 |
find_if() | 在给定范围内搜索满足指定条件的第一个元素 |
find_end() | 在指定范围内搜索最后一个满足特定条件的元素 |
find_first_of() | 在目标范围内搜索指定序列中的任何一个元素第一次出现的位置;在另一个重载版本中,它搜索满足指定条件的第一个元素 |
adjacent_find() | 在集合中搜索两个相等或满足指定条件的元素 |
比较算法 | |
equal() | 比较两个元素是否相等或使用指定的二元谓词判断两者是否相等 |
mismatch() | 使用指定的二元谓词找出两个元素范围的第一个不同的地方 |
lexicographical_compare() | 比较两个序列中的元素,以判断哪个序列更小 |
算法 | 描述 |
---|---|
初始化算法 | |
fill() | 将指定值分配给指定范围中的每个元素 |
fill_n() | 将指定值分配给指定范围中的前n 个元素 |
generate() | 将指定函数对象的返回值分配给指定范围中的每个元素 |
generate_n() | 将指定函数的返回值分配给指定范围中的前n 个元素 |
修改算法 | |
for_each() | 对指定范围内的每个元素执行指定的操作。当指定的参数修改了范围时,for_each 将是变序算法 |
transform() | 对指定范围中的每个元素执行指定的一元函数 |
复制算法 | |
copy() | 将一个范围复制到另一个范围 |
copy_backward() | 将一个范围复制到另一个范围,但在目标范围中将元素的排列顺序反转 |
删除算法 | |
remove() | 将指定范围中包含指定值的元素删除 |
remove_if() | 将指定范围中满足指定一元谓词的元素删除 |
remove_copy() | 将源范围中除包含指定值外的所有元素复制到目标范围 |
remove_copy_if() | 将源范围中除满足指定一元谓词外的所有元素复制到目标范围 |
unique() | 比较指定范围内的相邻元素,并删除重复的元素。该算法还有一个重载版本,它使用二元谓词来判断要删除哪些元素 |
unique_copy() | 将源范围内的所有元素复制到目标范围,但相邻的重复元素除外 |
替换算法 | |
replace() | 用一个值来替换指定范围中与指定值匹配的所有元素 |
replace_if() | 用一个值来替换指定范围中满足指定条件的所有元素 |
排序算法 | |
sort() | 使用指定的排序标准对指定范围内的元素进行排序,排序标准由二元谓词提供。排序可能改变相等元素的相对顺序 |
stable_sort() | 类似于sort,但在排序时保持相对顺序不变 |
partial_sort() | 将源范围内指定数量的元素排序 |
partial_sort_copy() | 将源范围内的元素复制到目标范围,同时对它们排序 |
分区算法 | |
partition() | 在指定范围中,将元素分为两组:满足指定一元谓词的元素放在第一个组中,其他元素放在第二组中。不一定会保持集合中元素的相对顺序 |
stable_partition() | 与partition 一样将指定范围分为两组,但保持元素的相对顺序不变 |
可用于有序容器的算法 | |
binary_search() | 用于判断一个元素是否存在于一个排序集合中 |
lower_bound() | 根据元素的值或二元谓词判断元素可能插入到排序集合中的第一个位置,并返回一个指向该位置的迭代器 |
upper_bound() | 根据元素的值或二元谓词判断元素可能插入到排序集合中的最后一个位置,并返回一个指向该位置的迭代器 |
#include <iostream>
#include <algorithm>
#include <vector>int main() {using namespace std;vector<int> numsInVec{ 2017, 0, -1, 42, 10101, 25 };cout << "Enter number to find in collection: ";int numToFind = 0;cin >> numToFind;auto element = find (numsInVec.cbegin(), // Start d(), // End of rangenumToFind); // Element to findif (element != d()) {cout << "Value " << *element << " found!" << endl;} else {cout << "No element contains value " << numToFind << endl;}cout << "Finding the first even number using find_if: " << endl;auto evenNum = find_if (numsInVec.cbegin(), // Start d(), // End of range[](int element) { return (element % 2) == 0; } );if (evenNum != d()) {cout << "Number '" << *evenNum << "' found at position [";// 确定找到的元素相对于容器起点的位置cout << distance (numsInVec.cbegin(), evenNum) << "]" << endl;}return 0;
}
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std;template <typename T>
void DisplayContents (const T& container) {for (auto element = container.cbegin(); element != d(); ++element) {cout << *element << ' ';} cout << endl;
}int main() {vector<int> numsInVec{ 2017, 0, -1, 42, 10101, 25, 9, 9, 9 };list<int> numsInList{ -1, 42, 10101 };cout << "The contents of the sample vector are: " << endl;DisplayContents (numsInVec);cout << "The contents of the sample list are: " << endl;DisplayContents (numsInList);cout << "search() for the contents of list in vector:" << endl;auto range = search (numsInVec.cbegin(), // Start range to d(), // End range to search innumsInList.cbegin(), // Start range to d()); // End range to search forif (range != d()) {cout << "Sequence in list found in vector at position: ";cout << distance (numsInVec.cbegin(), range) << endl;}cout << "Searching {9, 9, 9} in vector using search_n(): " << endl;auto partialRange = search_n (numsInVec.cbegin(), // d(), // End range3, // Count of item to be searched for9); // Item to search forif (partialRange != d()) {cout << "Sequence {9, 9, 9} found in vector at position: ";cout << distance (numsInVec.cbegin(), partialRange) << endl;}return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>// 一元谓词
template <typename elementType>
bool IsEven (const elementType& number) {return ((number % 2) == 0); // true, if even
}int main () {using namespace std;vector <int> numsInVec{ 2017, 0, -1, 42, 10101, 25 };size_t numZeroes = count (numsInVec.cbegin(), d(), 0); // 计算0的个数cout << "Number of instances of '0': " << numZeroes << endl;size_t numEvenNums = count_if (numsInVec.cbegin (),d (), IsEven <int> );cout << "Number of even elements: " << numEvenNums << endl;cout << "Number of odd elements: ";cout << numsInVec.size () - numEvenNums << endl;return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>int main () {using namespace std;vector<int> numsInVec (3);// fill all elements in the container with value 9fill (numsInVec.begin(), d(), 9);// Increase the size of the vector to hold size (6);// Fill the three elements starting at offset position 3 with value -9fill_n (numsInVec.begin() + 3, 3, -9); // 起始位置, 元素数, 要设置的值cout << "Contents of the vector are: " << endl;for (size_t index = 0; index < numsInVec.size(); ++index) {cout << "Element [" << index << "] = ";cout << numsInVec [index] << endl;}return 0;
}
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
#include <ctime>int main () {using namespace std;srand(time(NULL)); // seed random generator using timevector<int> numsInVec (5);generate (numsInVec.begin(), d(), // rangerand); // generator functioncout << "Elements in the vector are: ";for (size_t index = 0; index < numsInVec.size (); ++ index) {cout << numsInVec [index] << " ";} cout << endl;list<int> numsInList (5);generate_n (numsInList.begin(), 3, rand); // 起始位置, 调用rand()次数, randcout << "Elements in the list are: ";for (auto element = numsInList.begin(); element != d(); ++element) {cout << *element << ' ';} return 0;
}
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;template <typename elementType>
struct DisplayElementKeepcount {int count;DisplayElementKeepcount (): count (0) {}void operator () (const elementType& element) {++ count;cout << element << ' ';}
};int main () {vector<int> numsInVec{ 2017, 0, -1, 42, 10101, 25 };cout << "Elements in vector are: " << endl;DisplayElementKeepcount<int> functor =for_each (numsInVec.cbegin(), // Start d (), // End of rangeDisplayElementKeepcount<int> ()); // functorcout << endl;// 使用 for_each 返回值中存储的状态cout << "'" << unt << "' elements displayed" << endl;string str ("for_each and strings!");cout << "Sample string: " << str << endl;cout << "Characters displayed using lambda:" << endl;int numChars = 0;for_each (str.cbegin(), d (),[&numChars](char c) { cout << c << ' '; ++numChars; } );cout << endl;cout << "'" << numChars << "' characters displayed" << endl;return 0;
}
通过使用迭代器,可将容器及其实现同 STL 算法分离:transform() 是一种处理范围的算法,它无需知道实现这些范围的容器的细节。因此,虽然这里的输入范围为 vector,而输出范围为 deque,但该算法仍管用——只要指定范围的边界(提供给 transform 的输入参数)有效
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <iostream>
#include <functional>int main() {using namespace std;string str ("THIS is a TEst string!");cout << "The sample string is: " << str << endl;string size (str.size());transform (str.cbegin(), // start d(), // end source rangestrLowerCaseCopy.begin(), // start destination range::tolower); // unary functioncout << "Result of 'transform' on the string with 'tolower':" << endl;cout << """ << strLowerCaseCopy << """ << endl << endl;// Two sample vectors vector<int> numsInVec1{ 2017, 0, -1, 42, 10101, 25 };vector<int> numsInVec2 (numsInVec1.size(), -1);// A destination range for holding the result of additiondeque<int> sumInDeque (numsInVec1.size());transform (numsInVec1.cbegin(), // start of source d(), // end of source range 1numsInVec2.cbegin(), // start of source range 2sumInDeque.begin(), // start of destination rangeplus<int>()); // 头文件 function 中定义,将两个元素相加cout << "Result of 'transform' using binary function 'plus': " << endl;cout << "Index Vector1 + Vector2 = Result (in Deque)" << endl;for (size_t index = 0; index < numsInVec1.size(); ++index) {cout << index << " t " << numsInVec1 [index] << "t+ ";cout << numsInVec2[index] << " t = ";cout << sumInDeque[index] << endl;}return 0;
}
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std;template <typename T>
void DisplayContents(const T& container) {for (auto element = container.cbegin(); element != d(); ++element) {cout << *element << ' ';} cout << "| Number of elements: " << container.size() << endl;
}int main() {list<int> numsInList{ 2017, 0, -1, 42, 10101, 25 };cout << "Source (list) contains:" << endl;DisplayContents(numsInList);// Initialize vector to hold 2x elements as the listvector <int> numsInVec (numsInList.size() * 2);auto lastElement = copy (numsInList.cbegin(), // start d(), // end source rangenumsInVec.begin()); // start dest range// copy odd numbers from list into vectorcopy_if (numsInList.cbegin(), d(), lastElement,[](int element){return ((element % 2) != 0);});cout << "Destination (vector) after copy and copy_if:" << endl;DisplayContents(numsInVec);// Remove all instances of '0', resize vector using erase()auto newEnd = remove (numsInVec.begin(), d(), 0);ase (newEnd, d());// Remove all odd numbers from the vector using remove_ifnewEnd = remove_if (numsInVec.begin(), d(),[](int element) {return ((element % 2) != 0);} );ase (newEnd , d()); // resizingcout << "Destination (vector) after remove, remove_if, erase:" << endl;DisplayContents(numsInVec);return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;template <typename T>
void DisplayContents(const T& container) {for (auto element = container.cbegin(); element != d(); ++element)cout << *element << ' ';cout << "| Number of elements: " << container.size() << endl;
}int main () {vector<int> numsInVec (6);// fill first 3 elements with value 8, last 3 elements with 5fill (numsInVec.begin(), numsInVec.begin() + 3, 8);fill_n (numsInVec.begin () + 3, 3, 5);// std::random_shuffle 将容器内的值打乱random_shuffle (numsInVec.begin(), d());cout << "The initial contents of vector: " << endl;DisplayContents(numsInVec);cout << endl << "'std::replace' value 5 by 8" << endl;replace (numsInVec.begin(), d(), 5, 8);// 将所有偶数都替换为 -1 cout << "'std::replace_if' even values by -1" << endl;replace_if (numsInVec.begin (), d (),[](int element) {return ((element % 2) == 0); }, -1);cout << endl << "Vector after replacements:" << endl;DisplayContents(numsInVec);return 0;
}
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
using namespace std;template <typename T>
void DisplayContents(const T& container) {for (auto element = container.cbegin(); element != d(); ++element) {cout << *element << endl;}
}int main () {vector<string> vecNames{"John", "jack", "sean", "Anna"};vecNames.push_back ("jack");cout << "The initial contents of the vector are: " << endl;DisplayContents(vecNames);cout << "The sorted vector contains names in the order:" << endl;sort (vecNames.begin(), d());DisplayContents(vecNames);cout << "Searching for "John" using 'binary_search':" << endl;bool elementFound = binary_search (vecNames.begin(), d(), "John");if (elementFound) {cout << "Result: "John" was found in the vector!" << endl;} else {cout << "Element not found " << endl;} // 删除相邻的重复值auto newEnd = unique (vecNames.begin(), d());ase (newEnd, d());cout << "The contents of the vector after using 'unique':" << endl;DisplayContents(vecNames);return 0;
}
- 与 remove() 一样,unique() 也不调整容器的大小。它将元素前移,但不会减少元素总数。为避免容器末尾包含不想要或未知的值,务必在调用 unique() 后调用 vector::erase(),并将 unique() 返回的迭代器传递给它
- stable_sort() 确保排序后元素的相对顺序保持不变。为了确保相对顺序保持不变,将降低性能,这是一个需要考虑的因素,尤其在元素的相对顺序不重要时
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;bool IsEven (const int& num) {return ((num % 2) == 0);
}template <typename T>
void DisplayContents(const T& container) {for (auto element = container.cbegin(); element != d(); ++element) {cout << *element << ' ';} cout << "| Number of elements: " << container.size() << endl;
}int main () {vector<int> numsInVec{ 2017, 0, -1, 42, 10101, 25 };cout << "The initial contents: " << endl;DisplayContents(numsInVec);vector<int> vecCopy (numsInVec);cout << "The effect of using partition():" << endl;partition (numsInVec.begin(), d(), IsEven);DisplayContents(numsInVec);// stable_partition() 保持每个分区中元素的相对顺序不变,但速度更慢cout << "The effect of using stable_partition():" << endl;stable_partition (vecCopy.begin(), d(), IsEven);DisplayContents(vecCopy);return 0;
}
#include <algorithm>
#include <list>
#include <string>
#include <iostream>
using namespace std;template <typename T>
void DisplayContents(const T& container) {for (auto element = container.cbegin(); element != d(); ++element) {cout << *element << endl;}
}int main() {list<string> names{ "John", "Brad", "jack", "sean", "Anna" };cout << "Sorted contents of the list are: " << endl;names.sort();DisplayContents(names);cout << "Lowest index where "Brad" can be inserted is: ";auto minPos = lower_bound(names.begin(), d(), "Brad");cout << distance(names.begin(), minPos) << endl;cout << "The highest index where "Brad" can be inserted is: ";auto maxPos = upper_bound(names.begin(), d(), "Brad");cout << distance(names.begin(), maxPos) << endl;cout << endl;cout << "List after inserting Brad in sorted order: " << endl;names.insert(minPos, "Brad");DisplayContents(names);return 0;
}
本文发布于:2024-02-02 17:01:28,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170686448745198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |