简体   繁体   English

(C++) 如何显示这个向量?

[英](C++) How do I display this vector?

I would like to display my vector P1Hand to show the cards that Player 1 has.我想显示我的矢量 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;

Here is where I'm adding every card drawn from the deck to the player's hands (im not sure if the vector works though so I wanted to display it.在这里,我将从牌组中抽取的每张牌添加到玩家手中(我不确定矢量是否有效,所以我想显示它。

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

I've tried the std::p1hand options from googling how to display vectors but it just shows errors.我已经尝试使用谷歌搜索如何显示矢量的 std::p1hand 选项,但它只显示错误。

To display a vector of std::string s you can do:要显示std::string的向量,您可以执行以下操作:

  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"));

You can Copy it to the output stream您可以将其复制到输出流

#include <iterator>

std::copy (P1Hand.begin(), P1Hand.end(), std::ostream_iterator<string>(cout,"\n")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM