CPPPROGLIB Telegram 6088
🔍 Какой алгоритм поиска выбрать?

Выбор неправильного алгоритма поиска может в разы замедлить программу.


✏️ Выбираем по ситуации:

1️⃣ Неупорядоченные данные → std::find (O(n)):

std::vector<int> nums = {3, 1, 4, 1, 5};
auto it = std::find(nums.begin(), nums.end(), 4);
if (it != nums.end()) {
std::cout << "Found at position " << std::distance(nums.begin(), it);
}



2️⃣ Упорядоченные данные → std::binary_search (O(log n)):

std::vector<int> sorted_nums = {1, 2, 3, 4, 5};
if (std::binary_search(sorted_nums.begin(), sorted_nums.end(), 3)) {
std::cout << "Found!";
}



3️⃣ Частые поиски → std::unordered_set (O(1) average):

std::unordered_set<int> lookup = {1, 3, 5, 7, 9};
if (lookup.find(5) != lookup.end()) {
std::cout << "Found instantly!";
}



4️⃣ Поиск с предикатом → std::find_if:

auto even = std::find_if(nums.begin(), nums.end(), 
[](int n) { return n % 2 == 0; });



Частая ошибка: Использование find на отсортированных данных.


Библиотека C/C++ разработчика

#буст
Please open Telegram to view this post
VIEW IN TELEGRAM
🔥10👍1



tgoop.com/cppproglib/6088
Create:
Last Update:

🔍 Какой алгоритм поиска выбрать?

Выбор неправильного алгоритма поиска может в разы замедлить программу.


✏️ Выбираем по ситуации:

1️⃣ Неупорядоченные данные → std::find (O(n)):

std::vector<int> nums = {3, 1, 4, 1, 5};
auto it = std::find(nums.begin(), nums.end(), 4);
if (it != nums.end()) {
std::cout << "Found at position " << std::distance(nums.begin(), it);
}



2️⃣ Упорядоченные данные → std::binary_search (O(log n)):

std::vector<int> sorted_nums = {1, 2, 3, 4, 5};
if (std::binary_search(sorted_nums.begin(), sorted_nums.end(), 3)) {
std::cout << "Found!";
}



3️⃣ Частые поиски → std::unordered_set (O(1) average):

std::unordered_set<int> lookup = {1, 3, 5, 7, 9};
if (lookup.find(5) != lookup.end()) {
std::cout << "Found instantly!";
}



4️⃣ Поиск с предикатом → std::find_if:

auto even = std::find_if(nums.begin(), nums.end(), 
[](int n) { return n % 2 == 0; });



Частая ошибка: Использование find на отсортированных данных.


Библиотека C/C++ разработчика

#буст

BY Библиотека C/C++ разработчика | cpp, boost, qt


Share with your friend now:
tgoop.com/cppproglib/6088

View MORE
Open in Telegram


Telegram News

Date: |

Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators. Deputy District Judge Peter Hui sentenced computer technician Ng Man-ho on Thursday, a month after the 27-year-old, who ran a Telegram group called SUCK Channel, was found guilty of seven charges of conspiring to incite others to commit illegal acts during the 2019 extradition bill protests and subsequent months. How to create a business channel on Telegram? (Tutorial) Image: Telegram. It’s yet another bloodbath on Satoshi Street. As of press time, Bitcoin (BTC) and the broader cryptocurrency market have corrected another 10 percent amid a massive sell-off. Ethereum (EHT) is down a staggering 15 percent moving close to $1,000, down more than 42 percent on the weekly chart.
from us


Telegram Библиотека C/C++ разработчика | cpp, boost, qt
FROM American