C++ 如何访问和更改映射中对象的成员数据?

标签 c++ c++11 object dictionary iterator

我在 map 中插入了字符串和 Person 对象。

map <string,Person> _userList;

但是,在我的方法中,当我尝试在 Person 类中添加某些内容或更改其在 map 中的成员数据时,我无法再访问它。那是因为当我使用迭代器并通过打印来测试它时 i->second,i所指向的Person类有BLANK信息。这些 Person 对象的所有信息都被删除(或者我认为是这样)。 我真的不明白为什么。 我的目标是打印出 map 中 Person 对象的列表(成员数据)。当我遇到这个问题时我无法做到这一点,因为它会打印出空白。

以下是在 map 中插入字符串和 Person 对象的代码方法:

void SocialNetwork::createPerson(string firstName, string lastName)
{
    string fullName = firstName + " " + lastName;
    //checks if the name is NOT A DUPLICATE
    //iterate through _userList. _userList is a map
    //if map is empty
    if (_userList.empty())
    {
        _user = new Person(firstName, lastName);
        _userList.insert(make_pair(fullName, *_user));
        _numUsers++;
    }
    else
    {
        bool matchFound = false;
        //USE MAP COUNT TO SEE IF THE PERSON ALREADY EXISTS IN THE MAP
        if(_userList.count(fullName)>0)
        {
            matchFound = true;
            cout << "Error: Name already exists" << endl;
        }
        else
        {
            _user = new Person(firstName, lastName);
            _userList.insert(make_pair(fullName, *_user));
            _numUsers++;
        }
    }
}

下面是尝试打印下面 Person 类中的列表的方法的示例代码:

void SocialNetwork::listPending(string personsFirst, string personsLast)
{
    //prints list of pending Friend Requests
    string personsFullName = personsFirst + " " + personsLast;
    //check if this user exists
    bool userExists = false;
    if (_userList.empty())
    {
        cout << "Error: Person does not exist" << endl;
    }
    else
    {
        if(_userList.count(personsFullName)>0)
        {
            userExists = true;
        }
        if (!userExists)
        {
            cout << "Error: Person does not exist" << endl;
        }
        else
        {
            map<string,Person>::iterator i = _userList.begin();
            bool personFound = false;
            while (!personFound)
            {
                if(i != _userList.end())
                {
                    if(i->first == personsFullName)
                    {
                        //PROBLEM IS HERE
                        personFound = true;
                        cout << i->second <<endl; //Test Code. Delete later. How come their Person class is left BLANK?
                        i->second.printPendingRequestList(); //How come the list is left BLANK?
                    }
                    i++;
                }
            }
        }
    }
}

这是 Person 类中的 printPendingRequestList 方法:

void Person::printPendingRequestList()
{
    string test = _fullName;
    cout << _fullName << "'s pending list" << endl;
    queue<Person> toPrintQueue = _friendRequests;

    while (!toPrintQueue.empty())
    {
        cout << toPrintQueue.front().getFullName() << endl;
        toPrintQueue.pop();
    }
    cout << endl;
}

这是我收到的输出,它应该包含此人的名字、姓氏和所有其他信息,但没有,它保留默认值或空白:

First Name:
Last Name:
Full Name:
Number of Friends: 0
Number of people they blocked: 0
Friend Requests:


Friend List:

Block List:

Personal Message List:

's pending list

请帮忙

最佳答案

你的整个createPerson函数写得很糟糕。首先,它动态创建一个对象并立即释放指向它的指针 - 从而产生内存泄漏。其次,它不必要地重复代码。这是一个更好的版本:

void SocialNetwork::createPerson(string firstName, string lastName)
{
    string fullName = firstName + " " + lastName;
    auto it = _userList.find(fullName);
    if (it != _userList.end() {
        _userList.emplace(fullName, firstName, lastName);
        _numUsers++;
    } else {
        cout << "Error: Name already exists" << endl;
    }
}

关于C++ 如何访问和更改映射中对象的成员数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058058/

相关文章:

c++ - 动态分配数组的地址会根据数组的声明方式而变质吗?

c++ - 负无穷大

c++ - 为什么使用默认参数?

c++ - 关于取消引用和地址空间的基本 C++ 指针问题

c++ - 保证静态对象的静态(常量)初始化

C++静态与线程存储持续时间销毁顺序

java - Java 代码中 JRuby 对象的序列化

c++ - 在 C++ 中二进制搜索具有给定元素值的元组

javascript - React 从本地存储导入图像

Java 多线程对象