C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ multiset find()C++ multiset find() function is used to find an element with the given value val. If it finds the element, then it returns an iterator pointing to the element otherwise, it returns an iterator pointing to the end of the multiset i.e. multiset::end(). Syntaxiterator find (const value_type& val) const; // until C++ 11 const_iterator find (const value_type& val) const; //since C++ 11 iterator find (const value_type& val); //since C++ 11 Parameterval: specifies the value to be searched in the multiset container. Return valueIf it finds the element then it returns an iterator pointing to the element otherwise, it returns an iterator pointing to the end of the multiset i.e. multiset::end(). ComplexityLogarithmic in size. Iterator validityNo changes. Data RacesThe container is accessed (neither the constant nor the non-constant versions modify the container. No mapped values are accessed: concurrently accessing and modifying the elements is safe. Exception SafetyIf an exception is thrown, there are no changes in the multiset container. Example 1Let's see the simple example to find the element with the given key value: #include <iostream> #include <set> using namespace std; int main(void) { multiset<int> m = {100,200,300,300}; auto it = m.find(300); cout << "Iterator points to " << *it << endl; return 0; } Output: Iterator points to 300 Example 2Let's see a simple example to find the element: #include <iostream> #include <set> using namespace std; int main(void) { multiset<char> m = {'a', 'b', 'c', 'a'}; auto it = m.find('e'); if ( it == m.end() ) { // not found cout<<"Element not found"; } else { // found cout << "Iterator points to " << *it<< endl; } return 0; } Output: Element not found In the above example, find() function find the key value e in the multiset m, if it is not found in the multiset then it will return a not found message otherwise, it will display the multiset. Example 3Let's see a simple example: #include <iostream> #include <set> using namespace std; int main() { char n; multiset<char> example = {'a','b','c','d','b'}; cout<<"Enter the element which you want to search: "; cin>>n; auto search = example.find(n); if (search != example.end()) { cout << n<<" found and the value is " << *search << '\n'; } else { cout << n<<" not found\n"; } } Output: Enter the element which you want to search: b b found and the value is b In the above example, find() function is used to find the element according to user's given value. Example 4Let's see a simple example: #include <iostream> #include <set> using namespace std; int main () { multiset<int> mymultiset; multiset<int>::iterator it; for (int i = 1; i <= 10; i++) mymultiset.insert(i*10); it = mymultiset.find(40); mymultiset.erase (it); mymultiset.erase (mymultiset.find(60)); cout << "mymultiset contains:"; for (it = mymultiset.begin(); it!=mymultiset.end(); ++it) cout << ' ' << *it; cout << '\n'; return 0; } Output: mymultiset contains: 10 20 30 50 70 80 90 100
Next TopicC++ multiset
|