从阵列包中删除并插入元素。为什么是布尔数组而不是int? 我在C++中实现了使用包的数组。我不知道如何让deleteElement函数工作。假设它从数组中删除给定的元素。 我也不明白为什么数组是用bool初始化的,以及insert函数是如何工作的

从阵列包中删除并插入元素。为什么是布尔数组而不是int? 我在C++中实现了使用包的数组。我不知道如何让deleteElement函数工作。假设它从数组中删除给定的元素。 我也不明白为什么数组是用bool初始化的,以及insert函数是如何工作的,c++,arrays,bag,C++,Arrays,Bag,我有三个问题:\ 如何使deleteElement函数工作 为什么数组是用bool初始化的 insert函数是如何工作的?看起来它只是给数组增加了真值,但是当这个程序打印数组时,你会看到x值被打印出来了,我无法理解 你的三个问题实际上来自这样一个事实:这不是一个真正的袋子。这里的内容更像是一个“布尔掩码”,指示从零到BAG\u AS\u ARRAY\u SIZE-1的数字是真是假。这就是为什么有一个布尔数组作为存储,其中的所有元素都初始化为false。也就是说,掩码没有为从零到BAG\u AS\

我有三个问题:\

  • 如何使deleteElement函数工作
  • 为什么数组是用bool初始化的
  • insert函数是如何工作的?看起来它只是给数组增加了真值,但是当这个程序打印数组时,你会看到x值被打印出来了,我无法理解

  • 你的三个问题实际上来自这样一个事实:这不是一个真正的袋子。这里的内容更像是一个“布尔掩码”,指示从零到
    BAG\u AS\u ARRAY\u SIZE-1的数字是真是假。这就是为什么有一个布尔数组作为存储,其中的所有元素都初始化为false。也就是说,掩码没有为从零到
    BAG\u AS\u ARRAY\u SIZE-1的任何数字设置

    然后,您的
    deleteElement
    函数只需将掩码的对应数组位置设置为false即可“删除”该数字,“插入”一个数字即可将掩码中的特定位置设置为true


    display\u bag
    函数中,请注意,您打印的不是数组的内容(显然只能是true或false),而是数组中具有
    true
    值的位置索引。

    \u如何使deleteElement函数工作
    b.as_数组[x]=false?堆栈溢出问题应该问一个问题,而不是三个。注意:为了保持命名惯例的一致性,将
    deletelement
    更改为
    delete\u element
    。提出类似问题的更好方法是陈述您的观察结果和期望,然后询问您是否正确。你可能只需要一点点的修正,这表明你已经在自己解决问题上花了一些心思。隔室中有东西(
    true
    ),或者隔室是空的(
    false
    )。您看到的是数字,因为您打印的是索引(
    i
    ),而不是值。看见
    #include <iostream>
    #include <math.h> 
    #include <algorithm>
    using namespace std;
    
    // cin -> add 0 qry 0 del 0 qry 0 quit
    // cout -> TF
    
    // add x -> Adds the number x to the bag
    // qry x -> Checks if x belongs to the bag then output T, otherwise output F
    // del x -> If there is an element x in the bag, remove it, otherwise do nothing.
    // quit  -> Stops the program
    
    // Exercise: Fun with bags 1 (Here the bag is a set of int values).
    /*
    Example:
    input: add 1 add 2 add 1 del 1 qry 1 qry 2 quit
    output: FT
    */ 
    
    // enumeration type for actions on the bag
    enum action {add, qry, del, quit, none}; 
    
    // translation of strings into actions
    action str2action(string s) {
        if (s=="add")  return add;
        if (s=="qry")  return qry;
        if (s=="del")  return del;
        if (s=="quit") return quit;
        return none;
    }
    
    #define BAG_AS_ARRAY_SIZE 10
    
    struct bag {
        bool as_array[BAG_AS_ARRAY_SIZE];    // using arrays
    };
    
    // Simple function to initialise the bag
    void initialise(bag &b){
    
        // Array
        for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
            b.as_array[i] = false;
        }
    
    }
    
    // function to display the content of the bag
    void display_bag(bag b) {
    
        cout << "The bag is : " << endl;
    
        // Array
        cout << " - (A) - : " ;
        for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
            if(b.as_array[i])
                cout << i << " " ;
        }
        
        cout << endl;
    
    
        return;
    
    }
    
    void insert(bag &b,unsigned int x){ //add
    
        // Array
        b.as_array[x] = true;
    }
    
    void check(bag &b,unsigned int x) //qry
    {
        bool q = false;
        for(int i = 0; i < BAG_AS_ARRAY_SIZE; i++)
        { 
            if(b.as_array[x])
            {
                q = true;
            }
        }
    
       if(q == true)
       {
           cout << "T";
       }
       if(q == false) 
       {
           cout << "F";
       }
        cout << endl;
    }
    
    void DeleteElement(bag &b, unsigned int x) //del
    {
        int i;
       for (i=0; i<BAG_AS_ARRAY_SIZE; i++) 
          if (b.as_array[i] == x) 
             break;
    
        if (i < BAG_AS_ARRAY_SIZE) 
       { 
         for (int j=i; j<BAG_AS_ARRAY_SIZE; j++) 
            b.as_array[j] = b.as_array[j+1]; 
       } 
    
    }
    
    // this function deals with actions on a bag
    void update(bag &b, action a, unsigned int x){
    
        switch(a){
        case add:
            insert(b,x);
            break;
        case qry:
            check(b,x);
            break;
        case del:
            DeleteElement(b,x);
            break;
        case quit:
            break;
        case none:
            break;
        default:
            break;
        }
    
        return;
    }
    
    int main()
    {
        bag my_bag; //We create an array of boolean type.
        string my_act_str;
        unsigned int x;
    
        initialise(my_bag); //The array is initialised with False, which is 0
    
        bool go_on = true;
        while(go_on)
        {
            display_bag(my_bag);    
            cout << "What's next? (actions = add x ,qry x ,del x ,quit)" << endl;
            cin >> my_act_str;
            action act = str2action(my_act_str);
            if(act == quit)
            {
                go_on = false;
            }
            if(act == add)
            {
                cin >> x;
                update(my_bag,act,x);
            }
            if(act == qry)
            {
                cin >> x;
                update(my_bag,act,x);
            }
            if(act == del)
            {
                cin >> x;
                update(my_bag,act,x);
            }
    
        }
    
        return 0;
    }
    
    void delete_element(bag &b, unsigned int x) 
    {
            b.as_array[x] = false;
    }