Why merge() can not recognized?


  • 0
    R
    class Solution {
    public:
    void merge(int A[], int m, int B[], int n)
    {
    	//vector<int> veca(&A[0],&A[m]);
    	//vector<int> vecb(&B[0],&B[n]);
    
    	vector<int> vecc(m+n);
    	merge(A,A+m,B,B+n,vecc.begin());
    	for(int i=0;i<vecc.size();i++)
    		A[i]=vecc.at(i);
    	return ;
    }
    };
    

    It's a easy problem, but compile error:
    Line 9: no matching function for call to 'Solution::merge(int*&, int*, int*&, int*, std::vector<int>::iterator)'

    merge in "algorithm" is not C++11 extra function, I think g++ would support, could anyone help me explain it?


  • 0
    S

    void merge(int A[], int m, int B[], int n) does not match merge(A,A+m,B,B+n,vecc.begin()) .

    vecc.begin() is type of std::vector::iterator, but the definition needs int.

    I am not familiar with the original merge in c++ library. I won't suggest to use it though. It makes this problem meaningless.


  • 0
    R

    I do not mean recursive, just a lib function in algorithm......


  • 0
    S

    Their names are same, this code won't call the library one.


  • 0
    R

    ok, I know your meaning, that's really the problem, thank you~~~


  • 0
    S

    Since merge() is a member function of Solution::, you need to use std::merge() when you try to call it in Solution::merge(). Your code will be accepted if you make this change.

    Then again, the solution is meaningless if you use std::merge. Try to implement the merging algorithm by yourself.


  • 0
    R

    Thanks for your remind! I have implemented this algorithm, I just seen that algorithm has this function, and I try to practice it, so in the future, if it is a subpart of my other algorithm, maybe I can call it directly for quick coding. By the way, I am still not clear about using namespace std, why std: would distinguish ,merge in solution and merge in algorithm? Thanks


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.