一个简单的电话簿程序将联系人推回矢量

a simple phonebook program push back contacts into the vector

本文关键字:联系人 程序 电话簿 一个 简单      更新时间:2023-10-16

我正在编写C 中的简单电话簿程序不使用类。我有一个添加联系人的函数。我想知道为什么它不起作用!如果您能帮助我,它不会将联系人推回矢量,我将非常感谢。包括我的代码的平静。

    vector<ContactInfo> add(vector<ContactInfo> contacts, ContactInfo ci){
if(!(isRepetativeContact(contacts, ci)))
    contacts.push_back(ci);
return contacts;
}

这是" isrepetativecontact"函数:

    bool isRepetativeContact(const vector<ContactInfo>& contacts, const ContactInfo& ci){
for(int i = 0 ; i < contacts.size() ; i++)
    if((contacts.size() != 0) && (contacts[i] == ci))
        return true;
return false;
}

,我将contactInfo结构的操作员超载:

    bool operator==(const ContactInfo& ci) const {
    return (firstName == ci.firstName && lastName == ci.lastName &&
     phoneNumber == ci.phoneNumber && emailAddress == ci.emailAddress &&
     id == ci.id );
}

似乎您正在使用std :: vector重新创建std :: set。尝试使用std :: Set

std::pair<iterator,bool> insert( const value_type& value );

插入的返回值是一对。布尔指示该值是否已经在集合中;(插入成功(。迭代器指向std :: set中的元素(如果值已经在集合中,则指向现有值(

您不能在集合中重复。