C++?STL實(shí)現(xiàn)非變易查找算法的示例代碼
C++ STL 中的非變易算法(Non-modifying Algorithms)是指那些不會修改容器內(nèi)容的算法,是C++提供的一組模板函數(shù),該系列函數(shù)不會修改原序列中的數(shù)據(jù),而是對數(shù)據(jù)進(jìn)行處理、查找、計(jì)算等操作,并通過迭代器實(shí)現(xiàn)了對序列元素的遍歷與訪問。由于迭代器與算法是解耦的,因此非變易算法可以廣泛地應(yīng)用于各種容器上,提供了極高的通用性和靈活性。
這些算法都是在頭文件 <algorithm> 中定義的,其主要包括以下幾類非變易算法:
1.查找算法:
find():在容器中查找指定值的元素,并返回第一個匹配的位置。find_if():根據(jù)給定的條件(函數(shù)對象或謂詞)查找容器中滿足條件的元素,并返回第一個匹配的位置。count():計(jì)算容器中等于指定值的元素個數(shù)。
2.遍歷算法:
for_each():對容器中的每個元素應(yīng)用指定的函數(shù)。accumulate():計(jì)算容器中元素的累加和。count_if():計(jì)算滿足給定條件的元素個數(shù)。
3.排序算法(不屬于查找和遍歷,但不會修改元素內(nèi)容):
sort():對容器中的元素進(jìn)行排序,默認(rèn)是按升序排列。partial_sort():對容器中的部分元素進(jìn)行排序。stable_sort():穩(wěn)定地對容器中的元素進(jìn)行排序。
通過它們可以高效地操作容器中的元素,這為C++開發(fā)者提供了更方便和安全的方式來處理數(shù)據(jù),減少了代碼的復(fù)雜性和錯誤的可能性。通過合理地運(yùn)用這些算法,可以極大地提高程序的執(zhí)行效率和代碼的可讀性。
1.遍歷容器元素
For_each 算法函數(shù),用于對序列中的每個元素執(zhí)行指定操作。for_each的用法如下:
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function f);
其中,first、last是迭代器,表示待執(zhí)行操作的序列的范圍;f是一個函數(shù)對象,用于指定要執(zhí)行的操作。調(diào)用for_each函數(shù)后,將會對[first, last]區(qū)間內(nèi)的每個元素調(diào)用一次f函數(shù),并將該元素作為f函數(shù)的參數(shù)。for_each函數(shù)返回一個函數(shù)對象f。
該函數(shù)用于對容器的元素進(jìn)行循環(huán)操作,常用于元素遍歷。
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
struct MyPrint{
int count; // 設(shè)置元素計(jì)數(shù)
MyPrint(){ count = 0; } // 初始化元素
void operator()(int x) // 重載小括號
{
cout << x << endl;
count++;
}
};
int main(int argc, char* argv[])
{
list<int> ls {1,2,3,4,5,6,7,8,9};
MyPrint p = for_each(ls.begin(), ls.end(), MyPrint());
cout << "Link Count: " << p.count << endl;
system("pause");
return 0;
}2.普通查找容器元素
Find 算法函數(shù),用于查找序列中指定值的第一個元素,并返回該元素的迭代器。find的用法如下:
template<class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T& value);
其中,first、last是迭代器,表示待查找的序列的范圍;value是需要查找的元素的值。調(diào)用find函數(shù)后,將會在[first, last]區(qū)間中查找第一個等于value的元素,并將該元素的迭代器作為函數(shù)返回值返回。如果未找到等于value的元素,則函數(shù)將返回last。
該算法用于查找某個值等于某值得元素,它在迭代區(qū)間是(frist,last)之間尋找value值。
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
list<int> ls{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list<int>::iterator it = find(ls.begin(), ls.end(),6);
if (it != ls.end())
{
cout << "找到了元素" << endl;
cout << "前一個元素: " << *(--it) << endl;
}
system("pause");
return 0;
}3.類查找容器元素
Find 算法函數(shù),用于查找序列中指定值的第一個元素,并返回該元素的迭代器。find的用法如下:
template<class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T& value);
其中,first、last是迭代器,表示待查找的序列的范圍;value是需要查找的元素的值。調(diào)用find函數(shù)后,將會在[first, last]區(qū)間中查找第一個等于value的元素,并將該元素的迭代器作為函數(shù)返回值返回。如果未找到等于value的元素,則函數(shù)將返回last。
該算法不僅可以查詢普通數(shù)據(jù)結(jié)構(gòu),還可以查詢結(jié)構(gòu)與類中數(shù)據(jù),如下則是一段演示案例;
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Person
{
public:
string m_name;
int m_age;
public:Person(string name, int age){
this->m_name = name;
this->m_age = age;
}
public: bool operator==(const Person &p){
// 重載 == 實(shí)現(xiàn)遍歷數(shù)據(jù)
if (this->m_name == p.m_name && this->m_age == p.m_age)
return true;
return false;
}
};
int main(int argc, char* argv[])
{
vector<Person> var;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
var.push_back(p1);
var.push_back(p2);
var.push_back(p3);
vector<Person>::iterator pos = find(var.begin(), var.end(), p1);
if (pos != var.end())
cout << "找到姓名: " << (*pos).m_name << endl;
system("pause");
return 0;
}4.條件查找容器元素
Find_if 算法函數(shù),用于查找序列中滿足指定條件的第一個元素,并返回該元素的迭代器。find_if的用法如下:
template<class InputIterator, class UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred);
其中,first、last是迭代器,表示待查找的序列的范圍;pred是一個一元謂詞函數(shù),用于指定查找條件。調(diào)用find_if函數(shù)后,將會在[first, last]區(qū)間中查找第一個謂詞pred返回true的元素,并將該元素的迭代器作為函數(shù)返回值返回。如果未找到滿足條件的元素,則函數(shù)將返回last。
與上方的普通查找相比,該查找可以添加回調(diào)函數(shù),用于對查到的數(shù)據(jù)進(jìn)行篩選和過濾操作,如下所示案例中尋找第一個被5整除的元素。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool MyFunction(int x) { return x % 5 ? 0 : 1; }
int main(int argc, char* argv[])
{
vector<int> var(20);
// 循環(huán)生成數(shù)據(jù)
for (unsigned int x = 0; x < var.size(); x++)
var[x] = (x + 1) * (x + 3);
// 循環(huán)遍歷,并判斷是否符合條件
vector<int>::iterator it = find_if(var.begin(),var.end(),MyFunction);
if (it != var.end())
{
// 尋找第一個被5整除的元素
cout << *it << endl;
cout << "元素索引: " << it - var.begin() << endl;
}
system("pause");
return 0;
}5.條件查找類容器元素
Find_if 算法函數(shù),用于查找序列中滿足指定條件的第一個元素,并返回該元素的迭代器。find_if的用法如下:
template<class InputIterator, class UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred);
其中,first、last是迭代器,表示待查找的序列的范圍;pred是一個一元謂詞函數(shù),用于指定查找條件。調(diào)用find_if函數(shù)后,將會在[first, last]區(qū)間中查找第一個謂詞pred返回true的元素,并將該元素的迭代器作為函數(shù)返回值返回。如果未找到滿足條件的元素,則函數(shù)將返回last。
如下一個案例中,實(shí)現(xiàn)了查詢Person類中的特定數(shù)據(jù),查找ptr中的數(shù)據(jù)是否存在于我們的結(jié)構(gòu)中。
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;
class Person
{
public:
string m_name;
int m_age;
public:Person(string name, int age){
this->m_name = name;
this->m_age = age;
}
public: bool operator==(const Person &p){
// 重載 == 實(shí)現(xiàn)遍歷數(shù)據(jù)
if (this->m_name == p.m_name && this->m_age == p.m_age)
return true;
return false;
}
};
// 使用binary_function適配函數(shù)實(shí)現(xiàn)傳遞兩個Person數(shù)據(jù)
class MyCompare:public binary_function<Person *,Person *,bool>
{
public: bool operator()(Person *p1,Person *p2) const {
// 對比函數(shù),重載() 用于實(shí)現(xiàn)指針元素的對比
if (p1->m_name == p2->m_name && p1->m_age == p2->m_age)
return true;
return false;
}
};
int main(int argc, char* argv[])
{
vector<Person *> var;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
var.push_back(&p1);
var.push_back(&p2);
var.push_back(&p3);
// 查找這個屬性的類成員
Person *ptr = new Person("bbb", 20);
// 通過使用bind2nd綁定事件,將ptr傳遞給MyCompare() 即可實(shí)現(xiàn)兩個類屬性的對比查找
vector<Person *>::iterator pos = find_if(var.begin(), var.end(), bind2nd(MyCompare(),ptr));
if (pos != var.end())
cout << "找到姓名: " << (*pos)->m_name << endl;
system("pause");
return 0;
}6.鄰近查找容器元素
Adjacent_find 算法函數(shù),用于查找相鄰元素的第一個出現(xiàn)位置。adjacent_find的用法如下:
template<class InputIterator> InputIterator adjacent_find(InputIterator first, InputIterator last);
其中,first、last是迭代器,表示待查找的序列的范圍。調(diào)用adjacent_find函數(shù)后,將會在[first, last]區(qū)間中查找相鄰元素的第一個出現(xiàn)位置,并將找到的元素的迭代器作為函數(shù)返回值返回。如果未找到相鄰元素,則函數(shù)將返回last。
該函數(shù)用于查找相等或滿足條件的相鄰的重復(fù)的元素,找到了返回第一個出現(xiàn)位置的迭代器,如下則是一段演示案例;
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
bool MyFunction(int x,int y) { return (x - y) % 2 == 0 ? 1 : 0; }
int main(int argc, char* argv[])
{
list<int> ls {1,2,3,4,5,6,6,7,8,9,10};
// 查找鏈表中鄰接相等的元素
list<int>::iterator it = adjacent_find(ls.begin(), ls.end());
if (it != ls.end())
cout << *it << endl;
// 查找基偶性相同的鄰接元素
it = adjacent_find(ls.begin(), ls.end(), MyFunction);
if (it != ls.end())
{
cout << *it << endl;
it++;
cout << *it << endl;
}
system("pause");
return 0;
}7.范圍查找容器元素
Find_first_of 算法函數(shù),用于查找第一個出現(xiàn)于另一個序列中的指定元素。find_first_of的用法如下:
template<class InputIterator, class ForwardIterator> InputIterator find_first_of(InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2);
其中,first1、last1是迭代器,表示待查找的序列的范圍;first2、last2是迭代器,表示要查找的元素序列的范圍。調(diào)用find_first_of函數(shù)后,將會在[first1, last1]區(qū)間中查找第一個與[first2, last2]中任意一個元素相等的元素,并將找到的元素的迭代器作為函數(shù)返回值返回。如果未找到滿足條件的元素,則函數(shù)將返回last1。
該算法可用于查找位于某個范圍之內(nèi)的元素,如下則是一段演示案例;
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
char * str1 = "hello";
char * str2 = "lyshark This is a test case. Thank you for using it lyshark.";
char * result = find_first_of(str1, str1 + strlen(str1), str2, str2 + strlen(str2));
// 字符串str1的第一個字符,第一次出現(xiàn)在str2中的字符為.
cout << *result << endl;
system("pause");
return 0;
}8.普通元素計(jì)數(shù)統(tǒng)計(jì)
Count 算法函數(shù),用于統(tǒng)計(jì)序列中指定值的元素個數(shù)。count函數(shù)的用法如下:
template<class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const T& value);
其中,first、last是迭代器,表示待計(jì)數(shù)的序列的范圍;value是需要計(jì)數(shù)的元素的值。調(diào)用count函數(shù)后,將會在[first, last]區(qū)間中統(tǒng)計(jì)等于value的元素個數(shù),并將結(jié)果作為函數(shù)返回值返回。
該算法用于計(jì)算容器中某個給定值得出現(xiàn)次數(shù),如下則是一段演示案例;
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
list<int> ls;
// 批量插入測試數(shù)據(jù)
for (int x = 0; x < 100; x++)
{
ls.push_back(x % 20);
}
// 統(tǒng)計(jì)元素value出現(xiàn)次數(shù),將次數(shù)放入num中.
int num = 0; int value = 9;
num = count(ls.begin(), ls.end(), value);
cout << "這個值出現(xiàn)過(次): " << num << endl;
system("pause");
return 0;
}9.條件元素計(jì)數(shù)統(tǒng)計(jì)
Count_if 算法函數(shù),用于統(tǒng)計(jì)滿足給定條件的元素個數(shù)。count_if函數(shù)的用法如下:
template<class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, UnaryPredicate pred);
其中,first、last是迭代器,表示待計(jì)數(shù)的序列的范圍;pred是一個一元謂詞函數(shù),用于指定計(jì)數(shù)條件。調(diào)用count_if函數(shù)后,將會在[first, last]區(qū)間中統(tǒng)計(jì)滿足謂詞pred的元素個數(shù),并將結(jié)果作為函數(shù)返回值返回。
該算法與Count算法非常類似,區(qū)別在于Count_if可以在統(tǒng)計(jì)前增加判斷條件,如下則是一段演示案例;
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
struct Student{
struct Info{
char *name; // 學(xué)生姓名
int year; // 學(xué)生年齡
};
int id; // 學(xué)號
Info stu; // 學(xué)生Info數(shù)據(jù)
Student(int _id, char *_name, int _year)
{
id = _id;
stu.name = _name;
stu.year = _year;
}
};
// 獲取學(xué)生年齡大于20歲并且小于30歲的人
bool GetRange(pair<int, Student::Info> s)
{
if (s.second.year > 20 && s.second.year < 30)
return 1;
return 0;
}
int main(int argc, char* argv[])
{
// 初始化學(xué)生數(shù)據(jù)
Student stu1 = Student(1, "admin", 10);
Student stu2 = Student(2, "guest", 21);
Student stu3 = Student(3, "lyshark", 35);
// 映射Map結(jié)構(gòu)數(shù)據(jù),并將上方的數(shù)據(jù)插入到Map中
map<int, Student::Info> mp;
pair<int, Student::Info> pairSt1(stu1.id, stu1.stu);
mp.insert(pairSt1);
pair<int, Student::Info> pairSt2(stu2.id, stu2.stu);
mp.insert(pairSt2);
pair<int, Student::Info> pairSt3(stu3.id, stu3.stu);
mp.insert(pairSt3);
// 條件統(tǒng)計(jì),統(tǒng)計(jì)出年齡大于20歲并且小于30歲的人有多少個= num
int num = 0;
num = count_if(mp.begin(), mp.end(), GetRange);
cout << num << endl;
system("pause");
return 0;
}10.數(shù)組查找算法
Binary_search 算法函數(shù),用于在有序序列中查找某個元素。binary_search的用法如下:
template<class ForwardIterator, class T> bool binary_search(ForwardIterator first, ForwardIterator last, const T& value);
其中,first、last是前向迭代器,表示待查找的有序序列的范圍;value是需要查找的元素的值。調(diào)用binary_search函數(shù)后,將會在[first, last]區(qū)間中使用二分查找算法查找value。如果value存在于區(qū)間[first, last]中,則函數(shù)返回true;否則函數(shù)返回false。
該算法就是折半查找法,查找的元素集合必須是一個有序的序列,如下則是一段演示案例;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
vector<int> var{ 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10 };
bool ret = binary_search(var.begin(), var.end(), 4);
if (ret)
cout << "found ok" << endl;
else
cout << "not found" << endl;
system("pause");
return 0;
}11.元素不匹配查找
Mismatch 算法函數(shù),用于查找兩個序列中第一個不匹配的元素。mismatch函數(shù)的用法如下:
template<class InputIterator1, class InputIterator2> pair<InputIterator1,InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
其中,first1、last1是迭代器,表示第一個序列的范圍;first2是迭代器,表示第二個序列的起始位置。調(diào)用mismatch函數(shù)后,將會在[first1, last1]區(qū)間和以first2為起始位置的序列進(jìn)行元素值的逐一比較,若兩個序列中對應(yīng)元素值都相等,則繼續(xù)比較下一個元素。一旦出現(xiàn)對應(yīng)元素不相等時,函數(shù)返回一個pair對,pair對的第一個元素是距離[first1, last1]開頭最近不匹配的元素的迭代器,pair對的第二個元素是距離first2開頭最近不匹配的元素的迭代器。
該算法函數(shù)比較兩個序列,并從中找出首個不匹配元素的位置,如下則是一段演示案例;
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
bool StrEqual(const char* x, const char* y)
{
return strcmp(x, y) == 0 ? 1 : 0;
}
int main(int argc, char* argv[])
{
vector<int> var1{ 2, 0, 0, 6 };
vector<int> var2{ 2, 0, 0, 7 };
// 檢測var1與var2中不匹配元素數(shù),并輸出
pair<vector<int>::iterator, vector<int>::iterator> result;
result = mismatch(var1.begin(), var1.end(), var2.begin());
if (result.first == var1.end() && result.second == var1.end())
cout << "var1 與var2完全一致" << endl;
else
// var1 和var2不相同,不匹配的數(shù)是
cout << "var1 = " << *result.first << " --> var2= " << *result.second << endl;
// ---------------------------------------------------------------------------------
// 針對字符串的不匹配檢測
char * str1[] = { "apple", "pear", "banana", "grape" };
char * str2[] = { "apple", "pear", "banana", "zoops" };
pair<char **, char **> result2 = mismatch(str1, str1 + 4, str2, StrEqual);
if (result2.first != str1 + 4 && result2.second != str2 + 4)
{
// 成立則說明兩個str子串有不同的地方,并輸出他們之間的不同點(diǎn)
cout << "str1 = > " << str1[result2.first - str1] << endl << endl;
cout << "str2 = > " << str2[result2.second - str2] << endl;
}
system("pause");
return 0;
}12.元素相等的判斷
Equal 算法函數(shù),用于判斷兩個序列是否在元素值上相等。equal函數(shù)的用法如下:
template<class InputIterator1, class InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
其中,first1、last1是迭代器,表示第一個序列的范圍;first2是迭代器,表示第二個序列的起始位置。調(diào)用equal函數(shù)后,將會在[first1, last1]區(qū)間和以first2為起始位置的序列進(jìn)行元素值的逐一比較,若兩個序列中對應(yīng)元素的值都相等,則函數(shù)返回true,否則函數(shù)返回false。
該算法實(shí)現(xiàn)逐一比較兩個序列的元素是否相等,該函數(shù)不返回迭代器,如下則是一段演示案例;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool absEqual(const int x,const int y)
{
return (x == abs(y) || abs(x) == y) ? 1 : 0;
}
int main(int argc, char* argv[])
{
vector<int> var1;
vector<int> var2;
// 初始化向量
for (unsigned int x = 0; x < var1.size(); x++)
{
var1[x] = x;
var2[x] = -1 * x;
}
// 判斷v1和v2元素的絕對值是否完全相等
if (equal(var1.begin(), var1.end(), var2.begin(), absEqual))
{
cout << "完全相等" << endl;
}
system("pause");
return 0;
}13.子序列搜索算法
Search 算法函數(shù),用于在一個序列中查找另一個子序列。search函數(shù)的用法如下:
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
其中,first1、last1是迭代器,表示待查找的序列的范圍;first2、last2是迭代器,表示要查找的子序列的范圍。調(diào)用search函數(shù)后,將會在[first1, last1]區(qū)間中查找第一個與[first2, last2]相匹配的子序列,并返回距離區(qū)間開始點(diǎn)最近的元素的迭代器,如果沒有找到匹配的子序列,將返回last1。
該算法實(shí)現(xiàn)了在一個序列中搜索與另一個序列匹配的子序列,如下則是一段演示案例;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
vector<int> var1 = { 5, 6, 7, 8, 9 };
vector<int> var2 = { 7, 8 };
// 檢查var2是否構(gòu)成var1的子序列
vector<int>::iterator it;
it = search(var1.begin(), var1.end(), var2.begin(),var2.end());
if (it != var1.end())
{
// 輸出var2元素包含在var1中,的起始元素為
cout << "Offset = " << it - var1.begin() << endl;
}
system("pause");
return 0;
}14.重復(fù)元素子序列搜索
Search_n 算法函數(shù),用于在一個序列中查找連續(xù)n個符合條件的元素。search_n函數(shù)的用法如下:
template<class ForwardIterator, class Size, class T, class BinaryPredicate> ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value, BinaryPredicate pred);
其中,first、last是迭代器,表示待查找的序列的范圍;count表示需要匹配的元素個數(shù);value表示需要匹配的元素值;pred為一個謂詞函數(shù),用于指定匹配方式。調(diào)用search_n函數(shù)后,將會在[first, last]區(qū)間中查找是否有count個連續(xù)的value元素,并返回指向第一個符合條件的元素位置的迭代器。如果沒有找到符合條件的元素,將返回last。
該算法搜索序列中是否有一系列元素值均為某個給定值得子序列,如下則是一段演示案例;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
vector<int> var1 = { 5, 6, 7, 8,8,8,9 };
// 查找var1中存在三個連續(xù)是8的數(shù)據(jù)
vector<int>::iterator it;
it = search_n(var1.begin(), var1.end(), 3, 8);
if (it != var1.end())
{
cout << "var1中存在三連8" << endl;
}
system("pause");
return 0;
}15.最后一個子序列搜索
Find_end 算法函數(shù),用于在一個序列中查找另一個序列中的最后一次出現(xiàn)位置。find_end函數(shù)的用法如下:
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
其中,first1、last1是迭代器,表示待查找的序列的范圍;first2、last2是迭代器,表示要查找的子序列的范圍。調(diào)用find_end函數(shù)后,將會在[first1, last1]區(qū)間中查找最后一個與[first2, last2]相匹配的子序列,并返回距離區(qū)間結(jié)束點(diǎn)的最后一個元素的迭代器,如果沒有找到匹配的子序列,將返回last1。
簡單來講,該算法實(shí)現(xiàn)了在一個序列中搜索出最后一個與另一個序列匹配的子序列,如下是一段應(yīng)用案例。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
vector<int> var1 = { -5,1,2,-6,-8,1,2,-11 };
vector<int> var2 = { 1, 2 };
// var1中查找最后一個子序列var2
vector<int>::iterator it;
it = find_end(var1.begin(), var1.end(), var2.begin(), var2.end());
// 打印子序列在var1的起始位置,輸出起offset
if (it != var1.end())
cout << "offset = [" << it - var1.begin() << "]" << endl;
system("pause");
return 0;
}以上就是C++ STL實(shí)現(xiàn)非變易查找算法的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++ STL非變易查找算法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言實(shí)現(xiàn)五子棋對戰(zhàn)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)五子棋對戰(zhàn)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
C語言代碼實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言代碼實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實(shí)例(串口助手開發(fā))
這篇文章主要介紹了Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實(shí)例(串口助手開發(fā)),需要的朋友可以參考下2020-03-03
C++??STL?_?Vector使用及模擬實(shí)現(xiàn)
這篇文章主要介紹了C++ STL_Vector使用及模擬實(shí)現(xiàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08

