Arama ve filtreleme
- C++ kursu
- Orta seviye
- Ders 68/116
- 6 dk
STL algoritmalarıyla arama ve filtreleme yazarsın.
STL ile arama
Kütüphanede başlığa göre arama için std::find_if algoritmasını kullanırız. Bu algoritma bir koşula uyan ilk öğeyi bulur. Koşul, bir lambda ifadesiyle verilir: [&t](const Book& b) { return b.getTitle() == t; }.
Lambda, küçük bir adsız fonksiyondur. [&t] dışarıdaki t değişkenini referans ile yakalar. (const Book& b) parametre, { return b.getTitle() == t; } gövdedir. Lambda ifadelerini bir sonraki grupta ayrıntılı göreceksin; şimdilik bu kullanımı yeterli. std::find_if bir iterator döndürür; books.end()'e eşitse öğe bulunamamış demektir.
searchByTitle metodu
searchByTitle(const std::string& t) metodu std::find_if ile başlığa uyan kitabı arar. Dönen iterator books.end()'e eşitse false, değilse true döndürür. Metot const işaretlidir çünkü sınıfı değiştirmez.
Önceki dersin Library sınıfına (checkout içeren) bu metodu ekleyeceğiz. #include <algorithm> başlığını eklemeyi unutma; std::find_if bu başlıkta tanımlıdır. Böylece kütüphane hem ödünç verme hem de arama destekler; bir sonraki ders bu ikisini menüde birleştireceğiz.
Uygulama searchByTitle metodunu ekle
Önceki dersin sınıflarını al ve #include <algorithm> başlığını ekle. Library'ye bool searchByTitle(const std::string& t) const metodu ekle: std::find_if ile books.begin() ve books.end() aralığında [&t](const Book& b) { return b.getTitle() == t; } lambda'sıyla ara; iterator books.end()'e eşit değilse true döndür. main içinde "İnce Memed" ara ve bulunursa "bulundu" yazdır.
Başlangıç kodu
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Book {
private:
std::string title;
std::string author;
public:
Book(std::string t, std::string a) : title(t), author(a) {}
std::string getTitle() const { return title; }
std::string getAuthor() const { return author; }
};
class Library {
private:
std::vector<Book> books;
std::vector<bool> borrowed;
public:
void addBook(const Book& b) { books.push_back(b); borrowed.push_back(false); }
size_t count() const { return books.size(); }
Book getBook(int idx) const { return books[idx]; }
bool checkout(int idx) {
if (idx < 0 || idx >= (int)books.size()) return false;
if (borrowed[idx]) return false;
borrowed[idx] = true;
return true;
}
};
int main() {
Library lib;
lib.addBook(Book("Tutunamayanlar", "Oğuz Atay"));
lib.addBook(Book("İnce Memed", "Yaşar Kemal"));
// "İnce Memed" ara ve sonucu yazdır
return 0;
}Örnek çözüm
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Book {
private:
std::string title;
std::string author;
public:
Book(std::string t, std::string a) : title(t), author(a) {}
std::string getTitle() const { return title; }
std::string getAuthor() const { return author; }
};
class Library {
private:
std::vector<Book> books;
std::vector<bool> borrowed;
public:
void addBook(const Book& b) { books.push_back(b); borrowed.push_back(false); }
size_t count() const { return books.size(); }
Book getBook(int idx) const { return books[idx]; }
bool checkout(int idx) {
if (idx < 0 || idx >= (int)books.size()) return false;
if (borrowed[idx]) return false;
borrowed[idx] = true;
return true;
}
bool searchByTitle(const std::string& t) const {
auto it = std::find_if(books.begin(), books.end(),
[&t](const Book& b) { return b.getTitle() == t; });
return it != books.end();
}
};
int main() {
Library lib;
lib.addBook(Book("Tutunamayanlar", "Oğuz Atay"));
lib.addBook(Book("İnce Memed", "Yaşar Kemal"));
if (lib.searchByTitle("İnce Memed")) {
std::cout << "bulundu";
}
return 0;
}Beklenen çıktı
bulundu
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 Hangi STL algoritması koşula göre arar?
// koşula göre ilk eşleşmeyi bulur- std::sort
- std::find_if Doğru cevap
- std::count
- std::transform
Açıklama: std::find_if bir koşula uyan ilk öğeyi bulur.
2 find_if çağrısını tamamla
auto it = std::____(books.begin(), books.end(), [&t](const Book& b) { return b.getTitle() == t; });
auto it = std::find_if(books.begin(), books.end(), [&t](const Book& b) { return b.getTitle() == t; });
Açıklama: std::find_if koşula uyan ilk öğeyi bulur.
3 Parçayı rolüne eşle
| Kavram | Anlamı |
|---|---|
std::find_if |
Koşula göre arar |
books.begin() |
Aralık başı |
books.end() |
Aralık sonu |
[&t](const Book& b) |
Lambda yakalama |