.


:




:

































 

 

 

 


(set)




. . , , , . unsorted_set. , stl , .

template < class T, // set::key_type/value_type

class Compare = less<T>, // set::key_compare/value_compare

class Alloc = allocator<T> // set::allocator_type

> class set;

, - , .

set?

Set , , , . , vector. , (std::sort(vector.begin(), vector.end())).

 

#include "stdafx.h"

#include <set>

#include <iostream>

#include <algorithm>

#include <iterator>

#include <string>

 

using namespace std;

 

template <typename TData>

void Print(const set<TData>& set, const char* delimiter)

{

cout << "Set content: ";

 

copy(

set.begin(),

set.end(),

ostream_iterator<TData>(std::cout, delimiter));

cout << endl;

}

 

void PrintSet(const set<int>& set)

{

// first way.

cout << "Set content: ";

 

for (auto setIterator = set.begin(); setIterator!= set.end(); ++setIterator)

cout << *setIterator << " ";

cout << endl;

 

// second way.

cout << "Set content: ";

 

copy(

set.begin(),

set.end(),

ostream_iterator<int>(std::cout, " "));

cout << endl;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

// unique elements.

int elements[] = { 1, 1, 13 };

set<int> elementsSet(elements, elements + 3); // only unuqie elements were added.

PrintSet(elementsSet);

 

// search

set<string> namesSet = { "Test", "Abba" };

Print(namesSet, "\n");

if (namesSet.find("Test")!= namesSet.end())

cout << "Test is in set\n";

 

// add, remove elements

namesSet.erase("Test");

namesSet.insert("We need a hero");

Print(namesSet, ", ");

 

getchar();

 

return 0;

}

Multiset

, set, . .





:


: 2015-05-05; !; : 544 |


:

:

, .
==> ...

1550 - | 1360 -


© 2015-2024 lektsii.org - -

: 0.01 .