【发布时间】:2015-04-22 09:47:49
【问题描述】:
在底部编辑
如果您想知道如何做到这一点,请阅读接受的答案,它可以完美运行
好的,所以我这几天一直在尝试解决这个问题,我已经阅读了很多人的答案,但是由于某种原因,当我尝试在下面的程序中删除重复项时,我不断收到编译错误.由于我如何设置矢量,是否有一种特殊的方法需要删除这些重复项?请帮忙,我非常沮丧,我无法弄清楚这一点。
//libraries
#include <iostream>
#include <string>
#include <set>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
//class
class Name_Sorter
{
private:
//none
public:
//Name_Sorter();
//~Name_Sorter();
std::string name;
void get_Names(std::string person_Name ){ name = person_Name; }
void output_Names();
};
//get the user file
std::string get_File()//get input file
{
std::ifstream fin;
std::string file_To_Open;
std::cout << "What is the name of the file where you have stored the names? ";
getline(std::cin, file_To_Open);
//std::cout << file_To_Open; // for testing
return file_To_Open;
}
//output
void Name_Sorter::output_Names()
{
std::cout << "Name: " << name << std::endl;
}
//sort
bool comp(const Name_Sorter &t1, const Name_Sorter &t2) //definition
{
return t1.name < t2.name;
}//compare function
//main program
int main(int argc, const char * argv[])
{
//variables and vector
std::vector<Name_Sorter> info;
std::string names;
std::string file_To_Open;
std::ifstream fin;
int nameCounter = 0;
Name_Sorter *name_Data;
//get the file
file_To_Open = get_File();
fin.open(file_To_Open.c_str());
if (!fin.good()) throw "I/O Error";
//get name
while(!fin.eof())
{
fin >> names;
fin.ignore(1000, 10);
name_Data = new Name_Sorter;
name_Data -> get_Names(names);
info.push_back(*name_Data);
delete name_Data;//MM
nameCounter++;
}//get names
fin.close();
//sorting through the vector by name
std::sort(info.begin(), info.end(), comp);
//delete duplicates ****Seems to be a problem here****
info.erase(std::unique(info.begin(), info.end()), info.end());
std::vector<Name_Sorter>::iterator iter;
//transverse vector for output
for ( iter = info.begin(); iter != info.end(); ++iter)
{
/*for(int i = 0; i < nameCounter; i++)
{
erase(info.begin(), info.end(), info.end())
}*/
iter -> output_Names();
}//output
return 0;
}//main
这是错误信息:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:658:97: 二进制表达式的无效操作数('const Name_Sorter' 和 'const Name_Sorter')
以及错误消息链接到的位置:
template <class _T1>
struct __equal_to<_T1, _T1>
{
_LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
};
好的,所以我不再收到错误消息,但是当我按照建议添加 operator== 时,该函数似乎从向量中删除了所有重复项,而不仅仅是除一个之外的所有重复项。如果输入是“Hunter, Hunter, Hunter, Toby, Diane, Kiera”,我希望它输出“Diane, Hunter, Kiera, Toby”,现在它只会输出“Diane, Kiera, Toby”
bool operator== (const Name_Sorter &t1, const Name_Sorter &t2)
{
return t1.name < t2.name;
}
希望这最终可以帮助更多试图学习如何做到这一点的人,而不仅仅是我。
【问题讨论】:
-
std::unique默认调用operator==来比较元素。您需要为 _T1& 定义它 -
请参阅 TartanLlama 的更新答案。您的
operator==应该比较t1.name == t2.name以实现平等。 -
你的
operator==这样做:t1.name < t2.name- 这不是一个相等运算符。你所拥有的是一个小于运算符。 -
是的,我看到了,谢谢大家!这几天我真的很困惑。最初的任务是为一个类和手动遍历数组,我可以做得很好,但我总是想了解更多,所以我决定尝试自己使用一个向量。即使我不明白为什么,它现在也能完美运行——我将在明天/接下来的几个月里阅读运营商及其功能。
标签: c++ vector data-storage