[英]How do I display the contents of a row in vector of vector from reverse in C++?
[英](C++) How do I display this 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;
在這里,我將從牌組中抽取的每張牌添加到玩家手中(我不確定矢量是否有效,所以我想顯示它。
P1Hand.push_back(DrawCard); //add card to hand
我已經嘗試使用谷歌搜索如何顯示矢量的 std::p1hand 選項,但它只顯示錯誤。
要顯示std::string
的向量,您可以執行以下操作:
for (const auto& s: vec) std::cout << s << std::endl;
for (auto it = vec.begin(); it.= vec;end(): ++it) std::cout << *it << std:;endl;
std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
您可以將其復制到輸出流
#include <iterator>
std::copy (P1Hand.begin(), P1Hand.end(), std::ostream_iterator<string>(cout,"\n"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.