⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remove.cpp

📁 C++ sample code, for the book: C++ black book, including templates, exceptional handling.
💻 CPP
字号:
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;

template<class T>
class greater_than_5 
{
public:
    bool operator() (const T& value) {return value > 5;}
};

const int NUMBER_ELEMENTS = 10;

int main ()
{
    int initial_values[NUMBER_ELEMENTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    vector<int> vector1(NUMBER_ELEMENTS);
    vector<int> vector2(NUMBER_ELEMENTS);
    vector1.assign(initial_values, initial_values + NUMBER_ELEMENTS);
    vector2.assign(initial_values, initial_values + NUMBER_ELEMENTS);

    cout << "Original vector: " << endl;
    copy(vector1.begin(), vector1.end(), ostream_iterator<int,char>(cout," "));
    cout << endl;

    cout << "Removing the 8: " << endl;
    vector<int,allocator<int> >::iterator result = remove(vector1.begin(), vector1.end(), 8);
    vector1.erase(result, vector1.end());

    copy(vector1.begin(), vector1.end(), ostream_iterator<int,char>(cout," "));
    cout << endl;

    cout << "Removing elements greater than 5: " << endl;
    result = remove_if(vector1.begin(), vector1.end(), greater_than_5<int>());
    vector1.erase(result, vector1.end());

    copy(vector1.begin(), vector1.end(), ostream_iterator<int,char>(cout," "));
    cout << endl;

    cout << "Using the copying version to remove the 8: " << endl;
    remove_copy(vector2.begin(), vector2.end(), ostream_iterator<int,char>(cout," "), 8);
    cout << endl;

    cout << "Using the copying version to remove elements greater than 5: " << endl;
    remove_copy_if(vector2.begin(), vector2.end(), ostream_iterator<int,char>(cout," "),
        greater_than_5<int>());
 
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -