C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ multiset end()C++ multiset end() function is used to return an iterator which is next to the last entry in the multiset. Note:- This is a placeholder. No element exists in this location and attempting to access is undefined behavior.Syntax
iterator end(); //until C++ 11
const_iterator end() const; //until C++ 11
iterator end() noexcept; //since C++ 11
const_iterator end() const noexcept; //since C++ 11
Parameter
None Return valueIt returns an iterator which is pointing next to the last element of the multiset. Complexity
Constant. Iterator validityNo changes. Data RacesConcurrently accessing the elements of a multiset is safe. The container is accessed neither the non-const nor the const versions modify the container. Exception SafetyThis member function never throws exception. Example 1Let's see the simple example for end() function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<string> mymultiset = {"Java", "C++", "Java", "C++"};
// show content:
for (multiset<string>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it<< '\n';
return 0;
}
Output: C++ C++ Java Java In the above example, end() function is used to return an iterator pointing next to the last element in the mymultiset multiset. Example 2Let's see a simple example to iterate over the multiset using for-each loop:
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> c;
c.insert(5);
c.insert(2);
c.insert(1);
c.insert(1);
c.insert(0);
c.insert(0);
c.insert(2);
multiset<int>::iterator i = c.begin();
while (i != c.end())
cout << *i++ << " ";
cout << endl;
}
Output: 0 0 1 1 2 2 5 Example 3Let's see a simple example to iterate over the multiset using while loop:
#include <iostream>
#include <set>
#include <string>
int main()
{
using namespace std;
multiset<string> mymultiset = { "Nikita","Deep","Deep","Sonu","Aman" };
cout<<"Elements of mymultiset are: "<<endl;
multiset<string>::const_iterator it; // declare an iterator
it = mymultiset.begin(); // assign it to the start of the multiset
while (it != mymultiset.end()) // while it hasn't reach the end
{
cout << *it << "\n";
// print the value of the element it points to
++it; // and iterate to the next element
}
cout << endl;
}
Output: Elements of mymultiset are: Aman Deep Deep Nikita Sonu In the above example, end() function is used to return an iterator pointing next to the last element in the mymultiset multiset. Example 4Let's see a simple example:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
int val;
multiset<int> c = {10,20,10,20,40,50};
cout<<"Enter value to find: ";
cin>>val;
auto result = c.find(val);
//find until end of the multiset elements
if (result != c.end()) {
cout << "Element found: "<< *result;
cout << endl;
} else {
cout << "Element not found." << endl;
}
return 0;
}
Output: Enter value to find: 60 Element not found. Enter value to find: 20 Element found: 20 In the above example, end() function is used to return an iterator pointing next to the last element in the mymultiset multiset.
Next TopicC++ multiset
|