表題のとおり、STLコンテナ(vectorなど)をメンバ変数は初期化できるのでしょうか。
一般的な変数ならば、コンストラクタの名前のあとの()と{}の間に
コロンをつけてメンバ変数を初期化(メンバ変数のコンストラクタだと思いますが)
できます。
ですが、STLコンテナでは便利なコンストラクタによる初期化のやり方が
準備されていないため、boost::assignなどが用意されている状態であると
理解しております。
何かよい方法はございますでしょうか。(なお、メンバ変数はconstとしたいと
考えております。)
また、余談ですが、vectorの初期化のboost::assign::list_ofの使い方で、
多次元配列を初期化することは可能でしょうか。(ちょっと困っております。)
C++のSTLコンテナメンバ変数の初期化
Re: C++のSTLコンテナメンバ変数の初期化
実際に試してみました。
若干入れ子になって少々嫌な感じもしますが、
そのままシンプルに初期化できました。
chibago さんの模索していらっしゃる方法とずれていたらすみません。
実行結果
1
2
3
4
5
6
********************
1
2
3
4
5
6
若干入れ子になって少々嫌な感じもしますが、
そのままシンプルに初期化できました。
chibago さんの模索していらっしゃる方法とずれていたらすみません。
#include <vector>
#include <iostream>
#include <boost/assign.hpp>
#include <boost/foreach.hpp>
class hoge
{
public:
hoge():
m_constant(boost::assign::list_of(1)(2)(3))
{
BOOST_FOREACH(int val,m_constant)
{
std::cout << val << std::endl;
}
BOOST_FOREACH(int val,ms_constant)
{
std::cout << val << std::endl;
}
}
const std::vector<int> m_constant; //メンバconst
static const std::vector<int> ms_constant; // メンバstatic const
};
const std::vector<int> hoge::ms_constant = boost::assign::list_of(4)(5)(6);
int main(){
hoge h;
std::cout << "******************" << std::endl;
//多次元の初期化
std::vector< std::vector<int> > multi = boost::assign::list_of( boost::assign::list_of(1)(2)(3) )
( boost::assign::list_of(4)(5)(6) );
BOOST_FOREACH(const std::vector<int>& Array,multi)
{
BOOST_FOREACH(int val,Array)
{
std::cout << val << std::endl;
}
}
return 0;
}
1
2
3
4
5
6
********************
1
2
3
4
5
6
Re: C++のSTLコンテナメンバ変数の初期化
boost::assignを使うなら、こうでしょうか
このコードは、ideone上のgcc-4.3.4では、なぜかコンパイルエラーになりました。
以下のように、キャストしたら通りました。
boost::assignに頼らないなら、初期化用に自分で関数を用意します。
また、C++0xのinitializer_list機能に対応しているコンパイラならば、以下のようなコードが使えます。
#include <vector>
#include <iostream>
#include <boost/assign.hpp>
template <typename T>
void print_vector(const std::vector<T> &v)
{
std::cout << "[" << v.size() << "]" << std::endl;
for (int i = 0, N = v.size(); i < N; ++i) {
std::cout << v[i] << std::endl;
}
}
class test
{
public:
test() : v(boost::assign::list_of(1)(2)(3))
{
print_vector(v);
}
private:
const std::vector<int> v;
};
int main()
{
test();
}
以下のように、キャストしたら通りました。
#include <vector>
#include <iostream>
#include <boost/assign.hpp>
template <typename T>
void print_vector(const std::vector<T> &v)
{
std::cout << "[" << v.size() << "]" << std::endl;
for (int i = 0, N = v.size(); i < N; ++i) {
std::cout << v[i] << std::endl;
}
}
class test
{
public:
test() : v(static_cast<const std::vector<int> &>(boost::assign::list_of(1)(2)(3)))
{
print_vector(v);
}
private:
const std::vector<int> v;
};
int main()
{
test();
}
#include <vector>
#include <iostream>
template <typename T>
void print_vector(const std::vector<T> &v)
{
std::cout << "[" << v.size() << "]" << std::endl;
for (int i = 0, N = v.size(); i < N; ++i) {
std::cout << v[i] << std::endl;
}
}
// 必要な分だけ、オーバーロードするもよし
std::vector<int> make_vector(int a0, int a1, int a2)
{
std::vector<int> v;
v.push_back(a0);
v.push_back(a1);
v.push_back(a2);
return v;
}
class test
{
public:
test() : v(make_vector(1, 2, 3))
{
print_vector(v);
}
private:
const std::vector<int> v;
};
int main()
{
test();
}
#include <vector>
#include <iostream>
template <typename T>
void print_vector(const std::vector<T> &v)
{
std::cout << "[" << v.size() << "]" << std::endl;
for (int i = 0, N = v.size(); i < N; ++i) {
std::cout << v[i] << std::endl;
}
}
class test
{
public:
test() : v{1, 2, 3}
{
print_vector(v);
}
private:
const std::vector<int> v;
};
int main()
{
test();
}
Re: C++のSTLコンテナメンバ変数の初期化
うしおさん、a5uaさん、
さっそくのおへんじありがとうございます。
実際に動くサンプルを示していただき大変
ありがたいです。
(私は、結構な時間没頭したのですが、
結局、うまくいきませんんでした。)
さっそく活用させていただきます。
さっそくのおへんじありがとうございます。
実際に動くサンプルを示していただき大変
ありがたいです。
(私は、結構な時間没頭したのですが、
結局、うまくいきませんんでした。)
さっそく活用させていただきます。