C++のSTLコンテナメンバ変数の初期化

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
chibago

C++のSTLコンテナメンバ変数の初期化

#1

投稿記事 by chibago » 12年前

表題のとおり、STLコンテナ(vectorなど)をメンバ変数は初期化できるのでしょうか。

一般的な変数ならば、コンストラクタの名前のあとの()と{}の間に
コロンをつけてメンバ変数を初期化(メンバ変数のコンストラクタだと思いますが)
できます。

ですが、STLコンテナでは便利なコンストラクタによる初期化のやり方が
準備されていないため、boost::assignなどが用意されている状態であると
理解しております。

何かよい方法はございますでしょうか。(なお、メンバ変数はconstとしたいと
考えております。)

また、余談ですが、vectorの初期化のboost::assign::list_ofの使い方で、
多次元配列を初期化することは可能でしょうか。(ちょっと困っております。)

アバター
うしお
記事: 56
登録日時: 13年前

Re: C++のSTLコンテナメンバ変数の初期化

#2

投稿記事 by うしお » 12年前

実際に試してみました。
若干入れ子になって少々嫌な感じもしますが、
そのままシンプルに初期化できました。
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

アバター
a5ua
記事: 199
登録日時: 13年前

Re: C++のSTLコンテナメンバ変数の初期化

#3

投稿記事 by a5ua » 12年前

boost::assignを使うなら、こうでしょうか

コード:

#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();
}
このコードは、ideone上のgcc-4.3.4では、なぜかコンパイルエラーになりました。

以下のように、キャストしたら通りました。

コード:

#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();
}
boost::assignに頼らないなら、初期化用に自分で関数を用意します。

コード:

#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();
}
また、C++0xのinitializer_list機能に対応しているコンパイラならば、以下のようなコードが使えます。

コード:

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

chibago

Re: C++のSTLコンテナメンバ変数の初期化

#4

投稿記事 by chibago » 12年前

うしおさん、a5uaさん、
さっそくのおへんじありがとうございます。

実際に動くサンプルを示していただき大変
ありがたいです。
(私は、結構な時間没頭したのですが、
結局、うまくいきませんんでした。)

さっそく活用させていただきます。

閉鎖

“C言語何でも質問掲示板” へ戻る