如何从向量中删除struct元素?

时间:2015-08-18 01:22:50

标签: c++ vector struct

我是一名新手程序员,我正在开发一个程序,该程序在酒店中持有宠物注册表(我们在课堂上看到的一些愚蠢的运动,无关紧要)。我正在使用向量来保存结构元素(宠物)。结构的代码是这样的:

String toPDF = String.valueOf(Character.toChars(e.getCode("Euro")));

要求用户输入的功能是:

struct Pets{
string Name;
string Race;
string Owner;
int Tel;
}p;

好的,现在我需要通过输入名称或东西来创建一个删除宠物的功能。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:8)

矢量是一个未分类的容器,因此简单的解决方案确实是您唯一的选择。

var Vehicle = {
    colour : 'blue',
    info   : function() { console.log('wheels:' + this.wheels + ' colour:' + this.colour); }
};

var Car = {
    wheels : 4,
    drift  : function() { console.log('drifting'); }
};

var Bike = {
    wheels : 2,
    wheelie : function() { console.log('stunt riding'); }
};

var ferrari = construct( $.extend({}, Vehicle, Car),  {colour:'red'} ); 
var yamaha  = construct( $.extend({}, Vehicle, Bike) ); 

ferrari.drift(); // difting
ferrari.info(); // wheels:4 colour:red
yamaha.wheelie(); // stunt riding
yamaha.info(); // wheels:2 colour:blue

/*
    construct(proto, props, funcs)
    sets prototype functions from inherited classes
    invokes any functions passed
*/

function construct(proto, props, funcs)
{
    return Object.create(proto, {

        init : { value : function() {

            $.extend(this, props);
            if (funcs) for (var i=0; i<funcs.length; i++) funcs[i].call(this);
            delete this.init;

            return this;

        }, configurable : true } // used to delete init prop from object

    }).init(); 
}

这称为Erase-remove idiom

请注意,这将删除与该名称匹配的所有宠物,而不只是一个。

相关问题