Algoritmy “search” a “search_n”
C++ 11

image_printTisk

Úvod

Algoritmus search

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string s1 = "Hello, world !!!!!";
    string s2 = "wor";
    auto it = search(s1.begin(), s1.end(), s2.begin(), s2.end());
    if(it == s1.end()) {
        cout << "Nenalezeno" << endl;
    } else {
        cout << "Podřetězec začíná na indexu: " << distance(s1.begin(), it) << endl;
    }
    return 0;
}
Unable to connect to the JDoodle service.

Algoritmus search_n

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template<typename Iterator>
void print_n(Iterator it, int n) {
    while(n-- > 0) {
        cout << *it << ";";
        it++;
    }
    cout << endl;
}

vector<int> data = {1, 2, 7, 4, 6, 10, 4, 3, 1 };

int main() {
    // Najdi podposloupnost 4 cisel delitelnych dvema
    auto it = search_n(data.begin(), data.end(), 4, 2, [](int val, int divisibleBy){
        return val % divisibleBy == 0; 
    });
    print_n(it, 4);
    return 0;
    
}
Unable to connect to the JDoodle service.

Shrnutí

 

image_printTisk
Algoritmy “search” a “search_n”
C++ 11
Ohodnoťte tento článek

Související články