C++ 模板中的比较器
我需要存储和比较不同类型的(CSearch类中的struct-CElem)。我不知道如何实现相同的迭代器,以便在函数搜索中进行比较C++ 模板中的比较器,c++,templates,iterator,C++,Templates,Iterator,我需要存储和比较不同类型的(CSearch类中的struct-CElem)。我不知道如何实现相同的迭代器,以便在函数搜索中进行比较 if (comp(*t, *it)) 这种情况下不能使用相同类型的迭代器,可以建议吗 template <typename _Type, typename _Comparator = equal_to<typename _Type::value_type> > class CSearch { public: CSea
if (comp(*t, *it))
这种情况下不能使用相同类型的迭代器,可以建议吗
template <typename _Type, typename _Comparator = equal_to<typename _Type::value_type> >
class CSearch {
public:
CSearch() : comp(_Comparator()) {
};
CSearch(const _Comparator &_cmp) : comp(_cmp) {
};
void Add(int id,
const _Type & needle);
set<int> Search(const _Type & hayHeap) const;
private:
struct CElem {
int id;
_Type seq;
};
vector<CElem> m_Data;
_Comparator comp;
};
template <typename _Type, typename _Comparator>
void CSearch<_Type, _Comparator>::Add(int id, const _Type& needle) {
CElem temp;
temp.id = id;
temp.seq = needle;
m_Data.push_back(temp);
}
template <typename _Type, typename _Comparator>
set<int> CSearch<_Type, _Comparator>::Search(const _Type & hayHeap) const {
set<int> stmp;
typename _Type::const_iterator t;
typename vector<CElem>::const_iterator it;
for (t = hayHeap.begin(); t != hayHeap.end(); ++t){
//cout << *hayHeap << endl;
for(it=m_Data.begin(); it!=m_Data.end(); ++it){
if (comp(*t, *it))
stmp.insert(it->id);
}
}
return stmp;
}
class CharComparator
{
public:
CharComparator ( bool caseSensitive = true )
: m_CaseSensitive ( caseSensitive ) { }
bool operator () ( const char & a, const char & b ) const
{ return m_CaseSensitive ? a == b : toupper (a) == toupper (b); }
private:
bool m_CaseSensitive;
};
bool upperCaseCompare ( const char & a, const char & b )
{
return toupper ( a ) == toupper ( b );
}
void printSet ( const set<int> & s )
{
for ( set<int>::const_iterator it = s . begin (); it != s . end (); ++it )
cout << ( it == s . begin () ? "" : ", " ) << *it;
cout << endl;
}
template <typename _T, int _N>
vector<_T> makeVector ( const _T (&data)[_N] )
{
return vector<_T> ( data, data + _N );
}
int main(int argc, char** argv) {
CSearch <string> test1;
test1 . Add ( 0, "hello" );
test1 . Add ( 1, "world" );
test1 . Add ( 2, "rld" );
test1 . Add ( 3, "ell" );
test1 . Add ( 4, "hell" );
printSet ( test1 . Search ( "hello world!" ) );
// 0, 1, 2, 3, 4
printSet ( test1 . Search ( "hEllo world!" ) );
// 1, 2
CSearch <string, bool (*)(const char &, const char &)> test2 ( upperCaseCompare );
test2 . Add ( 0, "hello" );
test2 . Add ( 1, "world" );
test2 . Add ( 2, "rld" );
test2 . Add ( 3, "ell" );
test2 . Add ( 4, "hell" );
printSet ( test2 . Search ( "HeLlO WoRlD!" ) );
// 0, 1, 2, 3, 4
printSet ( test2 . Search ( "hallo world!" ) );
// 1, 2
CSearch <string, CharComparator> test3 ( CharComparator ( false ) );
test3 . Add ( 0, "hello" );
test3 . Add ( 1, "world" );
test3 . Add ( 2, "rld" );
test3 . Add ( 3, "ell" );
test3 . Add ( 4, "hell" );
printSet ( test3 . Search ( "heLLo world!" ) );
// 0, 1, 2, 3, 4
printSet ( test3 . Search ( "Well, templates are hell" ) );
// 3, 4
CSearch <vector<int> > test4;
static const int needleA [] = { 1, 6, 1, 6, 9, 12 };
static const int needleB [] = { 9, 12, 7 };
static const int hayHeap [] = { 1, 6, 1, 6, 1, 6, 9, 12, 8 };
test4 . Add ( 0, makeVector ( needleA ) );
test4 . Add ( 1, makeVector ( needleB ) );
printSet ( test4 . Search ( makeVector ( hayHeap ) ) );
// 0
CSearch <vector<string> > test5;
static const string needleX [] = { "Prague", "Bern", "Rome" };
static const string needleY [] = { "London", "Prague", "Bern" };
static const string needleZ [] = { "London", "Rome" };
static const string cityHeap [] = { "Berlin", "London", "Prague", "Bern", "Rome", "Moscow" };
test5 . Add ( 0, makeVector ( needleX ) );
test5 . Add ( 1, makeVector ( needleY ) );
test5 . Add ( 2, makeVector ( needleZ ) );
printSet ( test5 . Search ( makeVector ( cityHeap ) ) );
// 0, 1
return 0;
}
模板
C类研究{
公众:
CSearch():comp(_Comparator()){
};
C搜索(常量比较器和cmp):comp(常量cmp){
};
无效添加(int-id,
常量(针型和针型);
设置搜索(常量类型&草堆)常量;
私人:
结构名人{
int-id;
_类型seq;
};
向量m_数据;
_比较器;
};
模板
void CSearch::Add(整数id、常量类型和指针){
塞勒姆温度;
temp.id=id;
温度顺序=针;
m_数据。推回(临时);
}
模板
设置CSearch::Search(const\u Type&haychep)const{
设置stmp;
typename _Type::const_迭代器t;
typename向量::const_迭代器;
对于(t=hayHeap.begin();t!=hayHeap.end();++t){
//cout最简单的方法是创建一个中间助手类型,并从这两种类型进行隐式转换。然后在其上编写一个比较函数或functor,并使用它。以下划线大写字母开头的名称保留用于实现。因此,您的程序格式不正确。为什么不直接比较*t
和it->seq
?I这是学校的作业。因为比较器不同,只传递一个指向函数的指针。为什么不让comp成为一个模板函数呢?我添加了规定的测试代码。我不能更改它,但教授更改了。就目前而言,这个答案更像是一个注释。OP不太可能从中学习到任何东西。你想用它来扩展一下吗我的代码建议?