Algoritmy “fill” a “generate”
C++ 11

image_printTisk

Úvod

Algoritmus fill

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

template<typename C>
void printValues(C c) {
    for(auto val : c)
        cout << val << ";";
    cout << endl;
}

int main() {
    vector<int> data(10);
    fill(data.begin(), data.end(), 3);
    printValues(data);
    return 0;
}
Unable to connect to the JDoodle service.

Algoritmus fill_n

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

template<typename C>
void printValues(C c) {
    for(auto val : c)
        cout << val << ";";
    cout << endl;
}

int main() {
    vector<int> data;
    fill_n(back_inserter(data), 10, 4);
    printValues(data);
    return 0;
}
Unable to connect to the JDoodle service.

Algoritmus generate

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

template<typename C>
void printValues(C c) {
    for(auto val : c)
        cout << val << ";";
    cout << endl;
}

int main() {
    vector<int> data(10);
    int value = 0;
    generate(data.begin(), data.end(), [value]() mutable { value++; return  value; });
    printValues(data);
    return 0;
}
Unable to connect to the JDoodle service.

Algoritmus generate_n

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

template<typename C>
void printValues(C c) {
    for(auto val : c)
        cout << val << ";";
    cout << endl;
}

int main() {
    vector<int> data;
    int value = 0;
    generate_n(back_inserter(data), 15, [value]() mutable { value++; return  value; });
    printValues(data);
    return 0;
}
Unable to connect to the JDoodle service.

Shrnutí

 

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

Související články