凱翔 發問時間: 電腦與網際網路程式設計 · 1 0 年前

關於vector C++ Microsoft Visual

我想寫一個大樂透的程式

1需輸入購買幾組

2電腦亂數選出六個號碼(無需特別號)

3需對照六個號碼是否重覆

4在開一組為中獎號碼

5對照中獎號碼

因為規定一定要vector來寫而且需要對照中獎號碼

所以每一組都需存下來對照

因為vector不太會用二維!

所以想請教一下vector二維要怎麼宣告、使用

如果可以能幫我整個程式寫出來

或是教一下如何用二維

vector裡在包一vector

已更新項目:

可以中文註解一下嗎?

2 個已更新項目:

有沒有人幫我寫出完整的程式

拜託囉

3 個已更新項目:

可以給我清楚點的二維[][]如何使用嗎?

1 個解答

評分
  • 1 0 年前
    最佳解答

    #include <vector>

    typedef std::vector<int> LottoNumber;

    typedef std::vector<LottoNumber> Numbers;

    int main(void)

    {

    // some code here, srand, initialization....etc

    // when creating it

    LottoNumber lotto;

    for(int i = 0; i < 6; ++i)

    lotto.push_back(rand()%60 + 1);

    Numbers num;

    num.push_back(lotto);

    // to access it, for example, there are 6 lotto number in 'num'

    // if you want to access the 3rd number in 4th lotto numbers

    int value = num[4][3];

    // just like accessing double array

    // go check out msdn.com for more info on vector

    // personally, i will use std::map for this question, but that's just me

    // by the way, the code is not written in compiler, so might be some

    // typing mistakes. but the ideas are there. Good luck

    return 0;

    }

    2007-06-01 06:47:20 補充:

    I am sorry, my computer doesn't have Chinese input. But here is things you can do with vector:

    (1) typedef std::vector<int> Lotto; // create vector of int

    (2) typedef std::vector<Lotto> Numbers; // vector of vector of int

    same as std::vector< std::vector<int > >

    2007-06-01 06:47:30 補充:

    (3) Lotto lotto; // insert 6 numbers

    lotto.push_back(0);

    lotto.push_back(5);

    lotto.push_back(25);

    lotto.push_back(16);

    lotto.push_back(9);

    lotto.push_back(10);

    Numbers numbers;

    numbers.push_back(lotto); // so there is one set of number, add more

    2007-06-01 06:47:35 補充:

    (4) To access it:

    int fourth = numbers[0][3]; // fourth number in 1st number set

還有問題?馬上發問,尋求解答。