将矢量指向数组

时间:2013-08-16 18:36:50

标签: c++

我不确定我的标题是否措辞得当,所以我会在这里详细说明。

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

#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)

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

2 个答案:

答案 0 :(得分:1)

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

您不需要将卡片列表设为std::string[]。您可以使用std::array,它将为您提供标准的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模板,您将更有效地改进代码并学习语言。

答案 1 :(得分: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