At the line temp.insert(it); I keep getting the error No matching member function for call to insert, which means that at some point in my code temp became something other than a vector. anybody have any ideas?
#include <iostream>
#include <vector>
using namespace std;
vector<int> removeDuplicates(vector<int> stuff)
{
vector<int> temp;
vector<int>::iterator it;
for(vector<int>::size_type i = 0; i < stuff.size(); i++){
//iterator to find stuff
it = find(stuff.begin(), stuff.end(), stuff);
//if it not in temp then add it
if (it == temp.end()) {
temp.insert(it); //No matching member function for call to insert
}
}
return temp;
}
int main(int argc, const char * argv[])
{
vector<int> values;
for(int i = 0; i < 5; i++){
values.push_back(i);
values.push_back(i+1);
}
removeDuplicates(values);
return 0;
}
well the answers helped me out a lot, but I decided to rewrite the code and here is what I got and it seems to work pretty well
#include <iostream>
#include <vector>
using namespace std;
vector<int> removeDuplicates(vector<int> oldvector)
{
// newvector to hold non-duplicates
vector<int> newvector;
vector<int>::iterator it;
for(vector<int>::size_type i = 0; i < oldvector.size(); i ++){
//If it find a value then it returns the first element
it = find(newvector.begin(),newvector.end(), oldvector[i]);
if(it == newvector.end()){
newvector.push_back(oldvector[i]);
}
}
return newvector;
}
int main(int argc, const char * argv[])
{
vector<int> values;
vector<int> newvalues;
for(int i = 0; i < 5; i++){
values.push_back(i);
values.push_back(i+1);
}
newvalues = removeDuplicates(values);
//Print out the vector without duplicates
for(vector<int>::size_type i = 0; i < newvalues.size(); i ++){
cout << newvalues[i] << "\t";
}
return 0;
}
Thanks for the help with the original question, i learned a lot.
The three (pre-C++11) valid signatures for
std::vector::insertare:You chose invalid option #4:
That option does not exist, you’ll have to chose from one of the three listed above.
On a side note (as ildjarn pointed out), C++11 adds more… and takes a few away. Regardless, none of them would be valid the way you are calling it. See: http://en.cppreference.com/w/cpp/container/vector/insert