MpcHAG
MpcHAG

Reputation: 19

no match for ‘operator+’ (operand types are ‘std::vector’ and ‘std::vector::size_type {aka long unsigned int}’)

I can't find a way to apply the size( ) function.

I have the following output:

main.cpp:13:23: error: no match for ‘operator+’ (operand types are ‘std::vector’ and ‘std::vector::size_type {aka long unsigned int}’)
         sort(arr, arr + arr.size());

in the following code :

vector<int> removeDuplicates2(vector<int> arr)
{
    if(arr.size() == 0) 
    {
        return {};
    } else 
    {
       sort(arr, arr + arr.size());
    }
    vector<int> noDuplicatesArr; 
    noDuplicatesArr.push_back(arr[0]);
    for(int i = 1; i < arr.size(); i++)
    {
        if(arr[i] != arr[i-1])
        noDuplicatesArr.push_back(arr[i]);
    }
return noDuplicatesArr;
}

all code comes from geeksforgeeks.org and edcuba.com documentation so I do not know where to find a solution next; I've tried to set a new value n

vector<int> n = sizeof(arr) / sizeof(arr[0]);
OR
<int> n = sizeof(arr) / sizeof(arr[0]); 

and I get

main.cpp:6:33: error: conversion from ‘long unsigned int’ to non-scalar type ‘std::vector’ requested
     vector<int> n = sizeof(arr) / sizeof(arr[0]);
OR
main.cpp:13:23: error: no match for ‘operator+’ (operand types are ‘std::vector’ and ‘int’)
         sort(arr, arr + n);

Upvotes: 0

Views: 3736

Answers (2)

ScaledLizard
ScaledLizard

Reputation: 181

In some cases it could be faster to use a std::set for deduplicating. That would reduce the data moved, because duplicates never get inserted into the temporary data structure. Then, removeDuplicates2 should get a const reference to eleminate an unnecessary copy of the entire array. Ideally, you could return the constructed set instead of a new vector. In case removeDuplicates2 ever would have to handle different data types, it should be turned into a template.

The resulting function is quite simple:

template <typename T>
std::vector<T> removeDuplicates2(const std::vector<T>& arr)
{
    if(arr.size() == 0) 
    {
        return {};
    }

    // Use a set to remove the duplicates
    std::set<int> set(arr.begin(), arr.end());

    // Copy result back to different container
    std::vector<int> result(set.begin(), set.end());

    return result;
}

We could even abstract from the fact a vector is turned in by passing iterators instead. If it turns out the set-based approach is slowed by the allocation strategy, try a custom allocator: Alternative for std::set without memory reallocation?

Upvotes: 0

Nathan Pierson
Nathan Pierson

Reputation: 5565

sort takes a pair of iterators as its arguments.

If arr was, as the name suggests, a C-style array, then after array-to-pointer decay arr and arr + N (where N is the number of elements in the array) would be pointers to the beginning and the past-the-ending of the array. A pointer is an iterator, so that would be a valid pair of arguments to pass to sort.

However, arr is not a C-style array, it is a std::vector<int>. So arr is not an iterator, and arr + arr.size() is attempting to add a size to a container and that's just not something that has any meaning. Thankfully, std::vector does have member functions for its iterators: begin and end. So instead you can write

std::sort(arr.begin(), arr.end());

Upvotes: 3

Related Questions