为什么会是vector>?

cure 2002-05-20 09:31:19
在编译到 cout << current_vec[ 0 ] << endl 时出现错误:
"operator<<" not implemented in type "ostream" for arguments of type "vector<int,allocator<int>>"

我的current_vec此时到底是什么?

//初始化两个VECTOR
vector<int> fibonacci( 3 ), lucas( 3 );
fibonacci[ 0 ] = 1;
fibonacci[ 1 ] = 1;
fibonacci[ 2 ] = 2;
lucas[ 0 ] = 1;
lucas[ 1 ] = 3;
lucas[ 2 ] = 4;
//定义一个VECTOR类型的指针数组
vector<int> *seq_addrs[ 2 ] = { &fibonacci, &lucas};
//初始化current_vec
vector<int> *current_vec = 0;
//给 current_vec附值
int seq_index = 1;
current_vec = seq_addrs[ seq_index ];

我这时的current_vec是不是应该指向fibonacci,属于vector<int>类型?即
current_vec[ 0 ] == 1;
current_vec[ 1 ] == 1;
current_vec[ 2 ] == 2;
可从编译(我用的是c++ builder 6.0)的几处报错来看显然不是的。为什么呢?那我的current_vec这时又是什么呢?
vector<int,allocator<int>>又是什么东西呀?

谢谢大家指教!
...全文
436 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
RedProgramer 2002-05-20
  • 打赏
  • 举报
回复
vector<int,allocator<int>>中的allocator<int>是一个内存分配通用类,有一个缺省值,所以在使用时不用指定;
在这里current_vec是一个current_vec *类型,current_vec应该指向lucas;current_vec[0][0]==lucas[ 0 ];
current_vec[0]==lucas;
cure 2002-05-20
  • 打赏
  • 举报
回复
谢谢二位的解释。
fangrk 2002-05-20
  • 打赏
  • 举报
回复
cout << (*current_vec)[ 0 ] << endl;

vector<T, Alloc>
T:The vector's value type: the type of object that is stored in the vector.
Alloc:The vector's allocator, used for all internal memory management.
dybcb 2002-05-20
  • 打赏
  • 举报
回复
因为你的current_vec是指向vector的指针,你应该这样用
cout << (*current_vec)[ 0 ] << endl
vector<int, allocator<int> >就是vector<int>,allocator<int>是你定义的vector的内存分配子,用于对你定义的vector分配内存,用vector<int>定义vecotr时缺省的分配子时alloctor<int>

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧