Algoritmy “equal” a “mismatch”
C++ 11

image_printTisk

Úvod

Algoritmus equal

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

int main() {
    vector<int> input;
    fill_n(back_inserter(input), 10, 4);
    
    vector<int> output1, output2;
    copy(input.begin(), input.end(), back_inserter(output1));
    copy(input.begin(), input.end(), back_inserter(output2));
    output2[4] = 8;
    
    cout << boolalpha;
    cout << "Shoda output1: " << equal(input.begin(), input.end(), output1.begin()) << endl;
    cout << "Shoda output2: " << equal(input.begin(), input.end(), output2.begin()) << endl;
 
    return 0;   
}


Unable to connect to the JDoodle service.

Algoritmus mismatch

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

int main() {
    // Generuje čísla 0..9
    vector<int> data1;
    int value = 0;
    generate_n(back_inserter(data1), 10, [&value](){ return value++; });
    
    // Okopirujeme vektor
    vector<int> data2;
    copy(data1.begin(), data1.end(), back_inserter(data2));
    data2[7] = 100;
    
    // Mismatch
    auto p = mismatch(data1.begin(), data1.end(), data2.begin());
    
    cout << "Hodnota rozdilu v data1: " << *(p.first) << endl;
    cout << "Hodnota rozdilu v data2: " << *(p.second) << endl;
    cout << "Index rozdilu: " << distance(data1.begin(), p.first) << endl;
    return 0;
}
Unable to connect to the JDoodle service.

Shrnutí

 

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

Související články