在下面的C ++代码中,我想使用模板函数来确定两个向量是否完全相同,但是,我总是从模板函数中得到错误。你能给我一些关于如何从模板函数返回布尔值的建议吗? (我的C ++编译器是g ++ 4.6)
编辑:在pop_back p1 p2 p3 p4之后,结果现在与我的预期相匹配。
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
template<class T> bool areTheyMatched(shared_ptr<vector<T>> p1, shared_ptr<vector<T>> p2) {
if ((*p1).size() == (*p2).size()) {
cout << (*p1).size() << endl;
for (unsigned int i = 0; i < (*p1).size(); i++) {
if ((*p1)[i] != (*p2)[i]) {
cout << (*p1)[i] << " " << (*p2)[i] << endl;
return false;
}
}
} else {
return false;
}
cout << "All elements are exactly the same" << endl;
return true;
}
int main(int argc, char *argv[]) {
shared_ptr<vector<int>> p1(new vector<int>);
shared_ptr<vector<int>> p2(new vector<int>);
shared_ptr<vector<double>> p3(new vector<double>);
shared_ptr<vector<double>> p4(new vector<double>);
for (int i = 0; i < 10; i++)
(*p1).push_back(i);
for (int i = 0; i < 9; i++)
(*p2).push_back(i);
(*p2).push_back(11);
for (double i = 0.0; i < 9.9; i += 1.1)
(*p3).push_back(i);
for (double i = 0.0; i < 8.8; i += 1.1)
(*p4).push_back(i);
(*p4).push_back(11.0);
cout << "Case 1: " << areTheyMatched(p1, p2) << endl;
(*p1).pop_back();
(*p2).pop_back();
cout << "Case 2: " << areTheyMatched(p1, p2) << endl;
cout << "Case 3: " << areTheyMatched(p3, p4) << endl;
(*p3).pop_back();
(*p4).pop_back();
cout << "Case 4: " << areTheyMatched(p3, p4) << endl;
p1.reset();
p2.reset();
p3.reset();
p4.reset();
return 0;
}
答案 0 :(得分:6)
模板代码看起来很好,但测试向量从不相同,是不是。
首先,他们中的一个具有元素11,并且当移除它们时它们具有不同的尺寸。
答案 1 :(得分:2)
你的矢量真的与众不同。
for (int i = 0; i < 10; i++)
(*p1).push_back(i);
for (int i = 0; i < 9; i++)
(*p2).push_back(i);
(*p2).push_back(11);
p1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
p2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 11}
Case 1: false
(*p2).pop_back();
p1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
p2 = {0, 1, 2, 3, 4, 5, 6, 7, 8}
Case 2: false
答案 2 :(得分:1)
你的模板功能完全正常......
你得到错误的原因是因为你的输入真的不同,最初是在内容上,并且在弹出后甚至大小都不匹配。
你有没有理由使用shared_ptr?你可以传递矢量&amp;除非你有充分理由需要共享指针,否则你不应该使用它们。虽然当您需要跨多个实体共享数据时,它们是传递指针的最佳方式,但它们会产生开销。
答案 3 :(得分:0)
如果您检测代码,您将看到它正常工作。我需要使用std::tr1
和<tr1/memory>
以及> >
代替>>
来使用G ++ 4.6.1进行编译,但这显示了它的工作原理:
#include <iostream>
#include <tr1/memory>
#include <vector>
using namespace std;
using namespace std::tr1;
template<class T> bool areTheyMatched(shared_ptr< vector<T> > p1, shared_ptr< vector<T> > p2)
{
cout << "p1.size: " << (*p1).size() << ", p2.size: " << (*p2).size() << endl;
if ((*p1).size() != (*p2).size())
return false;
for (unsigned int i = 0; i < (*p1).size(); i++)
{
if ((*p1)[i] != (*p2)[i])
{
cout << "i = " << i << ": " << (*p1)[i] << " " << (*p2)[i] << endl;
return false;
}
}
cout << "All elements are exactly the same" << endl;
return true;
}
int main()
{
shared_ptr< vector<int> > p1(new vector<int>);
shared_ptr< vector<int> > p2(new vector<int>);
shared_ptr< vector<double> > p3(new vector<double>);
shared_ptr< vector<double> > p4(new vector<double>);
for (int i = 0; i < 10; i++)
(*p1).push_back(i);
for (int i = 0; i < 9; i++)
(*p2).push_back(i);
(*p2).push_back(11);
for (double i = 0.0; i < 9.9; i += 1.1)
(*p3).push_back(i);
for (double i = 0.0; i < 8.8; i += 1.1)
(*p4).push_back(i);
(*p4).push_back(11.0);
cout << "Case 1: " << areTheyMatched(p1, p2) << endl;
(*p2).pop_back();
cout << "Case 2: " << areTheyMatched(p1, p2) << endl;
(*p1).pop_back();
cout << "Case 2a: " << areTheyMatched(p1, p2) << endl;
cout << "Case 3: " << areTheyMatched(p3, p4) << endl;
(*p3).pop_back();
cout << "Case 4: " << areTheyMatched(p3, p4) << endl;
(*p4).pop_back();
cout << "Case 4a: " << areTheyMatched(p3, p4) << endl;
p1.reset();
p2.reset();
p3.reset();
p4.reset();
return 0;
}
p1.size: 10, p2.size: 10
i = 9: 9 11
Case 1: 0
p1.size: 10, p2.size: 9
Case 2: 0
p1.size: 9, p2.size: 9
All elements are exactly the same
Case 2a: 1
p1.size: 10, p2.size: 10
i = 9: 9.9 11
Case 3: 0
p1.size: 9, p2.size: 10
Case 4: 0
p1.size: 9, p2.size: 9
All elements are exactly the same
Case 4a: 1