gpt4 book ai didi

c++ - 如何删除 vector 中的重复字符串/数据?

转载 作者:行者123 更新时间:2023-11-28 02:28:15 25 4
gpt4 key购买 nike

已在底部编辑

如果您想知道如何执行此操作,请阅读已接受的答案,它非常有效

好吧,我这几天一直在努力解决这个问题,我已经阅读了很多人的回答,但出于某种原因,当我尝试删除下面程序中的重复项时,我一直遇到编译错误.由于我如何设置 vector ,是否有一种特殊的方法需要删除这些重复项?请帮助,我感到非常沮丧,因为我无法解决这个问题。

//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: Invalid operands to binary expression ('const Name_Sorter' and '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== 时,该函数似乎从 vector 中删除了所有重复项,而不仅仅是除了一个重复项之外的所有重复项。如果输入是“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==默认情况下。您没有像调用 std::sort 时那样传递比较函数.

要么定义一个比较器并修复对 std::unique 的调用,像这样:

bool eqComp (const Name_Sorter &t1, const Name_Sorter &t2)
{
return t1.name == t2.name;
}

info.erase(std::unique(info.begin(), info.end(), eqComp), info.end());
// include comparator ^^^^

或者,更好的是,重载 operator==适合你的类型:

bool operator== (const Name_Sorter &t1, const Name_Sorter &t2)
{
return t1.name == t2.name;
}

info.erase(std::unique(info.begin(), info.end()), info.end());

同样,你可以重载operator<让你的std::sort调用更简单:

bool operator< (const Name_Sorter &t1, const Name_Sorter &t2)
{
return t1.name < t2.name;
}

std::sort(info.begin(), info.end());

关于c++ - 如何删除 vector 中的重复字符串/数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com