C++ 从数组中删除元素

C++ 从数组中删除元素,c++,C++,我希望能够从清单中删除一个项目,即使用“删除头盔”,然后从阵列中删除头盔。我有一个例子,但我不知道哪一个变量去了哪里 void remData(const InventoryRecord list[], int size) { system("cls"); cout <<"Enter Item you wish to remove from your inventory: " << endl;// This is being displayed so us

我希望能够从清单中删除一个项目,即使用“删除头盔”,然后从阵列中删除头盔。我有一个例子,但我不知道哪一个变量去了哪里

void remData(const InventoryRecord list[], int size) {
    system("cls");
    cout <<"Enter Item you wish to remove from your inventory: " << endl;// This is being displayed so user can see items in the inventory
    double cost = 0;

  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "All Items in your Bag" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Item Name              Qty     Value" << endl;// It is not displaying right the alignment is off
    cout << "~~~~~~~~~~~~~~~~~~" << endl;
    /* from here I do not know what to do! What I want is have use type the item name they want removed
                            also display an error if they enter an  item wrong*/
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(4)  << list[i].qty
           << setw(10) << list[i].value << left << endl;
           cost = cost + list[i].value * list[i].qty;


    cout << "~~~~~~~~~~~~~~~~~~~" << endl;
    cout << right << setw(3) << size;
    cout << " items listed";
    cout << right << setw(19) << cost << endl << endl;



    }}}
void remData(常量清单记录列表[],整数大小){
系统(“cls”);

cout类似的方法可能会奏效,但我建议使用std::list,因为删除可以在固定时间内完成。如果您想保持顺序,则只能在线性时间内完成对数组的删除(如我所做的)。另一个选项是将空位置与数组中大小为1的元素交换,然后从大小中减去1

注意:您必须更改您的函数头,如果您想修改它,列表[]不能为常量,并且由于您要删除元素,所以大小应该通过引用传递,以便可以更改

void remData(InventoryRecord list[], int &size) {
    system("cls");
    cout <<"Enter Item you wish to remove from your inventory: " << endl;// This is being displayed so user can see items in the inventory

    // the name we query for
    string qname;
    bool found = false;
    int index;

    if(size < 1) {
        cout << "Nothing to display" << endl;
    } else {
        // display the inventory for reference
        dispData(list, size);

        // prompt for name
        cout << "Name      : ";
        getline(cin, qname);
        for ( int i = 0; i < size; ++i )
            if ( qname == list[i].name )
            {
                found = true;
                index = i;
                // assuming only removing first item with this name
                break;
            }

        if (!found) {
            // you could re-prompt using a loop instead of just ending here
            cout << "Could not find item " << qname << "!" << endl;
        } else {
            // remove item from list and shift other items over the one we want to remove
            for ( int i = index; i < size-1; ++i )
            {
                // modifying list (this is why it can't be const)
                list[i].name = list[i+1].name;
                list[i].qty = list[i+1].qty;
                list[i].value = list[i+1].value;
            }
            // decrement the size (this is why it was pass-by-reference)
            size--;
        }
    }
}
void remData(库存记录列表[],整数和大小){
系统(“cls”);

代码太多..您不能从数组中删除元素。您应该使用容器,例如,如果您真的要从数组中删除元素,则必须至少移动一个元素以缩小间距(除非是最后一个)。或者您可以将该元素标记为无效。在将代码缩减为@한국매미 says.Error 1 Error LNK2019:未解析的外部符号“void\uu cdecl remData(struct InventoryRecord const*const,int)”(?remData@@YAXQBUInventoryRecord)@@H@Z)在函数\u main c:\Users\Ashley\Documents\Visual Studio 2012\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.obj ConsoleApplication2中引用。错误2错误LNK1120:1未解析的外部c:\Users\Ashley\Documents\Visual Studio 2012\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe ConsoleApplication2这些是我发现的错误我发现了我的问题谢谢