模板中的操作符重载
operator overloading in templates
我正在尝试使用std::find算法&我的第三个参数是类型为MyPair的对象,它重载了==操作符。但问题是它没有被调用。下面是我的代码:
MyPair.h
template<class K, class V> class MyPair;
template<class K,class V>
bool operator==(const MyPair<K,V>& p1, const MyPair<K,V>& p2);
template<class K,class V>
class MyPair:public std::pair<K,V>
{
public:
MyPair(){};
MyPair(const K&x, const V& y) :pair<K, V>(x, y){ cout << "ctor"<< endl; }
template<class K, class V>
friend bool operator==<>(const MyPair<K, V>& p1, const MyPair<K, V>& p2) {
cout<<"called"; return true; }
};
另一个文件我使用的是std::find
算法
void WordVector::insert(string word){
MyPair<string, int> p(word, 1);
auto iter = find(wordvec.begin(),wordvec.end(),p);
if (iter == wordvec.end()){
wordvec.push_back(p);
}
else{
++iter->second;
}
}
如果你只是想找到一些特定的对,为什么不只是使用find_if
与适当的谓词?
E。g(演示):
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
int main() {
vector<pair<int,int> > a = { {1,1} , {1,2}, {1,3}};
auto res = find_if(a.begin(), a.end(),
[](pair<int,int> p){ return (p.first == 1 && p.second==3);});
cout<< (*res).first;
return 0;
}
以下代码适用于我的g++ 4.8.3
:
编辑:声明operator ==
并在之后定义它是相当没有意义的。因此,我把运算符的定义移到了MyPair
的定义后面。这也适用于g++
。
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
template<class K, class V> class MyPair;
template<class K,class V>
bool operator==(const MyPair<K,V>& p1, const MyPair<K,V>& p2);
template<class K,class V>
class MyPair:public std::pair<K,V>
{
public:
MyPair(){};
MyPair(const K&x, const V& y) : std::pair<K, V>(x, y){ std::cout << "ctorn"; }
friend bool operator==<>(const MyPair<K, V>& p1, const MyPair<K, V>& p2);
};
template <class K, class V>
bool operator==(const MyPair<K, V>& p1, const MyPair<K, V>& p2) {
std::cout<<"called"; return true;
}
//another file I am using std::find algo
class WordVector {
std::vector<MyPair<std::string,int> > wordvec;
public:
void insert(const std::string& word);
};
void WordVector::insert(const std::string& word){
MyPair<std::string, int> p(word, 1);
auto iter = find(wordvec.begin(),wordvec.end(),p);
if (iter == wordvec.end()){
wordvec.push_back(p);
}
else{
++iter->second;
}
}
int main() {
WordVector wv;
wv.insert("first");
wv.insert("second");
return 0;
}
/**
Local Variables:
compile-command: "g++ -std=c++11 test.cc -o test.exe && ./test.exe"
End:
*/
输出为:
g++ -std=c++11 test.cc -o test.exe && ./test.exe
ctor
ctor
called
Compilation finished at Sat Aug 9 18:31:36
与修改
template<class K,class V>
class MyPair:public std::pair<K,V>
{
public:
MyPair(){};
MyPair(const K&x, const V& y) : std::pair<K, V>(x, y){ std::cout << "ctorn"; }
template<class K, class V>
friend bool operator==<>(const MyPair<K, V>& p1, const MyPair<K, V>& p2) {
std::cout<<"called"; return true; }
};
更适合原始的我得到以下错误消息和警告:
g++ -std=c++11 test.cc -o test.exe && ./test.exe
test.cc:19:12: error: declaration of 'class K'
template <class K, class V>
^
test.cc:11:10: error: shadows template parm 'class K'
template<class K,class V>
^
test.cc:19:21: error: declaration of 'class V'
template <class K, class V>
^
test.cc:11:18: error: shadows template parm 'class V'
template<class K,class V>
^
test.cc:20:73: error: defining explicit specialization 'operator==<>' in friend declaration
friend bool operator==<>(const MyPair<K, V>& p1, const MyPair<K, V>& p2)
^
Compilation exited abnormally with code 1 at Sat Aug 9 18:37:40