C++ 在c+中作为参数传递向量指针+;

C++ 在c+中作为参数传递向量指针+;,c++,vector,C++,Vector,我试图将指针的向量传递给insertVector函数。因为我不知道主函数中向量数组的大小,所以我在insertVector函数中创建了向量对象数组。我得到了错误消息,如“总线错误(内核转储)”。当我在main函数中创建如下对象时,它是可行的。我做错了什么? 向量*ivec=新向量[5]; 我期待这样的结果。 索引:0 测试 哈哈 索引:1 测试 哈哈 索引:2 测试 哈哈 索引:3 测试 哈哈 索引:4 测试 哈哈 #包括 #包括 #包括 使用名称空间std; int insertVector(

我试图将指针的向量传递给insertVector函数。因为我不知道主函数中向量数组的大小,所以我在insertVector函数中创建了向量对象数组。我得到了错误消息,如“总线错误(内核转储)”。当我在main函数中创建如下对象时,它是可行的。我做错了什么? 向量*ivec=新向量[5]; 我期待这样的结果。 索引:0 测试 哈哈 索引:1 测试 哈哈 索引:2 测试 哈哈 索引:3 测试 哈哈 索引:4 测试 哈哈

#包括
#包括
#包括
使用名称空间std;
int insertVector(向量*输入)
{
int size=5;
输入=新向量[大小];
对于(int k=0;k
vector-ivec;
并修改代码以反映该更改

您没有分配
向量
对象。代码中的
ivec
是指向no where的指针。

尝试以下操作:

void AllocateVector(std::vector<int> **pIntVector)
{
    *pIntVector = new std::vector<int>[10];
    (*pIntVector)->push_back(10);
    (*pIntVector)->push_back(20);
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> *tmpIntVector;
    AllocateVector(&tmpIntVector);
}
void AllocateVector(std::vector**pIntVector)
{
*pIntVector=新标准::向量[10];
(*pIntVector)->向后推(10);
(*pIntVector)->推回(20);
}
int _tmain(int argc,_TCHAR*argv[]
{
std::vector*tmpIntVector;
AllocateVector(&tmpIntVector);
}

指针的使用是C程序通常的编写方式。为什么不通过引用传递向量?跟踪容器的大小也有C编程的味道。你知道吗
向量隐式地知道它自己的大小吗?你不必跟踪它。你(a)不正确地通过值传递指针,以及(b)由于(a)导致内存泄漏并调用未定义的行为。将参数更改为
vector*&input
将解决您的问题(在
main()
中随后发生的内存泄漏无法承受),但这不是真正的问题,也就是说,首先要以呈现的方式进行。为什么不使用向量向量-
std::vector
     vector<string> ivec;
void AllocateVector(std::vector<int> **pIntVector)
{
    *pIntVector = new std::vector<int>[10];
    (*pIntVector)->push_back(10);
    (*pIntVector)->push_back(20);
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> *tmpIntVector;
    AllocateVector(&tmpIntVector);
}