2016-11-10 76 views
0

下面是一個數獨初始值設定項,我試圖創建一個基於用戶輸入的函數,從板上擦除一個隨機元素。隨機元素可以從板的任何部分移除。如何從矢量<vector <short>>中隨機刪除元素?

class cell{ 
    bool m_occu; //occupied is shown '.' 
    int m_num; 

public: 
    cell() : m_occu(false), m_num(0) {} 
    void setMark(const int num){m_num = num; m_occu = true;} 
    bool isMarked() const { return m_occu; } 
    int getNum(){ return m_num;} 
    friend ostream& operator << (ostream& o, const cell& c){ 
     if (!c.m_occu) return o << setw(2) << '-'; 
     return o << setw(2) << c.m_num; 
    } 
}; 

class board { 
    vector<vector <cell> >m_map; 
    bool col_row; 

public: 
    board() { 
     vector<cell> a_row(9); 
     col_row = false; 
for (int i = 0; i < 9; ++i) 
     { 
      for(int j = 0; j < 9; j++) 
      { 
       a_row[j].setMark(j+1); 
      } 
      random_shuffle(a_row.begin(), a_row.end()); 
      m_map.push_back(a_row); 
     } 
    } 

    void erase(){ 

    } 
+0

我建議使用具有vector.erase(RAND()來做到這一點。 –

回答

0

這裏是擦除功能的代碼:用於隨機數生成

void erase(std::vector your_vector){ 
    your_vector.erase(your_vector.begin() + random(1,your_vector.size())); 
} 

這的代碼:)

int random(int min, int max) //range(min, max) 
{ 
    bool first = true; 
    if (first) 
    { 
     srand(time(NULL)); //seeding only for the first time 
     first = false; 
    } 
    return min + rand() % (max - min); 
} 
相關問題