Cocos2dx/std::vector
提供: 初心者エンジニアの簡易メモ
include
#include <vector>
intの場合
std::vector<int> nums; nums.push_back(1); nums.push_back(10); nums.push_back(100); nums.push_back(1000); int cnt = nums.size(); log("num=%d", nums[0]); // 1 log("num=%d", nums[1]); // 10 log("num=%d", nums[2]); // 100 log("num=%d", nums[3]); // 1000 log("size=%d", cnt); // 4
charの場合
std::vector<char*> names; names.push_back("taro"); names.push_back("jiro"); names.push_back("saburo"); int namecnt=names.size(); log("name=%s", names[0]); // taro log("name=%s", names[1]); // jiro log("name=%s", names[2]); // saburo log("size=%d", namecnt); // 3
カスタムクラス
SampleModule.h
#ifndef Helloworld_SampleModule_h #define Helloworld_SampleModule_h #include "cocos2d.h" class SampleModule { public: int level; static SampleModule* create(); }; #endif
SampleModule.cpp
#include "SampleModule.h" USING_NS_CC; SampleModule* SampleModule::create() { return new SampleModule(); }
HelloworldScene.cpp
std::vector<SampleModule*> samples; SampleModule* sample = SampleModule::create(); sample->level = 102; samples.push_back(sample); log("sample level=%d", samples[0]->level); // 102