Algoritmy “remove”
C++ 11

image_printTisk

Úvod

Algoritmus remove

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

int main() {
    string s = "Hello, world !!!!";
    cout << s << endl;
    remove(s.begin(), s.end(), 'l');
    cout << s << endl;
}
Unable to connect to the JDoodle service.

Algoritmus remove_if

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

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

int main() {
    vector<int> data;
    auto generator = []() {
        static int val = 0;
        return ++val;
    };
    generate_n(back_inserter(data), 20, generator);
    
    print(data);
    remove_if(data.begin(), data.end(), [](int item) { return item % 2 == 0; });
    print(data);
    return 0;
}
Unable to connect to the JDoodle service.

Algoritmus remove_copy

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

int main() {
    string s = "Hello, world !!!!";
    string s2;
    remove_copy(s.begin(), s.end(), back_inserter(s2), 'l');
    cout << s << endl;
    cout << s2 << endl;
}
Unable to connect to the JDoodle service.

Algoritmus remove_copy_if

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

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

int main() {
    vector<int> data;
    auto generator = []() {
        static int val = 0;
        return ++val;
    };
    generate_n(back_inserter(data), 20, generator);
    
    vector<int> data2;
    remove_copy_if(data.begin(), data.end(), back_inserter(data2), 
        [](int item) { return item % 2 == 0; });
    print(data);
    print(data2);
    return 0;
}
Unable to connect to the JDoodle service.

Shrnutí

 

image_printTisk
Algoritmy “remove”
C++ 11
Ohodnoťte tento článek

Související články