Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从数组中删除元素并将所有元素向上移动?_C++ - Fatal编程技术网

C++ 如何从数组中删除元素并将所有元素向上移动?

C++ 如何从数组中删除元素并将所有元素向上移动?,c++,C++,我试图从这个数组中删除一个名称,然后在删除后在数组的最后一个位置有一个空白点。我该怎么做?下面是我试过的。它会移除它,但不会移动到最后 const int array_size = 16; string restaurants[array_size] = {"Texas Roadhouse","On The Border","Olive Garden","Panda Express","Cracker Barrel","IHOP","Woohoo","Pei Wei","Mcdona

我试图从这个数组中删除一个名称,然后在删除后在数组的最后一个位置有一个空白点。我该怎么做?下面是我试过的。它会移除它,但不会移动到最后

const int array_size = 16;
string restaurants[array_size] = {"Texas Roadhouse","On The Border","Olive Garden","Panda       Express","Cracker Barrel","IHOP","Woohoo","Pei Wei","Mcdonalds","Denny's","Mrs. Fields","Subway","Dairy Queen","Burger King","Pizza Hut","Dominos"};
int current_size = 16;

cout << "Please enter the name of the Restaurant you would like to remove: ";
cin.ignore();
getline(cin, remove_restaurant);

remove(restaurants, restaurants_size, remove_restaurant);//function call

bool remove(string restaurants[], int& current_size, string name)//function to remove   array
{
    for (int i = 0; i < current_size; i++)//look at each name you want to remove
    {
        if ( restaurants[i] == name)
        {
        restaurants[i]=restaurants[i+1];
        current_size --;
        cout << "Restaurant removed successfully." << endl;
        return true;            
        }
    }
return false;
}
const int数组_size=16;
字符串餐厅[数组大小]={“德克萨斯Roadhouse”、“在边界上”、“橄榄园”、“熊猫快车”、“饼干桶”、“IHOP”、“Woohoo”、“Pei Wei”、“麦当劳”、“Denny's”、“Mrs.Fields”、“Subway”、“乳制品皇后”、“汉堡王”、“必胜客”、“多米诺骨牌”};
int current_size=16;
库特
  • 创建与原始阵列大小相同的阵列
  • 开始迭代原始数组的元素
  • 如果数组中的当前项不等于要删除的项,请将其添加到新数组中
  • 创建与原始阵列大小相同的阵列
  • 开始迭代原始数组的元素
  • 如果数组中的当前项不等于要删除的项,请将其添加到新数组中

  • std::remove
    std::fill
    一起使用remove-erase习惯用法:

    bool remove(string restaurants[], int& current_size, string name)//function to remove   array
    {
        auto begin = std::begin(restaurants);
        auto end = std::next(begin, current_size);
        auto new_end = std::remove(begin, end, name);
        std::fill(new_end, end, {});
        current_size = std::distance(begin, new_end);
        if (new_end != end) {
            std::cout << "Restaurant removed successfully." << std::endl;
        }
        return new_end != end;
    }
    
    bool remove(string[],int¤t_size,string name)//删除数组的函数
    {
    自动开始=标准::开始(餐厅);
    自动结束=标准::下一步(开始,当前大小);
    自动新建\u end=std::删除(开始、结束、名称);
    std::fill(new_end,end,{});
    当前_大小=标准::距离(开始,新_结束);
    如果(新结束!=结束){
    
    std::cout使用remove-erase习惯用法,与
    std::remove
    std::fill

    bool remove(string restaurants[], int& current_size, string name)//function to remove   array
    {
        auto begin = std::begin(restaurants);
        auto end = std::next(begin, current_size);
        auto new_end = std::remove(begin, end, name);
        std::fill(new_end, end, {});
        current_size = std::distance(begin, new_end);
        if (new_end != end) {
            std::cout << "Restaurant removed successfully." << std::endl;
        }
        return new_end != end;
    }
    
    bool remove(string[],int¤t_size,string name)//删除数组的函数
    {
    自动开始=标准::开始(餐厅);
    自动结束=标准::下一步(开始,当前大小);
    自动新建\u end=std::删除(开始、结束、名称);
    std::fill(new_end,end,{});
    当前_大小=标准::距离(开始,新_结束);
    如果(新结束!=结束){
    
    std::cout这里有一种可能的方法来修改您当前的解决方案。我同意ott-,不过,您可能应该使用列表

    for (int i = 0; i < current_size; i++)//look at each name you want to remove
    {
      if (restaurants[i] == name) {
        swap(restaurants[i], restaurants[current_size-1]);
        current_size --;
        cout << "Restaurant removed successfully." << endl;
        return true;
      }
    }
    
    for(inti=0;icout这里有一种可能的方法来修改您当前的解决方案。我同意ott-,不过,您可能应该使用列表

    for (int i = 0; i < current_size; i++)//look at each name you want to remove
    {
      if (restaurants[i] == name) {
        swap(restaurants[i], restaurants[current_size-1]);
        current_size --;
        cout << "Restaurant removed successfully." << endl;
        return true;
      }
    }
    
    for(inti=0;i
    
    #包括
    #包括
    #包括
    // ...
    矢量餐厅{…};
    餐厅;
    getline(cin,移除餐厅);
    删除
    (删除(restaurants.begin(),restaurants.end(),remove_restaurant));
    
    带有,并且:

    #包括
    #包括
    #包括
    // ...
    矢量餐厅{…};
    餐厅;
    getline(cin,移除餐厅);
    删除
    (删除(restaurants.begin(),restaurants.end(),remove_restaurant));
    
    首先,我认为你更愿意使用
    向量
    列表
    ,这就是它的设计目的。但如果你想这样做,你可以编写一个
    向上移动
    方法:

     void moveUp(int startIndex, int endIndex, string* array) {
         for (int ii = startIndex; ii < endIndex; ++ii) {
             array[ii] = array[ii + 1];
         }
    
         array[endIndex] = 0;
     }
    
    void moveUp(int-startIndex、int-endIndex、string*array){
    对于(int ii=startIndex;ii
    首先,我认为你更愿意使用
    向量
    列表
    ,这就是它的设计目的。但如果你想这样做,你可以编写一个
    向上移动
    方法:

     void moveUp(int startIndex, int endIndex, string* array) {
         for (int ii = startIndex; ii < endIndex; ++ii) {
             array[ii] = array[ii + 1];
         }
    
         array[endIndex] = 0;
     }
    
    void moveUp(int-startIndex、int-endIndex、string*array){
    对于(int ii=startIndex;ii使用代码列表代替数组。具体地,使用STD::列表从STLIT中只略微C++(Cout/CIN和内联声明)。使用STL容器来建议它真正成为C++解决方案。使用列表而不是数组。具体地,使用STD::STLT中的列表仅是轻微的C++(Cout/CIN和内联声明)。使用STL容器,建议它真正成为C++解决方案。出于好奇,您如何将“已成功删除”的消息添加到这个?检查NeXyEnter?= Enter?{ } { }是一个初始化值列表,用空值填充吗?我只会用“”,“那更糟?”Sarien啊,漏掉了副作用。<代码>
    原则上效率较低,因为
    std::string
    构造函数必须从空字符串文本中读取(尽管编译器可能会对其进行优化)。出于好奇,您如何将“removed successfully”(已成功删除)消息添加到此消息中?检查是否有新的\u end!=end?还有,{}用空值填充初始值设定项列表?我会简单地使用“”,更糟糕吗?@Sarien啊,忽略了这个副作用。
    原则上效率较低,因为
    std::string
    构造函数必须从空字符串文本读取(尽管编译器可能会对其进行优化)。