Misuse of replace function in STL

Source: Internet
Author: User

This is a problem encountered when solving the question of joj 1170. The replace function cannot get the expected result. After debugging, it is found that the replace function is not correctly executed. Check the source code in STL to find the problem. To solve this problem, re-write a short test code:

  1. # Include <algorithm>
  2. # Include <cstdio>
  3. # Include <ctime>
  4. # Include <cstdlib>
  5. Using namespace std;
  6. Int main (){
  7. Int testArr [16], I;
  8. Srand (time (0 ));
  9. For (I = 0; I <16; I ++)
  10. TestArr [I] = rand () % 5;
  11. Printf ("before replace:/n ");
  12. For (I = 0; I <16; I ++)
  13. Printf ("% 3d", testArr [I]);
  14. Printf ("/n ");
  15. Printf ("after replace testArr [0] with testArr [1]:/n ");
  16. Replace (testArr, testArr + 16, testArr [0], testArr [1]);
  17. For (I = 0; I <16; I ++)
  18. Printf ("% 3d", testArr [I]);
  19. Printf ("/n ");
  20. Printf ("helo world ");
  21. Return 0;
  22. }

The output result is as follows:

Before replace:
2 1 1 4 4 3 2 2 0 3 1 4 0 3 2 0
After replace testarr [0] With testarr [1]:
1 1 1 4 4 3 2 2 0 3 1 4 0 3 2 0

According to the expectation of the program, after the execution, all the 2 in the original array should be replaced with 1, but the output result indicates that the program only replaces the first 2 with 1, the following two remain unchanged.

The programming environment used is Emacs + mingw. It is executed to the replace call through GDB debugging, and the source code of replace is tracked by entering the S command, as shown below:

This code is in the stl_algo.h header file:

  1. Template <typename _ forwarditer, typename _ TP>
  2. Void
  3. Replace (_ forwarditer _ first, _ forwarditer _ last,
  4. Const _ TP & _ old_value, const _ TP & _ new_value)
  5. {
  6. // Concept requirements
  7. _ Glibcpp_function_requires (_ mutable_forwarditeratorconcept <_ forwarditer>)
  8. _ Glibcpp_function_requires (_ javasopconcept <
  9. Typename iterator_traits <_ forwarditer >:: value_type, _ TP>)
  10. _ Glibcpp_function_requires (_ convertibleconcept <_ TP,
  11. Typename iterator_traits <_ forwarditer >:: value_type>)
  12. For (; _ first! = _ Last; ++ _ First)
  13. If (* _ first = _ old_value)
  14. * _ First = _ new_value;
  15. }

We can't see the problem after executing the command here. We can only find it when we run the disp command in a single step to view _ old_value and _ new_value. After the first replacement operation is executed, __old_value becomes _ new_value, so no replacement operation will be performed later.

What is the cause of the problem? The reason is that the last two parameters of the replace function are const T & type.

Replace (testarr, testarr + 16, testarr [0], testarr [1]);

When calling, it is not the value of testArr [0], but the address of testArr [0 !!!

Modify the preceding call:

Replace (testArr, testArr + 16, (int) testArr [0], (int) testArr [1]);

In this way, the call will not fail.

 

The same Code is also a problem in VC6.0. This is also true for online judge. According to the previous call method, a wrong answer is submitted, and the latter one is called, so it is accepted.

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.