facebook twitter hatena line google mixi email

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;
    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);
}

参考

http://7ujm.net/stl/vector.html