如何在c++中使用printf打印std::string数组

How to print std::string array with printf in c++?

本文关键字:std 打印 string 数组 printf c++      更新时间:2023-10-16

我正在尝试用printf打印std::string,这是我的代码。但它不会打印我指定的字符串。

头文件

#include "cocos2d.h"
#include <iostream>
class Cards : public cocos2d::CCLayer{
public:
    virtual bool init();
    virtual void load();
    std::string TotalCards[52];
}

#include "Cards.h"
    bool Cards::init(){
        if ( !CCLayer::init() ) {
            return false;
        }
        TotalCards[0] = "ClubsA";
        TotalCards[1] = "HeartsB";
        TotalCards[2] = "Diamonds4";
        return true;
    }

    void Cards::load(){
        printf("Hey I am HEREn");
        for (int i=0 ; i<3; i++) {
            printf("CARD NAME %sn", TotalCards[i].c_str());
        }

它只打印

卡名

卡名

卡片名称

在调用Cards::load之前,请确保调用了Cards::init()并返回true。如果Cards::init()未被调用,或者返回false,则Cards::load访问的数组将由三个空字符串组成。