C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ multiset emplace()C++ Multiset emplace() function is used to extend the multiset container by inserting new elements into the container. Elements are built directly (neither copied nor moved). The constructor of the element is called by giving the arguments args passed to this function. Syntaxtemplate <class? Args> iterator emplace (Args&&? args); //since C++ 11 Parameterargs: The arguments forwarded to construct an element to be inserted into the container. Return valueemplace() function returns a bool pair that will indicate if the insertion is occurred or not, and returns an iterator pointing to the newly inserted element. ComplexityLogarithmic in the container size. Iterator validityNo changes. Data RacesThe container is modified. Iterating ranges in the container is not safe although concurrently accessing exiting 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 insert the elements into the multiset: #include <iostream> #include <set> using namespace std; int main(void) { multiset<char> m; m.emplace('a'); m.emplace('b'); m.emplace('a'); m.emplace('c'); m.emplace('b'); cout << "Multiset contains following elements" << endl; for (auto it = m.begin(); it != m.end(); ++it) cout << *it<< ", "; return 0; } Output: Multiset contains following elements a, a, b, b, c, In the above example, it simply inserts the element into the multiset m with the given values. Example 2Let's see a simple example to insert the element and check for the duplicate key: #include <set> #include <string> #include <iostream> using namespace std; template <typename S> void print(const S& s) { cout << s.size() << " elements: "; for (const auto& p : s) { cout << "(" << p << ") "; } cout << endl; } int main() { multiset<string> s1; s1.emplace("Deep"); s1.emplace("Nikita"); s1.emplace("Kesharwani"); cout << "multiset modified, now contains "; print(s1); cout << endl; s1.emplace("Nikita"); cout << "multiset modified, now contains "; print(s1); cout << endl; } Output: multiset modified, now contains 3 elements: (Deep) (Kesharwani) (Nikita) multiset modified, now contains 4 elements: (Deep) (Kesharwani) (Nikita) (Nikita) Example 3Let's see a simple example to find the sum of the inserted elements: #include <iostream> #include <set> using namespace std; int main() { // sum variable declaration int sum = 0; // multiset declaration multiset<int> mymultiset{}; mymultiset.emplace(1); mymultiset.emplace(3); mymultiset.emplace(4); mymultiset.emplace(1); mymultiset.emplace(2); mymultiset.emplace(2); mymultiset.emplace(3); // iterator declaration multiset<int>::iterator it; // finding sum of elements while (!mymultiset.empty()) { it = mymultiset.begin(); sum = sum + *it; mymultiset.erase(it); } // printing the sum cout << "Sum of elements is: "<<sum; return 0; } Output: Sum of elements is: 16 Example 4Let's see a simple example to insert the element: #include <iostream> #include <set> #include <string> using namespace std; int main() { typedef multiset<string> city; string name; city fmly ; int n; cout<<"Enter the number of family members :"; cin>>n; cout<<"Enter the name of each member: \n"; for(int i =0; i<n; i++) { cin>> name; // Get key fmly.emplace(name); } cout<<"\nTotal member of family is:"<< fmly.size(); cout<<"\nDetails of family members: \n"; cout<<"\nName \n ________________________\n"; city::iterator p; for(p = fmly.begin(); p!=fmly.end(); p++) { cout<<(*p)<<" \n "; } return 0; } Output: Enter the number of family members: 3 Enter the name of each member: Bob Robin David Total member of family is: 3 Details of family members: Name ________________________ Bob David Robin In the above example, it simply inserts the elements by the user's choice.
Next TopicC++ multiset
|