Last active
April 6, 2018 04:36
-
-
Save baobao/fcc173957ba81a30eb2f to your computer and use it in GitHub Desktop.
C++vectorテストコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
class Foo | |
{ | |
public: | |
Foo(){ | |
std::cout<<"constructor"<<"\n"; | |
} | |
~Foo(){ | |
std::cout<<"destructor"<<"\n"; | |
} | |
void init(int id); | |
void update(); | |
int _id; | |
}; | |
void Foo::init(int id) | |
{ | |
_id = id; | |
} | |
void Foo::update() | |
{ | |
std::cout<<"update::id::"<<_id<<"\n"; | |
} | |
void fooooo(std::vector<Foo> arr) | |
{ | |
//配列の中身実行 | |
for (int i = 0; i < arr.size(); i++) | |
{ | |
arr[i].update(); | |
} | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
std::vector<Foo> array; | |
int i; | |
for (i = 0; i < 10; i++) | |
{ | |
Foo *fuga = new Foo; | |
//ポインタへのアクセスは->アローシンタックス | |
fuga->init(i); | |
//参照をvectorに渡す | |
array.push_back(*fuga); | |
} | |
fooooo(array); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment