簡(jiǎn)介
find
//查找
find_if
//按條件查找
adjacent_find
//查找相鄰重復(fù)元素
count
//統(tǒng)計(jì)一個(gè)元素出現(xiàn)的個(gè)數(shù)
binary_search
//二分查找法
count_if
//按條件統(tǒng)計(jì)元素的個(gè)數(shù)
find
find 查找指定元素造壮,找到的話返回指定元素的迭代器,找不到返回結(jié)束元素的迭代器
find(iterator begin,iterator end,value);
value 表示要茶查找的元素
- 查找內(nèi)置數(shù)據(jù)類型
//查找內(nèi)置數(shù)據(jù)類型
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
//查找元素5
vector<int>::iterator pos = find(v.begin(), v.end(), 5);
if (pos == v.end())
{
cout << "未找到骂束!" << endl;
}
else
{
cout << "找到元素:" << *pos << endl;
}
}
- 查找自定義數(shù)據(jù)類型
class Person
{
public:
Person(string name, int age)
{
this->age = age;
this->name = name;
}
//重載==號(hào)
bool operator==(const Person& p)
{
if (this->age == p.age && this->name == p.name)
{
return true;
}
else
{
return false;
}
}
string name;
int age;
};
//查找自定義數(shù)據(jù)類型
void test02()
{
vector<Person> vp;
vp.push_back(Person("aaa", 20));
vp.push_back(Person("bbb", 10));
vp.push_back(Person("ccc", 36));
vp.push_back(Person("ddd", 29));
vp.push_back(Person("eee", 15));
vector<Person>::iterator pos = find(vp.begin(), vp.end(), Person("ddd", 29));
if (pos == vp.end())
{
cout << "未找到耳璧!" << endl;
}
else
{
cout << "找到了, 姓名:" << pos->name << " 年齡:" << pos->age << endl;
}
}
find用來(lái)查找自定義數(shù)據(jù)類型的時(shí)候需要重載“==”運(yùn)算符,否則find底層無(wú)法對(duì)自定義數(shù)據(jù)類型進(jìn)行比較展箱。
find_if
find_if(iterator begin,iterator end, _Pred);
_Perd代表謂詞或者函數(shù)
按條件查找元素旨枯,查找到的話返回指定位置的迭代器,找不到的話返回結(jié)束迭代器
- 內(nèi)置數(shù)據(jù)類型
class GreaterThanFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
//查找內(nèi)置數(shù)據(jù)類型
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
//查找元素比5大的元素
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterThanFive());
if (pos == v.end())
{
cout << "未找到混驰!" << endl;
}
else
{
cout << "大于5的數(shù)字為:" << *pos << endl;
}
}
- 自定義數(shù)據(jù)類型
class Person
{
public:
Person(string name, int age)
{
this->age = age;
this->name = name;
}
string name;
int age;
};
class Compare
{
public:
bool operator()(const Person &p)
{
return p.age > 20;
}
};
//查找自定義數(shù)據(jù)類型
void test02()
{
vector<Person> vp;
vp.push_back(Person("aaa", 20));
vp.push_back(Person("bbb", 10));
vp.push_back(Person("ccc", 36));
vp.push_back(Person("ddd", 29));
vp.push_back(Person("eee", 15));
vector<Person>::iterator pos = find_if(vp.begin(), vp.end(), Compare());
if (pos == vp.end()) //姓名ccc 年齡36
{
cout << "未找到攀隔!" << endl;
}
else
{
cout << "找到了, 姓名:" << pos->name << " 年齡:" << pos->age << endl;
}
}
adjacent_find
adjacent_find(iterator begin,iterator end)
返回相鄰重復(fù)的元素的第一個(gè)位置的迭代器,若沒有找到則返回結(jié)束迭代器
void test01()
{
vector<int>v;
v.push_back(0);
v.push_back(1);
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(3);
//查找相鄰重復(fù)元素
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos == v.end())
{
cout << "未找到栖榨!" << endl;
}
else
{
cout << "相鄰重復(fù)元素為:" << *pos << endl;
}
}
binary_search
bool binary_serach(iterator begin(),iterator end(),value)
查找指定的元素 找到返回true查不到返回false
只能在有序列里面使用昆汹,不能在無(wú)序序列里面使用
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
bool ret = binary_search(v.begin(), v.end(), 4);
if (ret == true)
{
cout << "未找到!" << endl;
}
else
{
cout << "找到了" << endl;
}
}
binary_search只能查找某一個(gè)元素是否存在婴栽,不能返回元素的迭代器满粗。只能在有序序列中使用,如果序列無(wú)序愚争,則查找
結(jié)果未必準(zhǔn)確映皆。
count
count(iterator begin(), iterator end(), value);
統(tǒng)計(jì)元素value出現(xiàn)的個(gè)數(shù),返回一個(gè)整型數(shù)字
- 內(nèi)置數(shù)據(jù)類型
//查找內(nèi)置數(shù)據(jù)類型
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(10);
v.push_back(10);
int ret = count(v.begin(), v.end(), 10);
cout << "元素10共有" << ret << "個(gè)";
}
- 自定義數(shù)據(jù)類型
class Person
{
public:
Person(string name, int age)
{
this->age = age;
this->name = name;
}
//重載==號(hào)
bool operator==(const Person& p)
{
if (this->age == p.age)
{
return true;
}
else
{
return false;
}
}
string name;
int age;
};
//查找自定義數(shù)據(jù)類型
void test02()
{
vector<Person> vp;
vp.push_back(Person("aaa", 20));
vp.push_back(Person("bbb", 20));
vp.push_back(Person("ccc", 36));
vp.push_back(Person("ddd", 29));
vp.push_back(Person("eee", 20));
int ret = count(vp.begin(), vp.end(), Person("fff", 20));
cout << "與fff同歲的有" << ret << "個(gè)" << endl; //3個(gè)
}
對(duì)于自定義數(shù)據(jù)類型轰枝,需要重載==捅彻。由于我們的只要求年齡相同,所以重載的函數(shù)體只寫了this->age == p.age
鞍陨,如果要求年齡也相等步淹,則需要加上條件this->name == p.name
。
count_if
count_if(iterator begin,iterator end,_Pred);
_Pred 謂詞
按條件統(tǒng)計(jì)
- 內(nèi)置數(shù)據(jù)類型
class GreaterThan20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
//查找內(nèi)置數(shù)據(jù)類型
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(40);
v.push_back(50);
v.push_back(10);
int ret = count_if(v.begin(), v.end(), GreaterThan20());
cout << "大于20的元素共有" << ret << "個(gè)"; //4個(gè)
}
- 自定義數(shù)據(jù)類型
class Person
{
public:
Person(string name, int age)
{
this->age = age;
this->name = name;
}
string name;
int age;
};
class Compare
{
public:
bool operator()(const Person &p)
{
return p.age > 20;
}
};
//查找自定義數(shù)據(jù)類型
void test02()
{
vector<Person> vp;
vp.push_back(Person("aaa", 20));
vp.push_back(Person("bbb", 20));
vp.push_back(Person("ccc", 36));
vp.push_back(Person("ddd", 29));
vp.push_back(Person("eee", 20));
int ret = count_if(vp.begin(), vp.end(), Compare());
cout << "年齡大于20的人有" << ret << "個(gè)" << endl; //2個(gè)
}