Hash Sets

Is There a HashSet in C#?

Yes, C# has HashSet<T>, but C++ uses std::unordered_set for the same job. Here is a name mapping and a short example that compares the two languages.

On this page
  1. Short answer
  2. HashSet in C#
  3. The C++ equivalent
  4. Name mapping
  5. What about C?
  6. Where to go next

Short answer

Yes. In C#, HashSet<T> is in the System.Collections.Generic namespace. C++ has no class with that name. The C++ equivalent is std::unordered_set, from the <unordered_set> header. The C language has no hash set at all.

This page is about C++, so the C# code below is here only for comparison.

HashSet in C#

A short C# example:

using System.Collections.Generic;

var colors = new HashSet<string>();
colors.Add("red");
colors.Add("green");

bool addedAgain = colors.Add("red");        // false, red is already in the set
bool hasGreen = colors.Contains("green");   // true

Add returns true when the item was added and false when it was already there. Contains checks whether an item is present.

The C++ equivalent

Here are the same steps in C++:

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

int main() {
    std::unordered_set<std::string> colors;
    colors.insert("red");
    colors.insert("green");

    colors.insert("red");  // ignored, red is already in the set
    bool has_green = colors.find("green") != colors.end();

    std::cout << std::boolalpha << has_green << "\n";  // prints true
    return 0;
}

insert() plays the role of Add, and find() compared with end() plays the role of Contains. insert() returns a pair, and its second member tells you whether the value was added.

Name mapping

The names differ between languages, but the ideas line up:

C# C++ What it holds
HashSet<T> std::unordered_set<T> Unique items, no guaranteed order
Dictionary<TKey, TValue> std::unordered_map<K, V> Key-value pairs, no guaranteed order
SortedSet<T> std::set<T> Unique items, kept in sorted order

C++ has no class named HashSet, so when you read C++ code, look for std::unordered_set instead.

What about C?

The C standard library has no hash set and no set type. If you are writing C and need one, you would write your own hash table or use a third-party library. C++ includes std::unordered_set in its standard library, so you do not need to write one yourself.

Where to go next

If you are learning C++ and want the full walkthrough of the C++ type, read about hashset in c. It covers creating, checking, removing and looping, along with common mistakes.

The complete guide to hash sets

Hash Sets in C++ With std::unordered_set
Read the guide

Keep exploring

Guides from other topics

All topics →

Rust vs C++ Performance

Rust and C++ both compile to fast native code, so speed gaps are often small. Learn what drives performance in each, and how to read benchmarks fairly.

Rust vs C++ 6 min read

The sizeof Operator in C++

Learn how the sizeof operator in C++ works: what it returns in bytes, how it handles arrays, pointers and structs, and common mistakes to avoid.

sizeof 6 min read