1

我不确定我的标题是否用词正确,所以我将在这里详细说明。

我需要做的是让我的向量从先前构建的 Array 中获取信息,对其进行洗牌,然后将其吐回。我想我可以做的,因为我有一个 for 循环来显示卡片组的信息是在 main 函数中有向量,但同时我觉得我需要创建一个单独的函数。但是,我不确定哪种方法最有效。到目前为止,我所拥有的是这个。

#include <iostream>
#include <string>

#include "prototypes.h"

using std::string;
using std::endl;
using std::cout;

const string CARDS[] =
{
"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD",
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
};

unsigned const DECK_SIZE = sizeof(CARDS) / sizeof(string);

int const SHUFFLE_COMMAND = 1;
int const SHOW_COMMAND = 2;
int const QUIT_COMMAND = 3;
const char MENU[] = "\nShowing Cards Program:\n1 -- Shuffle Cards\n2 -- Show Deck\n3 -- Quit\n: ";
const char IM_OUTTA_HERE[] =  "Buh-bye!";


int main()
{
   int command = 0;
   do
   {
    command = askForInt(MENU, QUIT_COMMAND, SHUFFLE_COMMAND);

    switch(command)
    {
        case SHUFFLE_COMMAND:

          break;
            case SHOW_COMMAND:
            for(int i = 0; i < DECK_SIZE; i++)
            {
                cout << CARDS[i] << " ";
                if(i == 12 || i == 25 || i == 38)
                    {
                        cout << endl;
                    }
            }
          break;
        case QUIT_COMMAND:
          break;    
    }
   }while(command != QUIT_COMMAND);

    cout << IM_OUTTA_HERE << endl;

    return 0;

}

askForInt 在另一个名为 Functions 的文件中,它所做的只是接收用户输入(1、2 或 3)

编辑:还必须注意,我需要使用指针来执行此操作,因为这是我目前正在尝试学习的内容。

4

2 回答 2

1

这一定是一项任务,因为其他 3 个人似乎在做同样的事情。

您不需要您的卡列表是std::string[]. 您可以使用std::arraywhich 将为您提供标准的 STL 迭代器方法,同时仍然是一个简单的数组。std::vector无论哪种方式,您都可以使用的迭代器构造函数将它们加载到向量中:

// assuming data is loading into myarray and myarray_size is the size of the array
std::vector<std::string> cards(myarray, myarray + myarray_size);

您可以直接访问向量的数据(如果需要),使用&cards[0]它将为您提供指向数组中第一个元素的指针。

也就是说,如果您使用 STL 模板,您将改进代码并更有效地学习语言。

于 2013-08-16T18:55:19.943 回答
1

由于您添加了限制,这必须全部使用指针来完成,这会大大改变事情。我编写了下面的代码以兼容回 C++98。希望能从中有所收获。

注意:这只是你目标的套牌随机化部分。其余的由您决定,但已从下面的示例中删除。

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

const string CARDS[] =
{
    "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
    "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD",
    "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH",
    "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
};
unsigned const DECK_SIZE = sizeof(CARDS) / sizeof(string);

int main()
{
    // seed rng
    srand((unsigned)time(nullptr));

    // create a vector of const string ptrs. using pointer enumeration through the
    //  array, add the address of each string object to our deck.
    vector<const string*> deck;
    for (const string* p = CARDS; p != (CARDS + DECK_SIZE); ++p)
        deck.push_back(p);

    // shuffle the pointer vector
    random_shuffle(deck.begin(), deck.end());

    // display each deck through dereferencing.
    for (vector<const string*>::const_iterator it=deck.cbegin(); it!= deck.cend(); ++it)
        cout << (*it)->c_str() << ' ';
    cout << endl;

    return 0;
}

输出

4H TS 9S TH JC AH QS 5S 8S 2D QH 9C 2C 8C 2S JS 9H JH KC 7S 4D 9D KS QC 4S AS 3S 5C KH 7D KD 3D 8D TD 3H 2H 5D JD 8H AC 4C 6S 7C 3C 6H 5H TC 6C QD 6D 7H AD 

如果复制是一个可行的选择,那么需要符合 C++11 或更高版本的以下内容会使这项工作变得微不足道:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <ctime>
using namespace std;

const string CARDS[] =
{
    "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
    "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD",
    "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH",
    "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
};

int main()
{
    // seed rng
    srand((unsigned)time(nullptr));
    vector<string> deck(begin(CARDS), end(CARDS));
    random_shuffle(deck.begin(), deck.end());
    copy(deck.begin(), deck.end(), ostream_iterator<string>(cout," "));
    cout << endl;
};

输出

3S 9C 4C 4S 3H 8D TC 7H TD 5S 3D QC QD 5H TS 4H 3C 7C 9D 6C TH 6S 9S 7S KS KC JC 7D JH 5C 8C 2H JD 9H QS AD JS 2C 4D 6D 8H 5D AS KD 2S 8S AH 6H AC QH 2D KH 
于 2013-08-16T20:07:44.400 回答