unordered_set in C++ STL

unordered_set is implemented using hash table where keys are hashed into indices of this hash table so it is not possible to maintain an order. All operation on unordered_set takes constant time O(1) on an average which can go up to linear time in worst case which depends on the internally used hash function but practically they perform very well and generally provide constant time lookup operation.  unordered_set 使用hash table实现,无法保持顺序,unordered_set 的操作的复杂度为 O(1),最坏取决于使用的哈希函数。
The unordered-set can contain key of any type – predefined or user-defined data structure but when we define key of type user define type, we need to specify our comparison function according to which keys will be compared.  

set vs unordered_set
Set set is an ordered sequence of unique keys whereas unordered_set is a set in which key can be stored in any order, so unordered.   set保存的是唯一有序的key,unordered_set 保存的是无序的key。
Set is implemented as balanced tree structure that is why it is possible to maintain an order between the elements (by specific tree traversal). Time complexity of set operations is O(Log n) while for unordered_set, it is O(1).  set由平衡树结构实现,是有序的,set的操作复杂度是O(Log n),而unordered_set是O(1)

Methods on unordered_set
For unordered_set many function are defined among which most useful are size and empty for capacity, find for searching a key, insert and erase for modification.
The Unordered_set allows only unique keys, for duplicate keys unordered_multiset should be used.

Example of declaration, find, insert and iteration in unordered_set is given below :

// C++ program to demonstrate various function of unordered_set 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// declaring set for storing string data-type 
	unordered_set<string> stringSet; 

	// inserting various string, same string will be stored 
	// once in set 
	stringSet.insert("code"); 
	stringSet.insert("in"); 
	stringSet.insert("c++"); 
	stringSet.insert("is"); 
	stringSet.insert("fast"); 

	string key = "slow"; 

	//	 find returns end iterator if key is not found, 
	// else it returns iterator to that key 
	if (stringSet.find(key) == stringSet.end()) 
		cout << key << " not found\n\n"; 
	else
		cout << "Found " << key << endl << endl; 

	key = "c++"; 
	if (stringSet.find(key) == stringSet.end()) 
		cout << key << " not found\n"; 
	else
		cout << "Found " << key << endl; 

	// now iterating over whole set and printing its 
	// content 
	cout << "\nAll elements : "; 
	unordered_set<string> :: iterator itr; 
	for (itr = stringSet.begin(); itr != stringSet.end(); itr++) 
		cout << (*itr) << endl; 
} 

Output :

slow not found

Found c++

All elements : 
is
fast
c++
in
code

Find, insert and erase take constant amount of time on average. The find() function returns an iterator to end() if key is not there in set otherwise iterator to key position is returned. The iterator works as pointer to key values so we can get key by dereferencing them by *.  操作Find, insert and erase的复杂度是常量的。

A practical problem based on unordered_set – given a set of integer number, find duplicate among them.

Input  : arr[] = {1, 5, 2, 1, 4, 3, 1, 7, 2, 8, 9, 5}
Output : Duplicate item are : 5 2 1 

Below is C++ solution using unordered_set.

// C++ program to find duplicate from an array using 
// unordered_set 
#include <bits/stdc++.h> 
using namespace std; 

// Print duplicates in arr[0..n-1] using unordered_set 
void printDuplicates(int arr[], int n) 
{ 
	// declaring unordered sets for checking and storing 
	// duplicates 
	unordered_set<int> intSet; 
	unordered_set<int> duplicate; 

	// looping through array elements 
	for (int i = 0; i < n; i++) 
	{ 
		// if element is not there then insert that 
		if (intSet.find(arr[i]) == intSet.end()) 
			intSet.insert(arr[i]); 

		// if element is already there then insert into 
		// duplicate set 
		else
			duplicate.insert(arr[i]); 
	} 

	// printing the result 
	cout << "Duplicate item are : "; 
	unordered_set<int> :: iterator itr; 

	// iterator itr loops from begin() till end() 
	for (itr = duplicate.begin(); itr != duplicate.end(); itr++) 
		cout << *itr << " "; 
} 

// Driver code 
int main() 
{ 
	int arr[] = {1, 5, 2, 1, 4, 3, 1, 7, 2, 8, 9, 5}; 
	int n = sizeof(arr) / sizeof(int); 

	printDuplicates(arr, n); 
	return 0; 
} 

Output :

Duplicate item are : 5 2 1 

Methods of unordered_set:

  • insert()– Insert a new {element} in the unordered_set container.
  • begin()– Return an iterator pointing to the first element in the unordered_set container.
  • end()– Returns an iterator pointing to the past-the-end-element.
  • count()– Count occurrences of a particular element in an unordered_set container.
  • find()– Search for an element in the container.
  • clear()– Removes all of the elements from an unordered_set and empties it.
  • cbegin()– Return a const_iterator pointing to the first element in the unordered_set container.
  • cend()– Return a const_iterator pointing to past-the-end element in the unordered_set container or in one of it’s bucket.
  • bucket_size()– Returns the total number of elements present in a specific bucket in an unordered_set container.
  • erase()– Remove either a single element of a range of elements ranging from start(inclusive) to end(exclusive).
  • size()– Return the number of elements in the unordered_set container.
  • swap()– Exchange values of two unordered_set containers.
  • emplace()– Insert an element in an unordered_set container.
  • max_size()– Returns maximum number of elements that an unordered_set container can hold.
  • empty()– Check if an unordered_set container is empty or not.
  • equal_range– Returns range that includes all elements equal to given value.
  • operator= – Copies (or moves) an unordered_set to another unordered_set and unordered_set::operator= is the corresponding operator function.
  • hash_function() – This hash function is a unary function which takes asingle argument only and returns a unique value of type size_t based on it.
  • reserve()– Used to request capacity change of unordered_set.
  • bucket()– Returns the bucket number of a specific element.
  • bucket_count() – Returns the total number of buckets present in an unordered_set container.
  • load_factor()– Returns the current load factor in the unordered_set container.
  • rehash()– Set the number of buckets in the container of unordered_set to given size or more.
  • max_load_factor()– Returns(Or sets) the current maximum load factor of the unordered set container.
  • emplace_hint()– Inserts a new element in the unordered_set only if the value to be inserted is unique, with a given hint.
  • == operator – The ‘==’ is an operator in C++ STL performs equality comparison operation between two unordered sets and unordered_set::operator== is the corresponding operator function for the same.
  • key_eq()– Returns a boolean value according to the comparison. It returns the key equivalence comparison predicate used by the unordered_set.
  • operator!=– The != is a relational operator in C++ STL which compares the equality and inequality between unordered_set containers.
  • max_bucket_count() – Find the maximum number of buckets that unordered_set can have.

unordered_set insert() function in C++ STL

The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not equivalent to any other elements already present in the container (elements in an unordered_set have unique values). The insertion is done automatically at the position according to the container’s criterion. This effectively increases the container size by the number of elements inserted.

Syntax:

unordered_set_name.insert (Value)

or,

unordered_set_name.insert (InputIterator first, InputIterator last)

Parameters:

  • Value: It specifies the value which is to be inserted in the container.
  • firstlast: Iterators specifying a range of elements. Copies of the elements in the range [first, last) are inserted in the unordered_set container. Keep in mind that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

Return Value: The function returns an iterator pointing either to the newly inserted element in the container or to the element whose key is equivalent.

Below programs illustrate the above function:

Program 1:

#include<iostream> 
#include <string> 
#include <unordered_set> 
using namespace std; 

int main() 
{ 
	unordered_set<string> mySet = { "first", "third" }; 

	string myString = "tenth"; 

	// inserts key in set 
	mySet.insert(myString); 

	cout << "My set contains:"
		<< endl; 
	for (const string& x : mySet) { 
		cout << x 
			<< " "; 
	} 

	cout << endl; 
	return 0; 
} 

Output:

My set contains:
tenth first third

Program 2:

// C++ program to illustrate 
// unordered_set::insert() 

#include <array> 
#include <iostream> 
#include <string> 
#include <unordered_set> 
using namespace std; 

int main() 
{ 
	unordered_set<std::string> mySet = { "first", "third", "second" }; 
	array<std::string, 2> myArray = { "tenth", "seventh" }; 
	string myString = "ninth"; 

	mySet.insert(myString); 

	// array elements range insertion in set 
	mySet.insert(myArray.begin(), myArray.end()); 

	// initializer list insertion 
	mySet.insert({ "fourth", "sixth" }); 

	cout << "myset contains:"
		<< endl; 
	for (const string& x : mySet) { 
		cout << x 
			<< " "; 
	} 
	cout << endl; 

	return 0; 
} 

Output:

myset contains:
sixth fourth seventh first tenth second third ninth

unordered_set begin() function in C++ STL

The unordered_set::begin() method is a builtin function in C++ STL which is used to return an iterator pointing to the first element in the unordered_set container. All of the iterators of an unordered_set can be used to only access the elements, iterators are not allowed to modify elements present in an unordered_set container.

Note: This iterator can point to either the very first element or the first element of any specified bucket in the unordered_set container.

Syntax:

unordered_set_name.begin(n)

Parameter: This is an optional parameter and specifies the bucket number. If this parameter is not passed then the begin() method will return an iterator pointing to the first element of the container and if this parameter is passed then the begin() method will return an iterator pointing to the first element of the specific bucket in the unordered_set container.

Return Value: This function returns an iterator pointing to the first element in the container or a specified bucket in the container.

Below program illustrate the unordered_set::begin() function:

// CPP program to illustrate the 
// unordered_set::begin() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<int> sampleSet; 

	// Inserting elements in the std 
	sampleSet.insert(5); 
	sampleSet.insert(10); 
	sampleSet.insert(15); 
	sampleSet.insert(20); 
	sampleSet.insert(25); 

	auto itr1 = sampleSet.begin(); 
	auto itr2 = sampleSet.begin(4); 

	cout << "First element in the container is: " << *itr1; 
	cout << "\nFirst element in the bucket 4 is: " << *itr2; 

	return 0; 
} 

Output:

First element in the container is: 25
First element in the bucket 4 is: 15

unordered_set end() in C++ STL

The unordered_set::end() function is a built-in function in C++ STL which returns an iterator pointing to the past-the-end-element. This iterator does not directly point to an element, rather it points to the location just after the last element.

Syntax

umap_name.end()

or,

umap_name.end(int i)

Parameters: This function takes a single integer parameter i which is optional.

Return value:

  • If the parameter i is not passed then the function returns an iterator pointing to the past-the-end element. Actually, it does not point to any element of the set, but it points to the position following the last element of the container.
  • If the parameter i is passed then the function returns an iterator pointing to the past-the-end element of i-th bucket. As in the previous case, it does not point to any element of the set, but it points to the position following the last element of i-th bucket.
    So the iterator returned by unordered_set::end() cannot be dereferenced.

Below programs illustrate the unordered_set::end() function:

Program 1:

// CPP program to illustrate the unordered_set::end() 
// function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 
	unordered_set<int> sampleSet = 
			{ 5, 10, 15, 4, 2, 7, 8, 6 }; 

	// Continue the loop until it points to the 
	// past-the-end position returned by sampleSet.end() 
	for (auto it = sampleSet.begin(); it != sampleSet.end(); 
														it++) 
	{ 
		cout << *it << " "; 
	} 

	return 0; 
} 

Output:

6 8 7 2 4 15 10 5

Program 2:

// CPP program to illustrate the 
// unordered_set::end() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 
	unordered_set<int> sampleSet = 
				{ 5, 10, 15, 4, 2, 7, 8, 6 }; 

	// displaying all the buckets of the set. 
	// Continue the loop until it points to the 
	// past-the-end position returned by sampleset.end(i) 
	for (unsigned i = 0; i < sampleSet.bucket_count(); ++i) 
	{ 
		cout << "Bucket " << i << " Contains: "; 
		for (auto it1 = sampleSet.begin(i); 
						it1 != sampleSet.end(i); ++it1) 
			cout << " " << *it1; 
		cout << endl; 
	} 
	
	return 0; 
} 

Output:

Bucket 0 Contains: 
Bucket 1 Contains: 
Bucket 2 Contains:  2
Bucket 3 Contains: 
Bucket 4 Contains:  4 15
Bucket 5 Contains:  5
Bucket 6 Contains:  6
Bucket 7 Contains:  7
Bucket 8 Contains:  8
Bucket 9 Contains: 
Bucket 10 Contains:  10

unordered_set count() function in C++ STL

The unordered_set::count() function is a built-in function in C++ STL which is used to count occurrences of a particular element in an unordered_set container. As the unordered_set container does not allows to store duplicate elements so this function is generally used to check if an element is present in the container or not. The function returns 1 if the element is present in the container otherwise it returns 0. count检查元素是否在容器中,存在返回1,否则返回0.

Syntax:

unordered_set_name.count(element)

Parameter: This function accepts a single parameter element. This parameter represents the element which is needed to be checked if it is present in the container or not.

Return Value: This function returns 1 if the element is present in the container otherwise it returns 0.

Below programs illustrate the unordered_set::count() function:

Program 1:

// CPP program to illustrate the 
// unordered_set::count() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<int> sampleSet; 

	// Inserting elements 
	sampleSet.insert(5); 
	sampleSet.insert(10); 
	sampleSet.insert(15); 
	sampleSet.insert(20); 
	sampleSet.insert(25); 

	// displaying all elements of sampleSet 
	cout << "sampleSet contains: "; 
	for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) { 
		cout << *itr << " "; 
	} 

	// checking if element 20 is present in the set 
	if (sampleSet.count(20) == 1) { 
		cout << "\nElement 20 is present in the set"; 
	} 
	else { 
		cout << "\nElement 20 is not present in the set"; 
	} 

	return 0; 
} 

Output:

sampleSet contains: 25 5 10 15 20 
Element 20 is present in the set

Program 2:

// C++ program to illustrate the 
// unordered_set::count() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<string> sampleSet; 

	// Inserting elements 
	sampleSet.insert("Welcome"); 
	sampleSet.insert("To"); 
	sampleSet.insert("GeeksforGeeks"); 
	sampleSet.insert("Computer Science Portal"); 
	sampleSet.insert("For Geeks"); 

	// displaying all elements of sampleSet 
	cout << "sampleSet contains: "; 
	for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) { 
		cout << *itr << " "; 
	} 

	// checking if element GeeksforGeeks is 
	// present in the set 
	if (sampleSet.count("GeeksforGeeks") == 1) { 
		cout << "\nGeeksforGeeks is present in the set"; 
	} 
	else { 
		cout << "\nGeeksforGeeks is not present in the set"; 
	} 

	return 0; 
} 

Output:

sampleSet contains: Welcome To GeeksforGeeks For Geeks Computer Science Portal 
GeeksforGeeks is present in the set

unordered_set find() function in C++ STL

The unordered_set::find() function is a built-in function in C++ STL which is used to search for an element in the container. It returns an iterator to the element, if found else, it returns an iterator pointing to unordered_set::end().  返回找到的元素的迭代器,没找到,返回指向最后的元素的迭代器。

Syntax :

unordered_set_name.find(key)

Parameter: This function accepts a mandatory parameter key which specifies the element to be searched for.

Return Value: It returns an iterator to the element if found, else returns an iterator pointing to the end of unordered_set.

Below programs illustrate the unordered_set::find() function:

Program 1:

// C++ program to illustrate the 
// unordered_set::find() function 

#include <iostream> 
#include <string> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" }; 

	// use of find() function 
	if (sampleSet.find("geeks1") != sampleSet.end()) { 
		cout << "element found." << endl; 
	} 
	else { 
		cout << "element not found" << endl; 
	} 

	return 0; 
} 

Output:

element found.

Program 2:

// CPP program to illustrate the 
// unordered_set::find() function 

#include <iostream> 
#include <string> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" }; 

	// use of find() function 
	if (sampleSet.find("geeksforgeeks") != sampleSet.end()) { 
		cout << "found" << endl; 
	} 
	else { 
		cout << "Not found" << endl; 
	} 

	return 0; 
} 

Output:

Not found

unoredered_set clear() function in C++ STL

The unordered_set::clear() function is a built-in function in C++ STL which is used to clear an unordered_set container. That is, this function removes all of the elements from an unordered_set and empties it. All of the iterators, pointers, and references to the container are invalidated. This reduces the size of the container to zero.删除容器所有元素,容器大小变为1.

Syntax:

unordered_set_name.clear()

Parameter: This function does not accepts any parameter.

Return Value: This function does not returns any value.

Below programs illustrate the unordered_set::clear() function:

Program 1:

// C++ program to illustrate the 
// unordered_set::clear() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<int> sampleSet; 

	// Inserting elements 
	sampleSet.insert(5); 
	sampleSet.insert(10); 
	sampleSet.insert(15); 
	sampleSet.insert(20); 
	sampleSet.insert(25); 

	// displaying all elements of sampleSet 
	cout << "sampleSet contains: "; 
	for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) { 
		cout << *itr << " "; 
	} 

	// clear the set 
	sampleSet.clear(); 

	// size after clearing 
	cout << "\nSize of set after clearing elemets: "
		<< sampleSet.size(); 

	return 0; 
} 

Output:

sampleSet contains: 25 5 10 15 20 
Size of set after clearing elemets: 0

Program 2:

// C++ program to illustrate the 
// unordered_set::clear() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<string> sampleSet; 

	// Inserting elements 
	sampleSet.insert("Welcome"); 
	sampleSet.insert("To"); 
	sampleSet.insert("GeeksforGeeks"); 
	sampleSet.insert("Computer Science Portal"); 
	sampleSet.insert("For Geeks"); 

	// displaying all elements of sampleSet 
	cout << "sampleSet contains: "; 
	for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) { 
		cout << *itr << " "; 
	} 

	// clear the set 
	sampleSet.clear(); 

	// size after clearing 
	cout << "\nSize of set after clearing elemets: "
		<< sampleSet.size(); 

	return 0; 
} 

Output:

sampleSet contains: Welcome To GeeksforGeeks For Geeks Computer Science Portal 
Size of set after clearing elemets: 0

unordered_set empty() function in C++ STL

The unordered_set::empty is a built-in function in C++ STL which is used to check if an unordered_set container is empty or not. It returns True if the unordered_set container is empty, otherwise it returns False.  判断容器是否为空,空返回1,不空返回0.

Syntax:

map_name.empty()

Parameters: This function does not accepts any parameter.

Return Value: It returns a boolean value True if the unordered_set container is empty, else false.

Below programs illustrate the above function:

Program 1:

// C++ program to illustrate the 
// unordered_set::empty function 
#include <iostream> 
#include <unordered_set> 
using namespace std; 

int main() 
{ 

	// declaration 
	unordered_set<int> sample; 

	// Check whether the unordered_set is empty 
	if (sample.empty() == true) 
		cout << "true" << endl; 
	else
		cout << "false" << endl; 

	// Insert a value 
	sample.insert(5); 

	// Now check whether it is empty 
	if (sample.empty() == true) 
		cout << "true" << endl; 
	else
		cout << "false" << endl; 

	return 0; 
} 

Output:

true
false

Program 2:

// C++ program to illustrate the 
// unordered_set::empty function 
#include <iostream> 
#include <unordered_set> 
using namespace std; 

int main() 
{ 
	// declaration 
	unordered_set<int> uset; 

	// Insert a value 
	uset.insert({ 5, 6, 7, 8 }); 
	// Check whether the unordered_set is empty 
	if (uset.empty() == true) 
		cout << "true" << endl; 
	else
		cout << "false" << endl; 

	return 0; 
} 

Output:

false

unordered_set size() function in C++ STL

The unordered_set::size() method is a builtin function in C++ STL which is used to return the number of elements in the unordered_set container. 返回容器中元素的个数。

Syntax:

unordered_set_name.size()

Parameter: It does not accepts any parameter.

Return Value: The function returns the number of elements in the container.

Below programs illustrate the unordered_set::size() function:

Program 1:

// C++ program to illustrate the 
// unordered_set.size() function 
#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<int> arr1 = { 1, 2, 3, 4, 5 }; 

	// prints the size of arr1 
	cout << "size of arr1:" << arr1.size(); 

	// prints the element 
	cout << "\nThe elements are: "; 
	for (auto it = arr1.begin(); it != arr1.end(); it++) 
		cout << *it << " "; 

	return 0; 
} 

Output:

size of arr1:5
The elements are: 5 1 2 3 4

Program 2:

// C++ program to illustrate the 
// unordered_set::size() function 
// when container is empty 
#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<int> arr2 = {}; 

	// prints the size 
	cout << "Size of arr2 : " << arr2.size(); 

	return 0; 
} 

Output:

Size of arr2 : 0

unordered_set erase() function in C++ STL

The unordered_set::erase() function is a built-in function in C++ STL which is used to remove either a single element of a range of elements ranging from start(inclusive) to end(exclusive). This decreases the size of a container by the number of elements removed.  删除特定范围的元素[start, end),大小降低了删去元素的个数。

Note: Buckets in unordered_set are numbered from 0 to n-1, where n is the total number of buckets.

Syntax:

unordered_set_name.erase(iterator start, iterator end)
          or 
unordered_set_name.erase(iterator position)
          or
unordered_set_name.erase(element)

Parameters: The function accepts three type of parameters. If it accepts a single element, then it finds that particular element and erases it. If it accepts an iterator, then it erases the element present at that position. If it accepts two iterators start and end, it erases all the elements in the range [start, end]

Return Value: This function returns an iterator pointing to the element following the last element which is erased in case of first two syntaxes. In case of the third syntax, it returns 1 if the element is present in the unordered_set else it returns 0 after erasing the element

Below programs illustrate the unordered_set::erase() function:

Program 1:

// CPP program to illustrate the 
// unordered_set::erase() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<int> sampleSet; 

	// Inserting elements 
	sampleSet.insert(5); 
	sampleSet.insert(10); 
	sampleSet.insert(15); 
	sampleSet.insert(20); 
	sampleSet.insert(25); 

	// erases a particular element by its position 
	sampleSet.erase(sampleSet.find(10)); 

	// displaying the set after removal 
	for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) { 
		cout << *it << " "; 
	} 

	// erases a range of elements 
	sampleSet.erase(sampleSet.begin(), sampleSet.end()); 

	cout << "\nSet size: " << sampleSet.size(); 

	return 0; 
} 

Output:

25 5 15 20  
Set size: 0

Program 2:

// CPP program to illustrate the 
// unordered_set::erase() function 

#include <iostream> 
#include <string> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

	unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" }; 

	// erases a particular element 
	sampleSet.erase("geeks1"); 

	// displaying the set after removal 
	cout << "Elements: "; 
	for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) { 
		cout << *it << " "; 
	} 

	sampleSet.insert("geeks1"); 
	// erases from where for is 
	sampleSet.erase(sampleSet.find("for"), sampleSet.end()); 

	// displaying the set after removal 
	cout << "\nAfter second removal set : "; 
	for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) { 
		cout << *it << " "; 
	} 

	return 0; 
} 

Output:

Elements: geeks2 for 
After second removal set : geeks1 geeks2 

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/86623890
今日推荐