Misuse of Multiset container erase function

Source: Internet
Author: User

The 3rd chapter of library function, which is about the problem of library function, is introduced in this chapter. Using library functions can reduce the difficulty of software development and improve the efficiency of code writing. This section introduces the misuse of the Multiset container erase function.

Ad:51cto Net + 12th salon: The beauty of big data-how to drive user experience with data

Misuse of erase function of 3.16 multiset container

code example

  1. int main () {
  2. Multiset <int> c1;
  3. C1.insert (3);
  4. C1.insert (2);
  5. C1.insert (3);
  6. C1.insert (3);
  7. C1.insert (5);
  8. int x=3;
  9. C1.erase (x);//remove one element with value 3
  10. for (Multiset <int>::iterator it = c1.begin (); It! = C1.end (); it++)
  11. {
  12. cout << *it << Endl;
  13. }
  14. return 0;
  15. }

Phenomena & Consequences

The code expects to delete an element with a value of 3, but the actual running result shows that all elements with a value of 3 are deleted.

Bug Analysis

There are two types of erase function prototypes with one parameter multiset. One is to pass an element value, as in the example code above, when all the values in the collection are equal to the input values, and the number of deleted elements is returned, and the other is to pass a iterator that points to an element, at which point the corresponding element is deleted, and no return value is removed. The user needs to correctly invoke the corresponding prototype according to their own application scenario. The intent of the example code is to delete an element, but it actually removes all elements with a value of 3, which is not expected.

Correct code

  1. int main () {
  2. Multiset <int> c1;
  3. C1.insert (3);
  4. C1.insert (2);
  5. C1.insert (3);
  6. C1.insert (3);
  7. C1.insert (5);
  8. int x=3;
  9. Multiset <int>::iterator pos = c1.find (x);
  10. C1.erase (POS);//remove one element with value 3
  11. for (Multiset <int>::iterator it = c1.begin (); It! = C1.end (); it++) /c7>
  12. {
  13. cout << *it << Endl;
  14. }
  15. return 0;
  16. }

Programming recommendations

When using multiset, it is important to note that the main difference between Mutilset and the normal set container is that Multiset allows elements to be duplicated, and set does not allow elements to be duplicated. This can have a different effect on some operations.

Misuse of Multiset container erase function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.