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:
- # Include <algorithm>
- # Include <cstdio>
- # Include <ctime>
- # Include <cstdlib>
- Using namespace std;
- Int main (){
- Int testArr [16], I;
- Srand (time (0 ));
- For (I = 0; I <16; I ++)
- TestArr [I] = rand () % 5;
- Printf ("before replace:/n ");
- For (I = 0; I <16; I ++)
- Printf ("% 3d", testArr [I]);
- Printf ("/n ");
- Printf ("after replace testArr [0] with testArr [1]:/n ");
- Replace (testArr, testArr + 16, testArr [0], testArr [1]);
- For (I = 0; I <16; I ++)
- Printf ("% 3d", testArr [I]);
- Printf ("/n ");
- Printf ("helo world ");
- Return 0;
- }
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:
- Template <typename _ forwarditer, typename _ TP>
- Void
- Replace (_ forwarditer _ first, _ forwarditer _ last,
- Const _ TP & _ old_value, const _ TP & _ new_value)
- {
- // Concept requirements
- _ Glibcpp_function_requires (_ mutable_forwarditeratorconcept <_ forwarditer>)
- _ Glibcpp_function_requires (_ javasopconcept <
- Typename iterator_traits <_ forwarditer >:: value_type, _ TP>)
- _ Glibcpp_function_requires (_ convertibleconcept <_ TP,
- Typename iterator_traits <_ forwarditer >:: value_type>)
- For (; _ first! = _ Last; ++ _ First)
- If (* _ first = _ old_value)
- * _ First = _ new_value;
- }
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.