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

时间:2015-10-05 21:09:53

标签: c++ list iterator

让我们假设您有一个指向列表中对象的迭代器,并且您希望编写一个函数来将迭代器指向的任何对象复制到另一个指定的列表中。这有可能吗?

例如

 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 );
      }
 }

其中

 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 );
            }
    }

1 个答案:

答案 0 :(得分:2)

这很直接:

'use strict';
var express = require('express');
var app = express();
var server = app.listen(80);
var io = require('socket.io')(server);
app.use(express.static('public'));

io.on('connection', function(socket) {
    console.log("socket connected: ", socket.id);

    // list all connected sockets
    var list = io.sockets.sockets;
    console.log("Connected sockets:");
    list.forEach(function(s) {
        console.log("    socket.id = ", s.id);
    });

    socket.on('join', function(name) {
        socket.join(name);

        // list all rooms
        console.log("Active rooms:");
        var rooms = io.sockets.adapter.rooms;
        for (var room in rooms) {
            if (rooms.hasOwnProperty(room)) {
                console.log("     ", room);
            }
        }

        // list all rooms this socket is in:
        console.log("Active rooms for this socket:");
        io.sockets.connected[socket.id].rooms.forEach(function(room) {
            console.log("    ", room);
        });


    });

    socket.on('leave', function(name) {
        socket.leave(name);
    });

});