• 締切済み

2次元のvectorにオブジェクトのポインタを格納

VC++6.0です。 まず、ポインタでなく、int型の格納なら分かったんです。 ・・・ #include <vector> using namespace std; main(){ vector <vector<int > > a(10); ←←← for(int i=0;i<10;i++){ a[i].resize(10); } ・・・ for(int j=0;j<10;j++){ for(int i=0;i<10;i++){ a[i][j]=i+j; } } ・・・ } という感じで利用できることが。 しかし、やりたいのは、整数型でなく、 オブジェクトのポインタを格納したいんですが、 矢印部分を例えば、vector <vector<class *> > a(10); などどしてもエラーになってしまいます。 どのように実装したらよいのでしょうか?

noname#108554
noname#108554

みんなの回答

noname#4252
noname#4252
回答No.1

どのようなエラーでしょうか?

noname#108554
質問者

お礼

すみません。勘違いしてました。 もうちょっと自分で考えてみます。

関連するQ&A

  • 2次元配列にポインタを格納

    http://www.okweb.ne.jp/kotaeru.php3?q=505241の訂正版の質問です。 VC++6.0を使っております。 下のようなプログラムを作ってみました。 #include <stdio.h> #include <vector> using namespace std; class c{ public: c(); virtual ~c(); int get(){return j;}; void set(int i){j=i;}; private: c(const c &right); const c &operator=(const c &right); int j; }; void main(){ vector <vector<c*> > a; c *b; for(int n=0;n<10;n++){ for(int i=0;i<10;i++){ b=new c; a[n].push_back(b); } } for(int j=0;j<10;j++){ for(int i=0;i<9;i++){ a[i][j] -> set(i+j); } } for(j=0;j<10;j++){ for(int i=0;i<9;i++){ printf("%d ",a[i][j] -> get()); } printf("%d\n",a[9][j] -> get()); } for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ delete a[i][j]; } } } すると、コンパイルには成功するのですが、実行は出来ません。 その理由は、「外部参照1が未解決」だそうです。 アドバイスをお願いいたします。

  • vectorに格納されたオブジェクトの廃棄

    次のようなテストプログラムを作ってみました。 OSはVineです。 #include <iostream> #include <string> #include <vector> using namespace std; class Neko{ string name; public: Neko(){} Neko(string n):name(n){} void SetName(string n){name=n;} void Naku() const; }; void Neko::Naku() const{ cout << "名前は" << name << endl; } int main(){ int i; int num; string temp; vector <Neko> x; for(i=0;i<num;i++){ Neko *y=new Neko; x.push_back(*y); } for(i=0;i<num;i++){ cout << "名前を入力" << endl; cin >> temp; x[i].SetName(temp); } for(i=0;i<num;i++)x[i].Naku(); delete [] x;・・・※ } 廃棄(※のところ)するとき、これでは コンパイルエラーになってしまいます。 for文で回しても同じです。 どのように書けばよいのでしょうか? vectorにはポインタではなくオブジェクトが 入っていることに注意してください。

  • STLのvectorで・・・

    4次元配列を使いたいので以下のようなプログラムを組んでみました。 -------------------------------------------------- #pragma warning( disable : 4786 ) #include <vector> #include <iostream> using namespace std; void EditYMatrix(vector< vector< vector<int> > > *vi, int i); void EditSquareMatrix(vector< vector<int> > *vi, int i); int main(int argc, char* argv[]) { int i, j, c; vector< vector< vector< vector<int> > > > vi; cout << "Xの要素数を入力してください" << endl; cin >> c; vi.resize(c); cout << "Yの要素数を入力してください" << endl; cin >> c; for(i=0 ; i<vi.size() ; i++) EditYMatrix(&vi[i], c); cout << "Zとωの入力" << endl; for(i=0; i<vi.size() ; i++){ for(j=0; j<vi[i].size() ; j++){ cout << i << "," << j << "番目の自由度を入力してください" << endl; cin >> c; EditSquareMatrix(&vi[i][j], c); } } return 0; } void EditYMatrix(vector< vector< vector<int> > > *vi, int i) { vi->resize(i); } void EditSquareMatrix(vector< vector<int> > *vi, int i) { vi->resize(i); for(int j=0; j<i ; j++) vi[j].resize(i); } -------------------------------------------------- Y,Z,ωは可変で、Zとωは同じにするので、 ためしにこのようなプログラムを組んでみました。 ですが、実行途中でエラーが起きてしまいます。 /* verify block type */ _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)); という部分でエラーが起きているのですが 知識不足で、どのような内容なのかわかりません。 どなたか分かる方教えてください。 OS:Win2000 VC++6.0

  • C++ 構造体型のvector配列でエラーがでます

    構造体のvector配列を関数に渡しています。 以下のソースコードで、3点エラーがでます どのように変更すればよいですか? #include<stdio.h> #include<stdlib.h> #include<iostream> #include <vector> using namespace std; std::vector<int> v; typedef struct fukusosu{ int a; int b; }FUKUSOSU; int sort(vector<FUKUSOSU> v[], int N){ FUKUSOSU tmp; int j; for(int i = 0 ; i < N - 1 ; i++){ j = i ; for(int k = i + 1 ; k < N ; k++){ if(v[j].a > v[k].a){j = k;}//ここのaに対して、エラーがでます } tmp = v[j];//ここのイコールに対して、エラーがでます v[j] = v[i]; v[i] = tmp;//ここのイコールに対してエラーがでます } } int main(void){ int N; // 要素数 cin >> N; vector<FUKUSOSU> v(N); for(int i = 0; i < N; ++i){ cin >> v[i].a; cin >> v[i].b; } }

  • 配列のプログラムですが

    #include <iostream> using namespace std; int main() { int a[100],b=-9999; int i=0,j; do { cout << "整数値を入力してください\n"; cin >> a[i]; b += a[i]; i++; }while( a[i-1] != 9999); cout << b << '\n'; for(j=0;j<i-1;j=j+1) cout << a[j] * 3 << '\n'; return 0; } このプログラムってどんな計算をしてるんですか?誰か分かる人いますか 整数値を入れてくださいって出るだけなんですが

  • 2次元配列のポインタ

    整数型行2列2の2次元配列の[1][0]は、ポインタでは3になるとおもっているのですが、ちがったでしょうか? 下記のソースでエラーが出ていて困っています。 void main() { int array[2][2] = { { 9, 9 }, { 9, 9 } }; int num=2, j,i; *(array + 3) = 0; for (i = 0; i < num; i++) { for (j = 0; j < num; j++) { printf("%d", array[i][j]); } puts(""); } } 99 09 と表示させたいのですが、どこに間違いがあるのでしょうか?

  • Vectorのポインタが入ったvector

    windows Vista sp1, Visual C++ 2008でC++の勉強をしています。 Vectorへのポインタが入ったvectorを使うプログラムを書いているのですがうまくいかず、困っています。 どういうプログラムかというと、 入力ファイルの">"という記号を区切りとして、その間にある各行をひとまとめのグループとしてvectorにいれます。 さらに各vectorのポインタをべつのvectorに入れます。 最終的に区切りの数だけvectorができ、入力ファイルを読み終わった後に すべてのvectorを"各グループのポインタが入ったvector"からループ処理ですべて出力する、というものです。 /入力ファイル input.txt/ > human cat dog > beetle dragonfly spider > salmon saury catfish > vector1には human cat dog vector2にはbeetle dragonfly spider vector3にはsalmon saury catfish が入り、 別のvectorにそれぞれのvectorのポインタをいれ、 最後にこのvectorをつかって全ファイル内容を出力するというものです。 具体的に書くと、 ">"の区切りごとの各行のstringを入れるvectorとしてeach_vector。 each_vectorのポインタを入れるvectorをvector_of_ptr_each_vectorとします。 ">"を認識するごとに new で each_vectorの領域を確保し、そのポインタをvector_of_ptr_each_vectorに追加していき、">"のない行のstringを each_vectorに入れます。 ファイルの読み込みが終わった後でvector_of_ptr_each_vectorからイテレータを使って各vector(each_vector)の全要素をそれぞれ出力する、というものです。 以下のようにコードを書きました。 #include <fstream> #include <string> #include <vector> #include <iostream> using namespace std; int main( ) { ifstream ifs("input.txt"); string buf; std::vector<string> each_vector; std::vector<std::vector<string> *> vector_of_ptr_each_vector; while(ifs && getline(ifs, buf)) { if(buf[0] == '>'){ std::vector<string>* ptr_eachvector ; ptr_eachvector = new std::vector<string>; each_vector = *ptr_eachvector ; vector_of_ptr_each_vector.push_back(ptr_eachvector) ; } each_vector.push_back(buf) ; } printf("\n output from vector of ptr of vector\n"); std::vector<std::vector<string> *>::iterator it_b = vector_of_ptr_each_vector.begin(); while( it_b != vector_of_ptr_each_vector.end() ) { std::vector<string>::iterator it_c = it_b->begin();    //エラー1 while( it_c != it_b->end() ) //エラー2 { cout << *it_c << endl; ++it_c; } ++it_b; } return 0; } ですが、エラーでビルドされず、 std::vector<string>::iterator it_c = it_b->begin(); の行に関して error C2839: invalid return type 'std::vector<_Ty> **' for overloaded 'operator ->' error C2039: 'begin' : is not a member of 'std::_Vector_iterator<_Ty,_Alloc>' while( it_c != it_b->end() )   の行に関して error C2839: invalid return type 'std::vector<_Ty> **' for overloaded 'operator ->' 1> with 1> [ 1> _Ty=std::string 1> ] error C2039: 'end' : is not a member of 'std::_Vector_iterator<_Ty,_Alloc>' 1> with 1> [ 1> _Ty=std::vector<std::string> , 1> _Alloc=std::allocator<std::vector<std::string> > 1> ] fatal error C1903: unable to recover from previous error(s); stopping compilation というようなエラーが出ます。 vectorのポインタを入れたvectorの扱い、特にイテレータに関して問題があると思うのですが原因が分かりません。 また、new でのeach_vectorの領域確保の方法も怪しいという感じがします。 解決策、アドバイスありましたらよろしくお願いします。

  • C++ vectorのbeginについて

    VC++2010にて下記コードのビルドは通るのですが、 vVecotrInt, vVectorIntPtrの要素数0のとき、iptr代入時に Debug assertaion Failed! vector iterator not dereferencableとなります。 そもそもbegin()はイテレータなので、ポインタに代入しようとしていることが間違いかと思うのですが。 質問1.int* iptr = ~ ではなく、std:vector<int>::iteratorとすれば、     要素数が0でもエラーがおきません。この違いは何でしょうか? 質問2.そもそもイテレータをポインタに代入して何か得することがあるんでしょうか?     ただイテレータとポインタは同じようなものだと思って、コーディングしてるだけなんでしょうか・・・ コード: // vVectorIntの要素を間接参照して(参照先はint)、そのアドレスをポインタに格納 std::vector<int> vVectorInt; int* iptr = &*vVectorInt.begin(); // ここでvector iterator not dereferencable // vVectorIntPtrの要素を間接参照して(参照先はint*)、ポインタに格納 std::vector<int*> vVectorIntPtr; int* iptr = *vVectorIntPtr.begin(); // ここでvector iterator not dereferencable // vVecotrIntPtr2の要素数0のときでも、イテレータを使えば問題ない std::vector<int*> vVectorIntPtr2; std::vector<int*>::iterator itr = vVectorIntPtr2.begin();

  • どのようにすればいいですか?

    10個の1桁の整数を入力して、その中から最も大きい数字を3つを 入力した順番通り出力するにはどのようにすればいいですか? #include<iostream> using namespace std; int main(void){ int b[10]; int a; for(int i=0; i<10; i++){ cin >> a; b[i]=a; } int i, j, temp; for (i = 1; i < 10; i++) { temp = b[i]; for (j = i; j > 0 && b[j-1] > temp; j--) b[j] = b[j -1]; b[i] = temp; } for(int i=0; i<3;i++){ cout << b[9-i]; } return 0; }

  • 高次元整数ベクトルの高速計算

    VC++2010でwindowsアプリケーション(CLI)を作成しています。 その中で1000次元の整数ベクトルのパワーを求めるために概略次のような計算をしています。 int v[1000]; //v[0]~v[999]に整数値を代入後 long long int power = 0; for(int i = 0; i < 1000; i++){ power += v[i] * v[i]; } この計算を数万回繰り返す必要があるため、是非高速化を図りたいのですが、CPUのベクトル計算機能等を利用して高速化することができるでしょうか?

専門家に質問してみよう