ヒープ内にベクターがありますが、その要素を取得できません - コンパイルされません - nエラー 'をバインドできません' std :: basic ostream 'スタックベクトルと単純配列は正常に動作します。ここで何が間違っていますか?ヒープ上のベクトルにアクセスできない
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> * factors= new vector<int>(4);
cout<<factors<<endl;
cout<<(factors+1)<<endl;
//cout<<factors[1]<<endl;
factors->push_back(12);
factors->push_back(232);
factors->push_back(54);
factors->push_back(42);
//cout<<*factors; //error - cannot bind 'std::ostream..' lvalue to 'std::basic_ostream...'
// cout<<factors[0]; // error
//vector in stack
vector<int> factors3(4);
factors3.push_back(43);
factors3.push_back(543);
factors3.push_back(64);
factors3.push_back(26);
cout<<"factors3 "<<factors3[3]<<endl; //gives "0"
cout<<"factors3 "<<factors3.at(3)<<endl; //gives "0"
int * factors2=new int[10];
factors2[0]=32;
factors2[1]=35;
factors2[2]=676;
factors2[3]=123;
cout<<factors2[0]<<endl; //it's OK
cout<<factors2[1]<<endl;
cout<<*factors2<<endl;
cout << "Done" << endl;
return 0;
}
ありがとうございます。それは今非常に明確です。しかし、もしintのベクトルのベクトルを持っていたらどうなりますか? 'ベクトル< vector> *ポインタ=新しいベクトル< vector>()'?ポインタが外部ベクトルを指し示すならば、外部参照を取得し、intを得るには '(* pointer)[0] [0]'が必要ですか?または、それが ''ベクトル<> *> * pointer2'の場合、intは '(*(* pointer2)[0]'ですか? –
parsecer
疑問に思ったら、タイプを見てください! 'pointer2'が'ベクトル> * 'の場合、' * pointer2'は 'ベクトル>'です。角かっこを使用してベクトルから何かを選択すると、基礎となる型の何かが返されます。つまり '(* pointer2)[a]'は 'ベクトル'なので、 '(* pointer2)[a] [b]'は 'int'になります。このアプローチを使うと、 'vector *> *'ならば、あなたは 'pointer2'から何かを選ぶでしょうか? –
templatetypedef