Pippo
Pippo

Reputation: 1583

C++ - Error with iteration of vector

struct user
{
    vector<size_t> follower;
    vector<size_t> following;
};

int main ()
{
    vector< user > node ( 100 );

    // Pushing back some indices of other nodes in some node[x].follower and node[x].following

    size_t i = 2;

    for ( size_t const& j : node[i].follower )
        node[j].following.erase( remove( node[j].following.begin(), node[j].following.end(), i ), node[j].following.end() );

}

In C++, I created the struct user, where vectors follower and following store indices to some other users belonging to the vector node. With the last for loop I would like to destroy all the indices i (which refer to the specific user i) from the vector follower and following of other users; unluckily, with gcc/4.7.2, using C++11, I obtain this error:

error: cannot convert 'std::vector<long unsigned int>::iterator {aka __gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int> >}' to 'const char*' for argument '1' to 'int remove(const char*)'

I also tried with a for loop of iterators, for ( vector<size_t>::iterator it = node[i].follower.begin(); it != node[i].follower.end(); ++it ), using *it in the code of the loop, but I still obtain a similar error.

I don't know what to do.

Upvotes: 1

Views: 840

Answers (1)

masoud
masoud

Reputation: 56479

#include <algorithm>

Otherwise, compiler tries to use other remove function which is for deleting files and accepts a C-style strings (By including <iostream>).

Upvotes: 8

Related Questions