I have written a program to get the reference count of an object from vector. I am storing the objects in list with there count from vector. I dont want to store them in sorted order thats why I am using a list to store. but while compilation this failing with error
"‘Item’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’".
I am not sure about error. Dear friends, can you please answer me with below questions.
- How to fix this error?
- How I can use any container which will store them with theirs reference count but not in sorted order but their arrival order in vector? Please use below link to view source file.
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include<bits/stdc++.h>
using namespace std;
class Item
{
public:
int count;
string name;
Item(){};
Item(string str, int cnt)
{
name = str;
count = cnt;
};
};
string func(vector<string>& vec)
{
list<Item> lst;
list<Item>:: iterator it;
for(int i=0; i<vec.size(); i++)
{
it = find(lst.begin(), lst.end(), vec[i]);
if(it != lst.end())
{
it->count++;
}
else
{
lst.push_back({vec[i], 1});
}
}
for(it = lst.begin(); it != lst.end(); it++)
{
if(it->count == 1)
return it->name;
}
return "";
}
int main()
{
vector <string> vec = {"saint", "catacana", "saint", "ant"};
cout<<"String is "<<func(vec);
return 0;
}