「Cocos2dx/std::vector」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→カスタムクラス) |
|||
(同じ利用者による、間の1版が非表示) | |||
行35: | 行35: | ||
public: | public: | ||
int level; | int level; | ||
+ | int address; | ||
static SampleModule* create(); | static SampleModule* create(); | ||
}; | }; | ||
行50: | 行51: | ||
std::vector<SampleModule*> samples; | std::vector<SampleModule*> samples; | ||
SampleModule* sample = SampleModule::create(); | SampleModule* sample = SampleModule::create(); | ||
− | sample->level = | + | sample->level = 101; |
+ | sample->address = "hoge"; | ||
samples.push_back(sample); | samples.push_back(sample); | ||
− | log("sample level=%d", samples[0]->level); // | + | SampleModule* sample2 = SampleModule::create(); |
+ | sample2->level = 102; | ||
+ | sample2->address = "hoge2"; | ||
+ | samples.push_back(sample2); | ||
+ | log("sample level=%d", samples[0]->level); // 101 | ||
+ | log("sample address=%s", samples[0]->address); // hoge | ||
+ | for (SampleModule* _sample : samples) { | ||
+ | log("sample level=%d", _sample->level); | ||
+ | log("sample address=%s", _sample->address); | ||
+ | } | ||
==参考== | ==参考== | ||
http://7ujm.net/stl/vector.html | http://7ujm.net/stl/vector.html |
2017年4月11日 (火) 12:00時点における最新版
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 = 101; sample->address = "hoge"; samples.push_back(sample); SampleModule* sample2 = SampleModule::create(); sample2->level = 102; sample2->address = "hoge2"; samples.push_back(sample2); log("sample level=%d", samples[0]->level); // 101 log("sample address=%s", samples[0]->address); // hoge for (SampleModule* _sample : samples) { log("sample level=%d", _sample->level); log("sample address=%s", _sample->address); }