How to Find the First Element in a Set in C++? (original) (raw)
Last Updated : 26 Feb, 2024
In C++, a set is a container that stores unique elements following a specific order. In this article, we will learn how to find the first element in a set.
Example:
**Input: mySet = {1, 2, 4, 3, 8, 4, 7, 8, 6, 4}
**Output: First Element = 1
Find the First Element in a Set in C++
To find the first element in a std::set in C++, we can use the std:📐:begin() function that returns an iterator pointing to the first element of the set. We can dereference this iterator to get the first element in the set.
C++ Program to Find the First Element in a Set
C++ `
// C++ Program to Find the First Element from vector #include #include using namespace std;
int main() { set mySet = { 3, 1, 4, 1, 5, 9, 2, 6 };
// Finding the first element in the set
auto firstElement = mySet.begin();
cout << "First element: " << *firstElement << endl;
return 0;
}
`
**Time complexity: O(1)
**Space complexity: O(1)
Similar Reads
- How to Find the Last Element in a Set in C++? In this article, we will learn how to find the last element is a set in C++.The most efficient way to find the last element of set is by using set rbegin() function. Let’s take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { set s = {1, 3, 4, 2 min read
- How to Find the Frequency of an Element in a Set in C++? C++ STL provides a set container that can be used to store unique elements in a sorted order. In this article, we will learn how to find the frequency of an element in a set in C++. Example: Input:sets ={10,20,30,40,50}Output:Frequency of 30 is 1Finding Frequency of an Element in a Set in 2 min read
- How to Find the Minimum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that stores data in non-contiguous memory locations efficiently. In this article, we will learn how to find the minimum element in a list in C++. Example: Input: myList = {30, 10, 20, 50, 40};Output: The minimum element in the list i 2 min read
- How to Find the Maximum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to find the maximum element in a list in C++. Example: Input: myList = {30, 20, 10, 5 2 min read
- How to Find First Occurrence of an Element in a List in C++? In C++, the list is a sequence container that stores data in non-contiguous memory allocation. It is defined in the STL (Standard Template Library) inside the header. In this article, we will learn how to find the first occurrence of a specific element in a list in C++. Example: Input: 2 min read
- How to Find First Occurrence of an Element in a Set in C++? In C++, a set is an ordered container that stores its unique values in some given order. In this article, we will see how to find the first occurrence of a specific element in a set in C++ STL. For Example, Input: mySet = {1, 2, 3, 8, 9, 11} Target = 9 Output: Element found at Index: 4Find the First 2 min read
- How to Find the Frequency of an Element in a Multiset in C++? In C++, a multiset is a container that stores elements in a sorted order and multiple elements can have the same values. In this article, we will learn how to find the frequency of a specific element in a multiset. Example: Input: myMultiset = { 5,2,8,5,8,8} Element: 8 Output: Frequency of 8 is: 3Fi 2 min read
- How to Find Frequency of an Element in a List in C++? In C++, lists are sequence containers that allow non-contiguous memory allocation. They are implemented as doubly-linked lists. The frequency of a specific element means how many times that particular element occurs in a list. In this article, we will learn how to find the frequency of a specific el 2 min read
- How to Find the Mode of All Elements in a List in C++? In C++, a list is a sequence container used to store data of similar type in non-contiguous memory locations. The mode of a list represents the element that occurs most frequently in the container. In this article, we will learn how to find the mode of a list in C++. Example: Input:myList = {1, 2, 3 2 min read
- How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read