简体   繁体   English

对于 exC++ 将迭代器指向的对象插入到列表中

[英]For exC++ inserting into a list the object being pointed to by an iterator

let's say that you have an iterator pointing to an object in a list and you want to write a function that will copy whatever object the iterator is pointing to into another specified list.假设您有一个迭代器指向列表中的一个对象,并且您想编写一个函数来将迭代器指向的任何对象复制到另一个指定的列表中。 is this possible?这可能吗?

for example例如

 std::list<Customer> customer;
 for (customer=customers.begin(); customer!=customers.end(); ++customer){
     std::list<Inventory>::iterator tool;
     for (tool=inventory.begin(); tool!=inventory.end(); ++tool){
          tool->addToWaitlist( customer );
      }
 }

where在哪里

 void addToWaitlist(std::list<Customer>::iterator customer){ 
            std::list<Customer>::iterator person;
            if (!wait_list.empty()){
                    for (person=wait_list.begin(); person!=wait_list.end(); ++person){
                            // Add customer with respect to time stamp
                            //if ( customer.getTime() <= person->getTime() ){
                            //      wait_list.insert( person, customer );
                            }
                    }
            } else {
                    // Add first person to wait list
                    wait_list.push_back( customer );
            }
    }

It's pretty straight forward:这很简单:

// given some T and the following lists
list<T> l1, l2;
list<T>::iterator objit=l1.begin();    // picked the first item in l1 since it's easy

// copy the item to l2
l2.push_back(*objit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM