Bag of ML Words

ML = Machine Learning, Music Love, and Miscellaneous things in daily Livings

C++ 学習記(3): vector

C++といえばvectorというくらい、配列は全部vectorにするのがいいっぽい。

 

どこで見たのか覚えていないのですが、webで見つけた資料を基に

vectorの挙動を調べるためのコードを書きました。ふーんって感じ。

*1

 

#include <iostream>
#include <vector>
#include <string>
using namespace std;

typedef vector<float> fvec;

void print(const char* str, vector<int> &v){
if(v.empty()){
cout << "コンテナ" << str << "は空です。" << endl;
cout << str << "の要素数 is " << v.size() << endl;
} else {
vector<int>::iterator it;
cout << str << "の要素数 is " << v.size() << endl;
for (it = v.begin(); it != v.end(); it++){
cout << " " << *it;
}
cout << endl;
}

}

void print(const char* str, fvec &v){
if(v.empty()){
cout << "コンテナ" << str << "は空です。" << endl;
cout << str << "の要素数 is " << v.size() << endl;
} else {
fvec::iterator it;
cout << str << "の要素数 is " << v.size() << endl;
for (it = v.begin(); it != v.end(); it++){
cout << " " << *it;
}
cout << endl;
}

}

int main(){
int i1;
vector<int> v1;
vector<int> v2(10, 3);
fvec fv1;
fvec fv2(5, 3.5);

/* initalize */
cout << "*** initialize a vector ***" << endl;
print("v1", v1); // should be empty
print("fv2", fv2);

for(i1 = 0; i1 < 10; ++i1){
v1.push_back(i1);
fv1.push_back( (float)i1 * 1.1 );
}
cout << "# v1.push_back(i1);" << endl;
print("v1", v1);

cout << "# fv1.push_back( (float)i1 * 1.1 );" << endl;
print("fv1", fv1);

cout << endl;
cout << "*** clear a vector ***" << endl;
fv1.clear();
cout << "# fv1.clear()" << endl;
print("fv1", fv1);


/* vector of vectors */
cout << endl;
cout << "*** initialize a vector of vectors ***" << endl;
vector<fvec> vector_of_fv;
for(i1 = 0;i1 < 3; ++i1){
fvec new_fv(fv2);
vector_of_fv.push_back(new_fv);
}
// check if the above push_back is reference or deep copy
fv2[0] = -1.9;

cout << "# fvec new_fv(fv2);" << endl;
cout << "# vector_of_fv.push_back(new_fv);" << endl;
print("fv2", fv2);
for(i1 = 0;i1 < 3; ++i1){
string str = "vector_of_fv[" + to_string(i1) + "]";
print(str.c_str(), vector_of_fv[i1]);
}

for(i1 = 0;i1 < 3; ++i1){
vector_of_fv[i1][i1] = -1.2 * i1;
}
cout << "# vector_of_fv[i1][i1] = -1.2 * i1;" << endl;

for(i1 = 0;i1 < 3; ++i1){
string str = "vector_of_fv[" + to_string(i1) + "](modified)";
print(str.c_str(), vector_of_fv[i1]);
}


}

出力結果

$ ./a.out [~/projects/CPP]
*** initialize a vector ***
コンテナv1は空です。
v1の要素数 is 0
fv2の要素数 is 5
3.5 3.5 3.5 3.5 3.5
# v1.push_back(i1);
v1の要素数 is 10
0 1 2 3 4 5 6 7 8 9
# fv1.push_back( (float)i1 * 1.1 );
fv1の要素数 is 10
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9

*** clear a vector ***
# fv1.clear()
コンテナfv1は空です。
fv1の要素数 is 0

*** initialize a vector of vectors ***
# fvec new_fv(fv2);
# vector_of_fv.push_back(new_fv);
fv2の要素数 is 5
-1.9 3.5 3.5 3.5 3.5
vector_of_fv[0]の要素数 is 5
3.5 3.5 3.5 3.5 3.5
vector_of_fv[1]の要素数 is 5
3.5 3.5 3.5 3.5 3.5
vector_of_fv[2]の要素数 is 5
3.5 3.5 3.5 3.5 3.5
# vector_of_fv[i1][i1] = -1.2 * i1;
vector_of_fv[0](modified)の要素数 is 5
-0 3.5 3.5 3.5 3.5
vector_of_fv[1](modified)の要素数 is 5
3.5 -1.2 3.5 3.5 3.5
vector_of_fv[2](modified)の要素数 is 5
3.5 3.5 -2.4 3.5 3.5

 

参考文献

完全にC++は未知だけどもJavaなら結構経験積んでいたので、私の知り合いの中で最もプログラミングに長けた大先輩におすすめしていただいた

を買いました。とりあえず、わからないことがあったらこれにあたってみると、1つくらいは例が載っているのでだいたい乗り切れています。*2

*1:でも、まだ各要素にアクセスするときはfor(int i)にしちゃう(イテレータに慣れてない)

*2:const参照とか&, *の使い方とか私にとっては完全に悪夢なので、毎回見ています