c ++ vector :: erase with class object iterator not erasing vector member [英] c++ vector::erase with class object iterator not erasing vector member

查看:175
本文介绍了c ++ vector :: erase with class object iterator not erasing vector member的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题与其他人类似,但我看了这里在其中并未解决这些问题我的问题。我能找到的所有内容都表明以下代码应该可行。
我用于测试的向量包含名称为superman,batman和spiderman的元素。我不能删除任何更多代码的例子来理解我不认为。

I know that this question is similar to others, but I looked here and here and neither of these solved my problem. Everything I have been able to find says that the following code should work. The vector I use for testing has elements with names superman, batman, and spiderman. I can't remove any more code for the example to make sense I don't think.

class Room{
  vector<User> users;
  users = {{superman, pass}, {batman, pass}, {spiderman, pass}};

public:
  vector<User> Room::getUsers() {return users;}
};

class User{
  string username;
  string pass;
};



    // Find the room. Once found, scan through the users
    // Once the user is found, remove them from the vector
    for(roomIt = rooms.begin(); roomIt < rooms.end(); roomIt++) {
      if (roomIt->getName().compare(roomName) == 0) {
        for (useIt = roomIt->getUsers().begin(); useIt < roomIt->getUsers().end(); useIt++) {
          if (useIt->getName().compare(username) == 0) {
            useIt = roomIt->getUsers().erase(useIt);
            const char * msg = "OK\r\n";
            write(fd, msg, strlen(msg));
            return;
          }
        }
      }
    }

room对象有一个user类型的向量。 roomIt-> getUsers()返回会议室中的用户向量。但是,最里面的if语句没有做任何事情。要调试,我有一个3用户的向量。使用gdb时,我可以看到 useIt = roomIt-> getUsers()。erase(useIt)导致迭代器从向量中的第二个元素移动到第三个元素元件。但是,矢量不会改变。预先有3个元素,之后也有3个元素。

The room object has a vector of type user. roomIt->getUsers() returns the vector of users in a room. The innermost if statement doesn't do anything, however. To debug, I have a vector of 3 Users. When using gdb, I can see that useIt = roomIt->getUsers().erase(useIt) causes the iterator to move from the second element in the vector to the third element. However, the vector does not change. There were 3 elements beforehand, and there are 3 elements afterwards as well.

推荐答案

as 在评论中推测的user2328447 您的 getUsers 按值返回,而不是引用。因此,每次调用它时,都会得到一个完全不相关的向量(相同的值,但是单独存储);甚至你的循环结构是错误的(它将 .begin()向量的一个副本与<$进行比较c $ c> .end()单独的副本。)

As user2328447 speculated in the comments your getUsers is returning by value, not reference. So every single time you call it, you get a completely unrelated vector (same values, but stored separately); even your loop construct is wrong (it's comparing the .begin() from one copy of the vector to the .end() of a separate copy).

如果你想能够改变实例的<$ c $来自调用者的c> vector ,您必须返回一个引用,更改:

If you want to be able to mutate the instance's vector from the caller, you must return a reference, changing:

vector<User> Room::getUsers() {return users;}

to:

vector<User>& Room::getUsers() {return users;}
            ^ return reference, not value

这篇关于c ++ vector :: erase with class object iterator not erasing vector member的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆