◐ Shell
clean mode source ↗

std::set::set – cppreference.com

Z cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>

(1)

explicit set( const Compare& comp = Compare(), {{#pad:|12}} const Allocator& alloc = Allocator() );

explicit set( const Allocator& alloc );

(1) (od C++11)
(2)

template< class InputIterator > set( InputIterator first, InputIterator last, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() );

set( const set& other );

(3)

set( const set& other, const Allocator& alloc );

(3) (od C++11)

set( set&& other );

(4) (od C++11)

set( set&& other, const Allocator& alloc );

(4) (od C++11)
(5)

set( std::initializer_list<value_type> init, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() );

(od C++11)

Konstruuje nowy kontener z różnych źródeł danych, opcjonalnie wykorzystując dostarczony przez użytkownika alokator alloc i/lub obiekt funkcji porównującej comp.

2) Konstruktor przedziałowy. Konstruuje kontener z zawartością przedziału

[first, last)

. Jeśli wiele elementów na przedziale ma klucze, które są porównywane jako identyczne, nie jest określone, który element zostanie wstawiony (pending LWG2844).

3) Konstruktor kopiujący. Konstruuje kontener z kopią zawartości

other

. If

alloc

is not provided, allocator is obtained by calling std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()).

4) Konstruktor przenoszący. Constructs the container with the contents of

other

using move semantics. If

alloc

is not provided, allocator is obtained by move-construction from the allocator belonging to

other

.

5) Konstruktor z listy inicjalizacyjnej. Konstruuje kontener z zawartością listy inicjalizacyjnej

init

. Jeśli wiele elementów na przedziale ma klucze, które są porównywane jako identyczne, nie jest określone, który element zostanie wstawiony (pending LWG2844).

Parametry

alloc - alokator używany do wszystkich alokacji pamięci wykonywanych przez ten kontener
comp - obiekt funkcji porównującej, wykorzystywany przy wszystkich porównaniach kluczy
first, last - przedział, z którego zostają skopiowane elementy
other - inny kontener, wykorzystywany jako źródło, z którego inicjalizowane są elementy kontenera
init - lista inicjalizacyjna, do zainicjowania wartości elementów kontenera
Wymagania względem typów
-InputIterator musi spełniać wymagania InputIteratorerator.
-Compare musi spełniać wymagania Compare.
-Allocator musi spełniać wymagania Allocator.

Złożoność

1) Stała

2) N log(N), gdzie N = std::distance(first, last) ogólnie, liniowa względem N jeśli przedział jest już posortowany zgodnie z value_comp().

3) Liniowa względem rozmiaru other

4) Stała. Jeśli alloc jest podany i alloc != other.get_allocator(), liniowa.

5) N log(N), gdzie N = init.size()) ogólnie, liniowa względem N jeśli init jest już posortowana zgodnie z value_comp().

Wyjątki

Wywołania Allocator::allocate mogą wyrzucić wyjątki.

Notka

Po skonstruowaniu kontenera przez przeniesienie (przeciążenie (4)), referencje, wskaźniki i iteratory (inne niż "past-the-end") do other pozostają prawidłowe, ale wskazują na elementy znajdujące się teraz w *this. Obecny standard gwarantuje to przez oświadczenie zbiorcze w §23.2.1[container.requirements.general]/12, i bardziej bezpośrednia gwarancja jest wzięta pod uwagę: LWG 2321.

Przykład

#include <iostream>
#include <string>
#include <set>
#include <cmath>

struct Point { double x, y; };
struct PointCmp {
    bool operator()(const Point& lhs, const Point& rhs) const { 
        return std::hypot(lhs.x, lhs.y) < std::hypot(rhs.x, rhs.y); 
    }
};
 
int main()
{
  // (1) Domyślny konstruktor
  std::set<std::string> a;
  a.insert("cat");
  a.insert("dog");
  a.insert("horse");
  for(auto& str: a) std::cout << str << ' ';
  std::cout << '\n';
 
  // (2) Konstruktor przedziałowy
  std::set<std::string> b(a.find("dog"), a.end());
  for(auto& str: b) std::cout << str << ' ';
  std::cout << '\n';
 
  // (3) Konstruktor kopiujący
  std::set<std::string> c(a);
  c.insert("another horse");
  for(auto& str: c) std::cout << str << ' ';
  std::cout << '\n';
 
  // (4) Konstruktor przenoszący
  std::set<std::string> d(std::move(a));
  for(auto& str: d) std::cout << str << ' ';
  std::cout << '\n';
  std::cout << "moved-from set is ";
  for(auto& str: a) std::cout << str << ' ';
  std::cout << '\n';
 
  // (5) Konstruktor z listy inicjalizacyjnej
  std::set<std::string> e {"one", "two", "three", "five", "eight"};
  for(auto& str: e) std::cout << str << ' ';
  std::cout << '\n';
  
  // własna funkcja porównująca
  std::set<Point, PointCmp> z = {{2, 5}, {3, 4}, {1, 1}};
  z.insert({1, -1}); // nie powiedzie się, ponieważ moduł (1,-1) jest równy (1,1)
  for(auto& p: z) std::cout << '(' << p.x << ',' << p.y << ") ";
  std::cout << '\n';
}

Wynik:

cat dog horse 
dog horse 
another horse cat dog horse 
cat dog horse 
moved-from set is 
eight five one three two 
(1,1) (3,4) (2,5)

Zobacz także