- NNNN
- 会議室デビュー日: 2007/01/22
- 投稿数: 2
|
投稿日時: 2007-01-22 16:25
vector<int>を書き換え不可/静的な変数として定義したいのですが、
初期値の指定方法がわかりません。
現在はstaticのみで静的メンバ変数として定義し、動作していますが、
本来この値は定数なので、constとして定義したいのです。
このように定義することはできるのでしょうか。
よろしくお願いします。
[ メッセージ編集済み 編集者: NNNN 編集日時 2007-01-22 16:33 ]
|
- Blue
- 大ベテラン
- 会議室デビュー日: 2005/09/12
- 投稿数: 230
- お住まい・勤務地: 知っている人は知っている
|
投稿日時: 2007-01-22 16:46
固定配列なら別にvectorじゃなくてもconstの配列でいいのでは?
(追加されることもないし、変更されることもない。)
static const int hoge[] = {1, 2, 3, 4, 5};
static const char* moge[] = {"ABC", "EFG", "HIJ"};
配列数も固定だし、vectorのlengthを使うとか意味なさげだし。
#define COUNTOF(ary) ((sizeof(ary))/(sizeof((ary)[0])))
みたいなマクロを使えば配列数も取れるし。
追記修正)
static const のメンバ変数を使う場合、宣言は
class Foo
{
static const int hoge[5]; // ここにはサイズを入れないとダメっぽい
};
のようにして、定義部分で
const int Foo::hoge[] = {1, 2, 3, 4, 5};
とすればOK。
[ メッセージ編集済み 編集者: Blue 編集日時 2007-01-22 16:56 ]
|
- 甕星
- ぬし
- 会議室デビュー日: 2003/03/07
- 投稿数: 1185
- お住まい・勤務地: 湖の見える丘の上
|
投稿日時: 2007-01-22 16:55
vectorを継承すればよいかと・・・
|
- Blue
- 大ベテラン
- 会議室デビュー日: 2005/09/12
- 投稿数: 230
- お住まい・勤務地: 知っている人は知っている
|
投稿日時: 2007-01-22 17:19
継承するとなるとこんな感じかな。
コード: |
| template<typename TYPE>
class Hoge : public std::vector<TYPE>
{
public:
Hoge(const TYPE* data, const size_t size)
{
for (size_t i = 0; i < size; ++i)
this->push_back(data[i]);
}
};
|
コード: |
| class Foo
{
public:
Foo();
virtual ~Foo();
static const Hoge<int> hoge;
};
|
コード: |
| #define COUNTOF(ary) ((sizeof(ary))/(sizeof((ary)[0])))
static const int h[] = {1, 2, 3, 4, 5};
const Hoge<int> Foo::hoge = Hoge<int>(h, COUNTOF(h));
|
やはり、これだとvectorである必要性をあまり感じない。
(std::mapならありかなとおもう。)
|
- Tdnr_Sym
- ぬし
- 会議室デビュー日: 2005/09/13
- 投稿数: 464
- お住まい・勤務地: 明石・神戸
|
投稿日時: 2007-01-22 22:59
こんばんは。
引用: |
|
NNNNさんの書き込み (2007-01-22 16:25) より:
vector<int>を書き換え不可/静的な変数として定義したいのですが、
初期値の指定方法がわかりません。
|
2つイテレータを引数に取るコンストラクタを呼び出せば初期化できます。
(イテレータの代わりにポインタを指定することができます。)
コード: |
|
#include <iostream>
#include <vector>
using namespace std;
void main()
{
static const int A[] = {101, 102, 103, 104, 105};
// static constなvectorを初期化
static const vector<int> v(A, A + sizeof(A) / sizeof(int));
// static const vector<int> v(&A[0], &A[5]);
// 内容の確認
for (vector<int>::const_iterator itor = v.begin(); itor != v.end(); itor++) {
cout << *itor << endl;
}
}
|
|
- Blue
- 大ベテラン
- 会議室デビュー日: 2005/09/12
- 投稿数: 230
- お住まい・勤務地: 知っている人は知っている
|
投稿日時: 2007-01-23 01:10
引用: |
|
Tdnr_Symさんの書き込み (2007-01-22 22:59) より:
2つイテレータを引数に取るコンストラクタを呼び出せば初期化できます。
|
継承しなくても、もともとあったわけね。
完全に見落としていました。
|
- NNNN
- 会議室デビュー日: 2007/01/22
- 投稿数: 2
|
投稿日時: 2007-01-23 10:25
Blue 様
甕星 様
Tdnr_Sym 様
ありがとうございます。
要求通りの動作をするコードができました。
蛇足ながら、同じ動作をmap<int,string>で行う場合は
コード: |
|
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
class test{
public:
test();
static const pair<int,string> ar[];
static const map<int,string> mp;
};
const pair<int,string> test::ar[]={
make_pair(1,"str1"),
make_pair(2,"str2"),
make_pair(3,"str3")
};
const map<int,string> test::mp(ar,ar+sizeof(ar)/sizeof(ar[0]));
int main(){
map<int,string>::const_iterator itor;
itor=test::mp.find(3);
cout << itor->second << endl;
return true;
}
|
というやり方でおかしい所はないでしょうか。
(動作するのは確認できました)
|
- Blue
- 大ベテラン
- 会議室デビュー日: 2005/09/12
- 投稿数: 230
- お住まい・勤務地: 知っている人は知っている
|
投稿日時: 2007-01-23 10:35
> static const pair<int,string> ar[];
はメンバ変数にする必要ありますか?
(あっても、privateメンバ変数になる?)
(ar[]って書き方、宣言を.hに、定義を.cpp分けて書くとコンパイルエラーにならないかな?(VC6はエラー吐いた))
全然関係ないけど
>return true;
int型を返すのにbool側の値を返すのはおかしいかと。
(キャストするとか。ちなみにmain文の戻り値は正常の場合0((int)false)にするようなきがした。)
|