0

I have this recursive function, which does not compile giving errors at vector concatenation lines.

    vector<int> binaryInsert(vector<int> subArray, int subArraySize, int nextVal) {
    if(subArraySize == 1)
    {
        if(nextVal >= subArray[0]) subArray.insert(subArray.begin()+1, nextVal);
        else subArray.insert(subArray.begin(), nextVal);
        return subArray;
    }
    else if(subArraySize == 2)
    {
        if(nextVal >= subArray[0]) {
            if(nextVal >= subArray[1]) subArray.insert(subArray.begin()+2, nextVal);
            else subArray.insert(subArray.begin()+1, nextVal);
        }
        else subArray.insert(subArray.begin(), nextVal);
        return subArray;
    }
    else
    {
        if(subArraySize%2 == 1)
        {
            int halfPoint = (subArraySize-1)/2;
            vector<int> leftSubArray(subArray.begin(), subArray.begin()+halfPoint);
            vector<int> rightSubArray(subArray.begin()+halfPoint, subArray.end());
            vector<int> newVec;
            if(nextVal <= leftSubArray[halfPoint-1]) {
                newVec = binaryInsert(leftSubArray, halfPoint, nextVal);
                return newVec.insert(newVec.end(), rightSubArray.begin(), rightSubArray.end());
            }
            else
            {
                newVec = binaryInsert(rightSubArray, halfPoint+1, nextVal);
                return leftSubArray.insert(leftSubArray.end(), newVec.begin(), newVec.end());
            }
            
        }
        else
        {
            int halfPoint = (subArraySize)/2;
            vector<int> leftSubArray(subArray.begin(), subArray.begin()+halfPoint);
            vector<int> rightSubArray(subArray.begin()+halfPoint, subArray.end());
            vector<int> newVec;
            if(nextVal <= leftSubArray[halfPoint-1]) {
                newVec = binaryInsert(leftSubArray, halfPoint, nextVal);
                return newVec.insert(newVec.end(), rightSubArray.begin(), rightSubArray.end());
            }
            else
            {
                newVec = binaryInsert(rightSubArray, halfPoint, nextVal);
                return leftSubArray.insert(leftSubArray.end(), newVec.begin(), newVec.end());
            }
        }      
    }  
}

The line return newVec.insert(newVec.end(), rightSubArray.begin(), rightSubArray.end()); gives below error.

could not convert ‘newVec.std::vector::insert<__gnu_cxx::__normal_iterator<int*, std::vector > >(__gnu_cxx::__normal_iterator<const int*, std::vector >(newVec.std::vector::end()), rightSubArray.std::vector::begin(), rightSubArray.std::vector::end()’ from ‘std::vector::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector >}’ to ‘std::vector’

Same for return leftSubArray.insert(leftSubArray.end(), newVec.begin(), newVec.end()); too.

I am certain the method I use for vector concatenation is the most reliable and better method, yet I can't figure out what causes the error. Is there something wrong I'm doing here?

1 Answer 1

5

Calling std::vector::insert returns an iterator, not the modified vector. So the return statement will try to convert an iterator to a vector, giving the compiler error that you see.

You can solve this by simply breaking up the return statement into 2 lines:

newVec.insert(newVec.end(), rightSubArray.begin(), rightSubArray.end());
return newVec;

You need to do a similar thing for your other return statements as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly. I did that but even then my stupid code editor kept showing errors, when I should have compiled again. Thanks it works.
@wr93_ No problem. In general, don't rely on your editor to give you errors. c++ is a difficult language to parse, and you should leave that up to the compiler :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.