Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thinking In C++, 2nd Edition, Volume 2 Standard Libraries& Advanced Topics - Eckel B..pdf
Скачиваний:
314
Добавлен:
24.05.2014
Размер:
2.09 Mб
Скачать

will contain abs(n-m) copies of that value, where abs( ) is the absolute value. The return value is the end of the output range

Example

It’s easiest to see the set operations demonstrated using simple vectors of characters, so you view the sets more easily. These characters are randomly generated and then sorted, but the duplicates are not removed so you can see what the set operations do when duplicates are involved.

//: C05:SetOperations.cpp

// Set operations on sorted ranges #include <vector>

#include <algorithm> #include "PrintSequence.h" #include "Generators.h" using namespace std;

int main() {

vector<char> v(50), v2(50); CharGen g;

generate(v.begin(), v.end(), g); generate(v2.begin(), v2.end(), g); sort(v.begin(), v.end()); sort(v2.begin(), v2.end()); print(v, "v", "");

print(v2, "v2", "");

bool b = includes(v.begin(), v.end(), v.begin() + v.size()/2, v.end());

cout << "includes: " <<

(b ? "true" : "false") << endl; vector<char> v3, v4, v5, v6; set_union(v.begin(), v.end(),

v2.begin(), v2.end(), back_inserter(v3)); print(v3, "set_union", ""); set_intersection(v.begin(), v.end(),

v2.begin(), v2.end(), back_inserter(v4)); print(v4, "set_intersection", ""); set_difference(v.begin(), v.end(),

v2.begin(), v2.end(), back_inserter(v5)); print(v5, "set_difference", ""); set_symmetric_difference(v.begin(), v.end(),

v2.begin(), v2.end(), back_inserter(v6)); print(v6, "set_symmetric_difference","");

Chapter 15: Multiple Inheritance

321

Соседние файлы в предмете Программирование