柚子快報(bào)激活碼778899分享:c++---find()函數(shù)
目錄
1. string類的find函數(shù)2. < algorithm >下的 find 函數(shù)3. C++ find_if()函數(shù)4. C++ find_if_not()函數(shù)5. map和vector的find
1. string類的find函數(shù)
string 類的 find() 函數(shù)用于在字符串中查找子串或字符。它有多種重載形式,可以用于查找不同類型的子串或字符。 下面是 find() 函數(shù)的幾種常用形式:
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;
其中,str為要查找的子串;s為要查找的字符數(shù)組;c為要查找的字符;pos為查找的起始位置;n為要查找的字符個(gè)數(shù)。 如果查找成功,find()函數(shù)返回子串或字符在字符串中第一次出現(xiàn)的位置;否則,返回一個(gè)特殊值string::npos,表示查找失敗。 下面這段代碼演示了如何使用find()函數(shù)在字符串中查找子串或字符:
#include
#include
int main()
{
std::string str = "Hello, world!";
std::cout << str.find("world") << '\n'; // 輸出7
std::cout << str.find('w') << '\n'; // 輸出7
std::cout << str.find("abc") << '\n'; // 輸出18446744073709551615(即string::npos)
}
//上面的代碼會(huì)在控制臺(tái)輸出 “ 7 7 18446744073709551615 ”。
注意事項(xiàng) 在使用string類的find()函數(shù)時(shí),有幾點(diǎn)需要注意:
find()函數(shù)返回的是一個(gè)無(wú)符號(hào)整數(shù)(size_t類型),如果查找失敗,它會(huì)返回一個(gè)特殊值string::npos。由于string::npos的值為-1,因此如果你直接輸出find()函數(shù)的返回值,可能會(huì)看到一個(gè)很大的數(shù)字(無(wú)符號(hào)整數(shù)的最大值)。如果你想讓程序輸出-1,可以使用強(qiáng)制類型轉(zhuǎn)換將返回值轉(zhuǎn)換為有符號(hào)整數(shù)(如int類型)。find()函數(shù)只能查找子串或字符在字符串中第一次出現(xiàn)的位置。如果你想查找子串或字符在字符串中所有出現(xiàn)的位置,可以使用循環(huán)結(jié)構(gòu)和find()函數(shù)的第二個(gè)參數(shù)(查找起始位置)實(shí)現(xiàn)。find()函數(shù)是區(qū)分大小寫的。如果你想忽略大小寫進(jìn)行查找,可以先將字符串和子串都轉(zhuǎn)換為小寫或大寫,然后再使用find()函數(shù)進(jìn)行查找。
find_last_of() & find_first_of
#include
#include
int main() {
std::string str = "Hello, world!";
std::string chars = "o!";
// 使用 find_last_of 查找最后一個(gè)匹配 chars 中任意一個(gè)字符的位置
size_t pos = str.find_last_of(chars);
if (pos != std::string::npos) {
std::cout << "last position: " << pos << std::endl;
} else {
std::cout << "no result." << std::endl;
}
// 使用 std::find_first_of 查找第一個(gè)匹配 chars 中任意一個(gè)字符的位置
auto result = std::find_first_of(str.begin(), str.end(), chars.begin(), chars.end());
if (result != str.end()) {
std::cout << "first position: " << std::distance(str.begin(), result) << std::endl;
} else {
std::cout << "no result." << std::endl;
}
return 0;
}
通過(guò)檢查pos是否等于std::string::npos來(lái)判斷是否找到了匹配的字符。如果找到了,輸出匹配字符的位置,否則輸出沒(méi)有找到匹配的字符。
2. < algorithm >下的 find 函數(shù)
find() 函數(shù)本質(zhì)上是一個(gè)模板函數(shù),用于在指定范圍內(nèi)查找和目標(biāo)元素值相等的第一個(gè)元素。 如下為 find() 函數(shù)的語(yǔ)法格式:
InputIterator find (InputIterator first, InputIterator last, const T& val);
其中,first 和 last 為輸入迭代器,[first, last) 用于指定該函數(shù)的查找范圍;val 為要查找的目標(biāo)元素。 正因?yàn)?first 和 last 的類型為輸入迭代器,因此該函數(shù)適用于所有的序列式容器。 另外,該函數(shù)會(huì)返回一個(gè)輸入迭代器,當(dāng) find() 函數(shù)查找成功時(shí),其指向的是在 [first, last) 區(qū)域內(nèi)查找到的第一個(gè)目標(biāo)元素;如果查找失敗,則該迭代器的指向和 last 相同。 值得一提的是,find() 函數(shù)的底層實(shí)現(xiàn),其實(shí)就是用 == 運(yùn)算符將 val 和 [first, last) 區(qū)域內(nèi)的元素逐個(gè)進(jìn)行比對(duì)。這也就意味著,[first, last) 區(qū)域內(nèi)的元素必須支持 == 運(yùn)算符。
舉個(gè)例子:
#include
#include
#include
int main() {
std::vector
auto it = find(myvector.begin(), myvector.end(), "com");
if (it != myvector.end())
std::cout << "result: " << *it << std::endl;
else
std::cout << "no result." << std::endl;
return 0;
}
3. C++ find_if()函數(shù)
和 find() 函數(shù)相同,find_if() 函數(shù)也用于在指定區(qū)域內(nèi)執(zhí)行查找操作。不同的是,前者需要明確指定要查找的元素的值,而后者則允許自定義查找規(guī)則。 所謂自定義查找規(guī)則,實(shí)際上指的是有一個(gè)形參且返回值類型為 bool 的函數(shù)。值得一提的是,該函數(shù)可以是一個(gè)普通函數(shù)(又稱為一元謂詞函數(shù)),比如:
bool mycomp(int i) {
return ((i % 2) == 1);
}
確切地說(shuō),find_if() 函數(shù)會(huì)根據(jù)指定的查找規(guī)則,在指定區(qū)域內(nèi)查找第一個(gè)符合該函數(shù)要求(使函數(shù)返回 true)的元素。 find_if() 函數(shù)的語(yǔ)法格式如下:
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
其中,first 和 last 都為輸入迭代器,其組合 [first, last) 用于指定要查找的區(qū)域;pred 用于自定義查找規(guī)則。 值得一提的是,由于 first 和 last 都為輸入迭代器,意味著該函數(shù)適用于所有的序列式容器。甚至當(dāng)采用適當(dāng)?shù)闹^詞函數(shù)時(shí),該函數(shù)還適用于所有的關(guān)聯(lián)式容器(包括哈希容器)。 同時(shí),該函數(shù)會(huì)返回一個(gè)輸入迭代器,當(dāng)查找成功時(shí),該迭代器指向的是第一個(gè)符合查找規(guī)則的元素;反之,如果 find_if() 函數(shù)查找失敗,則該迭代器的指向和 last 迭代器相同。
舉個(gè)例子:
#include
#include
#include
//自定義一元謂詞函數(shù)
bool mycomp(int i) {
return ((i % 2) == 1);
}
//以函數(shù)對(duì)象的形式定義一個(gè) find_if() 函數(shù)的查找規(guī)則
class mycomp2 {
public:
bool operator()(const int& i) {
return ((i % 2) == 1);
}
};
int main() {
std::vector
auto it = find_if(myvector.begin(), myvector.end(), mycomp2());
std::cout << "*it = " << *it;
return 0;
}
程序執(zhí)行結(jié)果為:
*it = 3
std::find_if 是 C++ 標(biāo)準(zhǔn)庫(kù)中的一個(gè)算法函數(shù),用于在指定范圍內(nèi)查找滿足特定條件的元素,并返回第一個(gè)滿足條件的元素的迭代器。它的聲明位于 < algorithm > 頭文件中。 函數(shù)原型如下:
template< class InputIt, class UnaryPredicate >
InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
參數(shù)解釋:
first 和 last 定義了查找范圍,表示要在 [first, last) 區(qū)間內(nèi)查找。p 是一個(gè)一元謂詞(Unary Predicate),是一個(gè)可調(diào)用對(duì)象(函數(shù)、函數(shù)指針、函數(shù)對(duì)象等),接受一個(gè)參數(shù)并返回 bool 類型的值。查找過(guò)程中,對(duì)于每個(gè)元素,p 會(huì)被調(diào)用一次,如果返回 true,則該元素滿足條件,查找成功。 返回值: 如果找到滿足條件的元素,則返回指向該元素的迭代器。 如果未找到滿足條件的元素,則返回 last。
#include
#include
#include
int main() {
std::vector
// 查找第一個(gè)大于 5 的元素
auto it = std::find_if(numbers.begin(), numbers.end(), [](int x) {
return x > 5;
});
if (it != numbers.end()) {
std::cout << "Found element greater than 5: " << *it << std::endl;
} else {
std::cout << "No element greater than 5 found." << std::endl;
}
return 0;
}
結(jié)合程序執(zhí)行結(jié)果不難看出,對(duì)于 myvector 容器中的元素 4 和 2 來(lái)說(shuō),它們都無(wú)法使 (i%2)==1 這個(gè)表達(dá)式成立,因此 mycomp2() 返回 false;而對(duì)于元素 3 來(lái)說(shuō),它可以使 mycomp2() 函數(shù)返回 true,因此,find_if() 函數(shù)找到的第一個(gè)元素就是元素 3。 值得一提的是,C++ STL find_if()官網(wǎng)給出了 find_if() 函數(shù)底層實(shí)現(xiàn)的參考代碼(如下所示):
template
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred){
while (first!=last) {
if (pred(*first)) return first;
++first;
}
return last;
}
4. C++ find_if_not()函數(shù)
find_if_not() 函數(shù)和 find_if() 函數(shù)的功能恰好相反,通過(guò)上面的學(xué)習(xí)我們知道,find_if() 函數(shù)用于查找符合謂詞函數(shù)規(guī)則的第一個(gè)元素,而 find_if_not() 函數(shù)則用于查找第一個(gè)不符合謂詞函數(shù)規(guī)則的元素。
find_if_not() 函數(shù)的語(yǔ)法規(guī)則如下所示:
InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred);
`其中,first 和 last 都為輸入迭代器,[first, last) 用于指定查找范圍;pred 用于自定義查找規(guī)則。 和 find_if() 函數(shù)一樣,find_if_not() 函數(shù)也適用于所有的容器,包括所有序列式容器和關(guān)聯(lián)式容器。 同樣,該函數(shù)也會(huì)返回一個(gè)輸入迭代器,當(dāng) find_if_not() 函數(shù)查找成功時(shí),該迭代器指向的是查找到的那個(gè)元素;反之,如果查找失敗,該迭代器的指向和 last 迭代器相同。 舉個(gè)例子:
#include
#include
#include
//自定義一元謂詞函數(shù)
bool mycomp(int i) {
return ((i % 2) == 1);
}
int main() {
std::vector
//調(diào)用 find_if() 函數(shù),并以 mycomp() 一元謂詞函數(shù)作為查找規(guī)則
auto it = find_if_not(myvector.begin(), myvector.end(), mycomp);
std::cout << "*it = " << *it;
return 0;
}
程序執(zhí)行結(jié)果為:
*it = 4
可以看到,由于第一個(gè)元素 4 就不符合 (i%2)==1,因此 find_if_not() 成功找到符合條件的元素,并返回一個(gè)指向該元素的迭代器。 find_if_not() 函數(shù)的底層實(shí)現(xiàn)和 find_if() 函數(shù)非常類似,C++ STL find_if_not()官網(wǎng)給出了該函數(shù)底層實(shí)現(xiàn)的參考代碼:
template
InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred){
while (first!=last) {
if (!pred(*first)) return first;
++first;
}
return last;
}
5. map和vector的find
std::map 和 std::vector 是 C++ 中常用的容器,它們都提供了 find() 函數(shù)用于查找元素。 5.1 map 的 find() 函數(shù) std::map 是一個(gè)有序鍵值對(duì)的容器,find() 函數(shù)用于在 std::map 中根據(jù)鍵查找對(duì)應(yīng)的值。find() 函數(shù)返回一個(gè)指向匹配元素的迭代器,如果找不到匹配的元素,則返回指向 std::map 末尾的迭代器 end()。 用法示例:
#include
#include
int main() {
std::map
myMap[1] = "apple";
myMap[2] = "banana";
myMap[3] = "orange";
// 查找鍵為2的元素
auto it = myMap.find(2);
if (it != myMap.end()) {
std::cout << "Found: " << it->second << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}
輸出結(jié)果:
Found: banana
5.2 vector的find()函數(shù) std::vector 是一個(gè)動(dòng)態(tài)數(shù)組的容器,find() 函數(shù)用于在 std::vector 中查找指定元素。find() 函數(shù)返回一個(gè)指向匹配元素的迭代器,如果找不到匹配的元素,則返回指向 std::vector 末尾的迭代器 end()。 用法示例:
#include
#include
#include
int main() {
std::vector
// 查找值為3的元素
auto it = std::find(myVector.begin(), myVector.end(), 3);
if (it != myVector.end()) {
std::cout << "Found: " << *it << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}
輸出結(jié)果:
Found: 3
std::map 和 std::vector 中的 find() 函數(shù)原理:
std::map 的底層是通過(guò)紅黑樹(shù)實(shí)現(xiàn)的,查找元素的時(shí)間復(fù)雜度為 O(log N),其中 N 是 std::map 中元素的個(gè)數(shù)。std::vector 的底層是一個(gè)連續(xù)的內(nèi)存區(qū)域,查找元素需要遍歷整個(gè)容器,時(shí)間復(fù)雜度為 O(N),其中 N 是 std::vector 中元素的個(gè)數(shù)。
通過(guò) find() 函數(shù),可以方便地在 std::map 或 std::vector 容器中查找所需的元素,并對(duì)其進(jìn)行相應(yīng)的操作。 如有疏漏,還請(qǐng)海涵。
年年歲歲花相似,歲歲年年人不同。 只愿君心似我心,定不負(fù)相思意。 2024年3月20日21:27:23
柚子快報(bào)激活碼778899分享:c++---find()函數(shù)
參考閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。