gpt4 book ai didi

c++ - (C++) 如何显示这个 vector ?

转载 作者:行者123 更新时间:2023-11-27 23:36:03 25 4
gpt4 key购买 nike

我想显示我的 vector P1Hand 以显示玩家 1 拥有的牌。

//Create vector to store deck names in & player's hands
vector<string> Cards;
vector<string> P1Hand;


//Generate the deck of cards
for (int i = 0; i < SuitNum;i++) //iterate through suits
{
for (auto j : CardValue)
{
card = j.first + suits[i]; //declare card
DeckValue[card] = CardValue[j.first];//add card to deck map and assign value
Cards.push_back(card); // add card to vector
CardNum++;// raise card number for every card added to deck
}
}


//display cards for p1
while (CardNum > 13)
{
int RCard = rand()%CardNum; //generate a random number based on number of cards left in deck
string DrawCard = Cards.at(RCard); // access a random card
P1Hand.push_back(DrawCard); //add card to hand
Cards.erase(Cards.begin() + RCard); // remove cards from vector so they cant be called twice
CardNum--; //lower available cards
cout<<"["<<DrawCard<<"]"; //print card and its value
}
cout<<endl;
cout<<"Select Card to Play: ",cin>>PlayedCardP1;
cout<<endl;

这里是我将从牌组抽取的每张牌添加到玩家手中的地方(我不确定 vector 是否有效,所以我想显示它。

P1Hand.push_back(DrawCard); //add card to hand

我已经通过谷歌搜索如何显示 vector 尝试了 std::p1hand 选项,但它只显示错误。

最佳答案

要显示 std::string 的 vector ,您可以执行以下操作:

  1. for (const auto& s : vec)
    std::cout << s << std::endl;
  2. for (auto it = vec.begin(); it != vec.end(); ++it)
    std::cout << *it << std::endl;
  3. std::copy(vec.begin(), vec.end(), 
    std::ostream_iterator<std::string>(std::cout, "\n"));

关于c++ - (C++) 如何显示这个 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59123798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com