none
Remove elements from another vector similar to first vector through iterate RRS feed

  • Вопрос

  • I have vector of tuples std::vector <std::tuple <int, int, int>> passengers and vector of tuples active_users. (it may be another type)


    I copy elements from passengers to active_users in one condition and do it iterating through iterator (maybe the counter).

    I want to delete copied element from original  vector passengers while iterating.

    But when  I erase(). the first element becomes 0 element and indexes are ruined. and then I can`t delete passenger[1] for example.

    How can I do it?

    Elevator can hold users with same direction if user 1 goes from 8 to 14 then Elevator can lift another user from 10 to 18.
        std::vector <std::tuple <int, int, int>> passengers; // user_ID, user_floor; destination floor

     std::vector <std::tuple <int, int, int>> active_users;     std::get<1>(passengers[0]) = 8; std::get<2>(passengers[0]) = 14;
        std::get<1>(passengers[1]) = 10; std::get<2>(passengers[1]) = 18;
        std::vector <std::tuple <int, int, int>>::iterator k;

        for (auto k = passengers.begin(); k != passengers.end(); ++k)
        
        {
            
            if (direct[std::distance(passengers.begin(), k)]....) cond)
            {
                Elevation::active_users.push_back({ userid,cur_floor,dst_queue });
                k = passengers.erase(k); // not erase passenger[0]
                ------------------
            }


            if ((direct[std::distance(passengers.begin(), k)]....  cond)
            {
                Elevation::active_users.push_back({ userid,cur_floor,dst_queue });
                 k = passengers.erase(k);
                ----
            }

        }
       



    • Изменено Streamc 21 июня 2019 г. 9:27
    21 июня 2019 г. 9:24

Все ответы

  • I have vector of tuples std::vector <std::tuple <int, int, int>> passengers and vector of tuples active_users. (it may be another type)
    I want to delete copied element from original  vector passengers while iterating.
    But when  I erase(). the first element becomes 0 element and indexes are ruined. and then I can`t delete passenger[1] for example.
    How can I do it?

    I think you may be looking for the erase-remove idiom: https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom

    Dave

    21 июня 2019 г. 10:21
  • OK, let`s try. How to do it when instead of integer I have vector of tuples.

    passengers.erase(std::remove(passengers.begin(), passengers.end(), 5), passengers.end());

    would erase integer 5 for <int> vec

    But instead of <int> 5 I have <int, int, int> tuple. And I want to delete passengers where

     std::get<2>(passengers[k])== std::get<2>(active_users[k]) I mean copy to active users and delete. This is condition. Not simply delete 5 or another

    21 июня 2019 г. 12:07
  • OK, I try just simple condiiton second element <4

        for (auto k = passengers.begin(); k != passengers.end();++k )


            {      

                passengers.erase(std::remove(passengers.begin(), passengers.end(), [](const std::vector< std::tuple<int, int, int> >& x) {return  std::get<2>(x[2]) <2; }), passengers.end());
            }
    Severity    Code    Description    Project    File    Line    Suppression State
    Error    C2678     binary '==': no operator found which takes a left-hand operand of type 'std::tuple<int,int,int>' (or there is no acceptable conversion)    Elevation    C:\Program Files




    • Изменено Streamc 21 июня 2019 г. 12:54
    21 июня 2019 г. 12:40
  • >Error    C2678     binary '==': no operator found which takes a left-hand operand of type 'std::tuple<int,int,int>' (or there is no acceptable conversion)

    Presumably you need to have an appropriate operator for the type.
    I'd write a class for your type rather than use a tuple, and add the operator to that class.

    Dave

    23 июня 2019 г. 22:41