Jump to content

Set (C++): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Line 39: Line 39:
===rbegin===
===rbegin===
function will return a reverse iterator to the end of current set. It is constant time function.
function will return a reverse iterator to the end of current set. It is constant time function.
syntax
* syntax
<Source lang="c">
<Source lang="c">
#include<set>
#include<set>
Line 52: Line 52:
==External links==
==External links==
* [http://en.cppreference.com/enwiki/w/cpp/container/set C++ reference for <code>std::set</code>]
* [http://en.cppreference.com/enwiki/w/cpp/container/set C++ reference for <code>std::set</code>]



[[Category:C++ Standard Library]]
[[Category:C++ Standard Library]]

Revision as of 18:20, 14 September 2011

A set is an associative container data structure that is available as part of the C++ Standard Library (STL), and contains a sorted set of unique objects.[1]

Although the abstract concept of a set does not necessarily imply an ordered collection, the standard library set data structure is always ordered. Its functionality in the STL is provided as a template class, such that any valid C++ object can be used with it. Sets are guaranteed to perform operations of insertion, deletion, and testing whether an element is in it, in logarithmic time - O(log n). As such, they are typically implemented using self-balancing binary search trees and support's bidirectional iterator

Types

There are 2 types of sets available in the C++ STL: Set and Multiset.

Set

In a set, every element is unique, and insertions of values that are already present in the container are ignored.

Multiset

In a multiset, multiple occurrences of the same value are allowed.

Characteristics

  • Unique element value: In a set, no two values are same.
  • Element value is itself a key.
  • Elements follow strict weak ordering at all times.

Function's in Set

In a set container various functions are used. Some of them are as follows:

begin

Function begin() is used to return an iterator to the first element.It is constant time function.

  • Syntax
#include<set>
iterator begin();
const_iterator begin const;

end

The function end() will return an iterator just before the end of the set. It is constant time function.

  • Syntax
#include<set>
iterator end();
const_iterator end() const;

rbegin

function will return a reverse iterator to the end of current set. It is constant time function.

  • syntax
#include<set>
reverse iterator rbegin();
const_reverse_iterator rbegin() const;


References