「Cocos2dx/std::vector」の版間の差分
提供: 初心者エンジニアの簡易メモ
行35: | 行35: | ||
public: | public: | ||
int level; | int level; | ||
+ | int address; | ||
static SampleModule* create(); | static SampleModule* create(); | ||
}; | }; | ||
行51: | 行52: | ||
SampleModule* sample = SampleModule::create(); | SampleModule* sample = SampleModule::create(); | ||
sample->level = 102; | sample->level = 102; | ||
+ | sample->address = "hoge"; | ||
samples.push_back(sample); | samples.push_back(sample); | ||
log("sample level=%d", samples[0]->level); // 102 | log("sample level=%d", samples[0]->level); // 102 | ||
+ | log("sample address=%s", samples[0]->address); // hoge | ||
==参考== | ==参考== | ||
http://7ujm.net/stl/vector.html | http://7ujm.net/stl/vector.html |
2017年4月5日 (水) 17:56時点における版
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; int address; 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; sample->address = "hoge"; samples.push_back(sample); log("sample level=%d", samples[0]->level); // 102 log("sample address=%s", samples[0]->address); // hoge