如何从具有用户类参数的向量中删除元素?

Posted

技术标签:

【中文标题】如何从具有用户类参数的向量中删除元素?【英文标题】:How to erase an element from my vector that has user class parameters? 【发布时间】:2015-11-21 00:47:02 【问题描述】:

所以我的工作是描述函数,我正在研究void erase() 函数,该函数应该删除给定字符串名称的向量元素。老师只给了我们用于电话簿作业的原型。他还给了我们一个内置的main(),我们不允许更改他的main() 或原型,只能描述它们。 首先列出的是原型,然后是我的工作,然后是主要的。

原型

    #ifndef PHONEBOOK_H
    #define PHONEBOOK_H
    #include <string>
    #include <vector>
    using namespace std;

    class Person
    
    public:
        Person();
        Person(string new_name, int new_phone);
        string get_name() const;
        int get_phone() const;
        bool operator < (Person p) const;
        void print() const;

    private:
        string name;
        int phone;
    ;

    void add_people(vector<Person> &phone_book);
    void erase(vector<Person> &phone_book, string name);
    void sort(vector<Person> &phone_book);
    void shuffle(vector<Person> &phone_book);
    void reverse(vector<Person> &phone_book);
    void print(vector<Person> &phone_book);
    int lookup(const vector<Person> &phone_book, string name);
    #endif

我的作品

        #include <iostream>
        #include <string>
        #include <vector>
        #include <time.h>
        #include "phonebook.h"

        Person::Person()
        
            name = "NONE";
            phone = 0000000;

        
        Person::Person(string new_name, int new_phone)
        
            name=new_name;
            phone=new_phone;
        
        string Person::get_name() const
        
            return name;
        
        int Person::get_phone() const
        
            return phone;
        
        bool Person::operator < (Person p) const
        
            return name < p.name;
        
        void Person::print() const
        
            cout << endl << name << " " << phone;
        

        void add_people(vector<Person> &phone_book)
        
            cout << "Please enter the new name: ";
            string s;
            getline(cin, s);
            cout << "Please enter new number: ";
            int number;
            cin >> number;
            phone_book.push_back(Person(s,number));
        
        void erase(vector<Person> &phone_book, string name)
           
        
        void sort(vector<Person> &phone_book)
        
        
        void shuffle(vector<Person> &phone_book)
        
        
        void reverse(vector<Person> &phone_book)
        
        
        void print(vector<Person> &phone_book)
        
            for(int i=0; i < phone_book.size(); i++)
                cout << phone_book[i] << " ";
        

        int lookup(const vector<Person> &phone_book, string name)
        
                int i = 0;
                while (i < phone_book.size() && phone_book[i].get_name() != name)
                
                    i++;
                    return phone_book[i].get_phone();
                
        

主要

#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"

using namespace std;




int main()


    vector<Person> phone_book;
    string name;
    int number;
    int answer;

    srand((int)(time(0)));

    phone_book.push_back(Person("Bruin, Joe", 5556456));
    phone_book.push_back(Person("Simpson, Homer", 5557471));
    phone_book.push_back(Person("Duffman, Barry", 5533331));


    cout <<"\n";
    cout << "Your phone book contains the following names and numbers: \n";

    for (int i=0; i < phone_book.size(); i++)
    

        phone_book[i].print();
        cout << "\n";
    
    cout <<"\n";
    answer=0;

    while (answer != 8)
    
        cout << "\nChoose from the following options:\n\n";

        cout << "1) Add people to the phone book.\n";
        cout << "2) Erase a person from the phone book.\n";
        cout << "3) Sort the phone book.\n";
        cout << "4) Shuffle the phone book.\n";
        cout << "5) Reverse the phone book.\n";
        cout << "6) Print the phone book.\n";
        cout << "7) Look up a person in the phone book.\n";
        cout << "8) Quit.\n\n";

        cin >> answer;

        string clear;

        getline(cin, clear);

        if (answer == 1)
            add_people(phone_book);
        else if (answer == 2)
        
            cout << "Enter a name: ";
            getline(cin, name);
            erase(phone_book, name);

        
        else if (answer == 3)
            sort(phone_book);
        else if (answer == 4)
            shuffle (phone_book);
        else if (answer == 5)
            reverse(phone_book);
        else if (answer == 6)
        
            cout <<"\n";
            cout << "Your phone book contains the following names and numbers: \n";
            print(phone_book);
        
        else if (answer ==7)
                           
            cout << "Enter a name: ";
            getline(cin, name);
            int number = lookup(phone_book, name); 
            if (number > 0)
            
                cout << "\n\nThe number for " << name << " is: " << number << "\n\n";
            
            else
                cout << name << " not found in the phone book.\n";
        
    
    return 0;

【问题讨论】:

请提供mcve。 如果你们可以给我关于排序函数的指针,它应该根据它们各自的字符串参数按字典顺序排列向量的元素 【参考方案1】:

您的查找函数查找具有给定名称的人在向量中的位置。您可以使用此索引以及 vector erase function 从向量中删除 Person。

顺便说一句,在您的查找功能中,您可能还想考虑在没有给定名称的人的情况下会发生什么。

【讨论】:

以上是关于如何从具有用户类参数的向量中删除元素?的主要内容,如果未能解决你的问题,请参考以下文章

从向量中删除元素时如何减小向量的大小?

如何从向量中随机删除一个元素?

从向量中删除取消引用的元素

有没有更好的替代 std::remove_if 从向量中删除元素的方法?

如何将元素添加到指针向量中?

使用模板时如何从 std::vector 中删除元素?