简体   繁体   中英

(C++) How do I display this vector?

I would like to display my vector P1Hand to show the cards that Player 1 has.

//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.

To display a vector of std::string s you can do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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