1
我提供了一個函數對象,find_name
,以搜索匹配name
特定的對象,但事實證明,我做錯了什麼:查找自定義對象
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct Item
{
Item() : name(), id(), value() { }
Item(std::string n, int i, double v) : name(n), id(i), value(v) { }
Item(const Item& src) : name(src.name), id(src.id), value(src.value) { }
Item& operator= (const Item& src)
{
Item temp(src);
swap(temp);
return *this;
}
void swap(Item& src)
{
std::swap(src.name, name);
std::swap(src.id, id);
std::swap(src.value, value);
}
std::string name;
int id;
double value;
};
//--------------------------------------------------------------------
void swap(Item& lhs, Item& rhs) { lhs.swap(rhs); }
//--------------------------------------------------------------------
struct find_name
{
find_name(const std::string& t) : target(t) { }
bool operator() (const Item& src) const { return src.name == target; }
std::string target;
};
//--------------------------------------------------------------------
void erase (std::vector<Item>& v, const std::string& name)
{
std::vector<Item>::iterator found = std::find(v.begin(), v.end(), find_name(name));
//std::vector<Item>::iterator found = std::find(v.begin(), v.end(), [name] (const Item& src) { return src.name == name; });
if (found != v.end())
{
v.erase(found);
}
else
{
std::cout <<"\nName: "<< name <<" not found!\n";
}
}
//--------------------------------------------------------------------
int main()
{
std::vector<Item> items;
items.push_back(Item("horse shoe", 99, 12.34));
std::string name("horse shoe");
erase(items, name);
return 0;
}
是什麼在find
功能的問題?
按照要求:錯誤消息:
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Value = const find_name]’:
/usr/include/c++/6/bits/stl_algo.h:120:14: required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const find_name>]’
/usr/include/c++/6/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const find_name>]’
/usr/include/c++/6/bits/stl_algo.h:3792:28: required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Tp = find_name]’
prog.cpp:77:83: required from here
/usr/include/c++/6/bits/predefined_ops.h:199:17: error: no match for ‘operator==’ (operand types are ‘Item’ and ‘const find_name’)
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:856:5: note: candidate: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:856:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/predefined_ops.h:199:17: note: ‘Item’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:863:5: note: candidate: template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:863:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/predefined_ops.h:199:17: note: ‘Item’ is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/6/bits/c++allocator.h:33:0,
from /usr/include/c++/6/bits/allocator.h:46,
from /usr/include/c++/6/string:41,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/ext/new_allocator.h:139:5: note: candidate: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
^~~~~~~~
/usr/include/c++/6/ext/new_allocator.h:139:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/predefined_ops.h:199:17: note: ‘Item’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
編譯和運行代碼時會發生什麼?你有任何錯誤?如果是這樣,他們是什麼?如果不是,輸出是什麼,它與你想要的有什麼不同? –
@ Code-Apprentice我在最後提供了一個鏈接。它工作嗎? – Ziezi
請直接在這裏顯示輸出。 –