简体   繁体   中英

C++ creating a std::vector from std::string array

I am learning about c++ and was following a course. A final exercise involves making a program for deck of cards. I have thought of an approach:

I initially tried to do everything with string arrays but realised that it would make more sense to use vectors since. I am now trying to create a std::vector std::string out of my std::string array but with no luck.

I have found some example code online such as: 在此处输入图像描述 from https://thispointer.com/5-different-ways-to-initialize-a-vector-in-c/

And tried to implement it for my program, however, I cannot get it to work and cant fully understand what is the issue.

My code:


#include <iostream>
#include <string>
#include <vector>




class card_deck {
public:
    card_deck();
    std::vector<std::string> deal_hand(int num_cards);
    void new_deck();
    

private:
    //std::vector<std::string> cards_vector;
    std::string cards[52] =
    {
        "As","2s","3s","4s","5s","6s","7s","8s","9s","Ts","Js","Qs","Ks",
        "Ah","2h","3h","4h","5h","6h","7h","8h","9h","Th","Jh","Qh","Kh",
        "Ad","2d","3d","4d","5d","6d","7d","8d","9d","Td","Jd","Qd","Kd",
        "Ac","2c","3c","4c","5c","6c","7c","8c","9c","Tc","Jc","Qc","Kc"
    };

    std::vector<std::string> cards_vector(cards, sizeof(cards)/sizeof(std::string) );
    
};

As you can see from my code, I initialize a string array in my private variables, and then want to convert this string array to std::vector

The error message returned: 在此处输入图像描述

UPDATE

Code works when called in main()

int main()
{
    std::string cards[52] =
    {
        "As","2s","3s","4s","5s","6s","7s","8s","9s","Ts","Js","Qs","Ks",
        "Ah","2h","3h","4h","5h","6h","7h","8h","9h","Th","Jh","Qh","Kh",
        "Ad","2d","3d","4d","5d","6d","7d","8d","9d","Td","Jd","Qd","Kd",
        "Ac","2c","3c","4c","5c","6c","7c","8c","9c","Tc","Jc","Qc","Kc"
    };

    // Initialize vector with a string array
    std::vector<std::string> vecOfStr(cards, cards + sizeof(cards) / sizeof(std::string));
    for (std::string str : vecOfStr)
        std::cout << str << std::endl;

}

Does not work when used in class

#include <iostream>
#include <string>
#include <vector>

class card_deck {
public:
    card_deck();
    std::vector<std::string> deal_hand(int num_cards);
    void new_deck();
    

private:

    std::string cards[52] =
    {
        "As","2s","3s","4s","5s","6s","7s","8s","9s","Ts","Js","Qs","Ks",
        "Ah","2h","3h","4h","5h","6h","7h","8h","9h","Th","Jh","Qh","Kh",
        "Ad","2d","3d","4d","5d","6d","7d","8d","9d","Td","Jd","Qd","Kd",
        "Ac","2c","3c","4c","5c","6c","7c","8c","9c","Tc","Jc","Qc","Kc"
    };

    // Initialize vector with a string array
    std::vector<std::string> vecOfStr(cards, cards + sizeof(cards) / sizeof(std::string));
    for (std::string str : vecOfStr)
        std::cout << str << std::endl;
    
};



int main()
{

}

Easy way:

std::vector<std::string> cards {
    "As","2s","3s","4s","5s","6s","7s","8s","9s","Ts","Js","Qs","Ks",
    "Ah","2h","3h","4h","5h","6h","7h","8h","9h","Th","Jh","Qh","Kh",
    "Ad","2d","3d","4d","5d","6d","7d","8d","9d","Td","Jd","Qd","Kd",
    "Ac","2c","3c","4c","5c","6c","7c","8c","9c","Tc","Jc","Qc","Kc"
};

and drop the separate cards_vector member. cards.size() yields the number of elements in the vector.

This uses the initialiser-list syntax of C++11.

And the compiler works out the size for you: handy if you need to add the jokers in later for example.

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