C++ 清除和重新填充std::vector<;标准::向量<;T>&燃气轮机;造成分配问题
我将数据存储在doubleC++ 清除和重新填充std::vector<;标准::向量<;T>&燃气轮机;造成分配问题,c++,vector,allocation,memory-management,C++,Vector,Allocation,Memory Management,我将数据存储在doublestd::vector结构中,我需要能够重复填充和清除:这导致了一些我不理解的分配问题 我使用的是结构向量XSims,其中系数采用以下形式 class Coefficients{ int N; public: vector<gsl_vector *> Coefs; //Iterator to traverse the vector structure. vector<gsl_vector*>::iterator it; void it_star
std::vector
结构中,我需要能够重复填充和清除:这导致了一些我不理解的分配问题
我使用的是结构向量XSims
,其中系数
采用以下形式
class Coefficients{
int N;
public:
vector<gsl_vector *> Coefs;
//Iterator to traverse the vector structure.
vector<gsl_vector*>::iterator it;
void it_start(){
it = Coefs.begin();
}
void it_end(){
it = Coefs.end();
}
void it_next(){
++it;
}
void it_prev(){
--it;
}
bool not_end(){
return it < Coefs.end();
}
//Return number of vectors contained.
size_t length(){return Coefs.size();}
//Append pointer to outside data into the Coefs structure.
void append( gsl_vector * X ){
Coefs.push_back(X);
}
Coefficients(int N1) : N(N1) {
Coefs.reserve(N);
}
//Clean up procedure
void Clear(){
//Forward iterator.
it_start();
while(not_end()){
gsl_vector_free((*it));
it_next();
}
Coefs.clear();
}
~Coefficients(){
//Forward iterator.
Clear();
Coefficients::Coefs.clear();
}
};
我正在努力结合使用的两个功能如下:
inline void Simulate(){
XSims.reserve(N+1);
Xstart();
for(int i=0;i<N; i++){
//Build a container to insert into XSims
Coefficients * XsimsIteration = new Coefficients(1000);
// time points to previous vector of simulations.
(*Xit)->it_start();
for(int m=0;m<1000;m++){
//Allocate free space for the components of the DW and Xsims.
gsl_vector * X = gsl_vector_alloc(X0.X0->size);
XsimsIteration->append(X);
gsl_vector_memcpy(X,(*Xit));
//Next simulation
(*Xit)->it_next();
}
cout << "Number of sims being inserted into X = " << XsimsIteration->length() << endl;
//Insert XsimsIteration into the XSims container
XSims.push_back(XsimsIteration);
//next time point
X_next();
cout << "Number of simulations stored in X = " << (*Xit)->length() << endl;
}
}
inline void XW_clear(){
Xstart();
//Don't want to clear the initial values, so step forward!
X_next();
while(X_not_end()){
(*Xit)->Clear();
X_next();
}
XSims.clear();
}
前两个函数工作正常,但第二个Simulate()
在运行时崩溃。基本上,它似乎不想将第二个外循环上的XsimsIteration
推回:我得到了奇怪的输出:
Number of sims being inserted into X = 1000
Number of simulations stored in X = 0
存储在X中的第二个
模拟数实际上应该与第一个相同,即1000。您的末端测试不正确
inline bool X_not_end(){return {Xit < XSims.end()};}
同样的情况也适用于系数::not_end()
看看这个:
inline void Simulate(){
XSims.reserve(N+1);
Xstart();
for(int i=0;i<N; i++){
//Build a container to insert into XSims
Coefficients * XsimsIteration = new Coefficients(1000);
// time points to previous vector of simulations.
(*Xit)->it_start();
为了将来的参考,我强烈建议您发布一个可编译的示例。在这个过程中,您通常会学到很多东西,而且通常在执行此操作和调试时,您会发现自己的问题。在这种情况下,我们必须假设在实际的可执行程序中,您可能正在做什么,也可能没有做什么。如果我从后面而不是从前面清除
XSims,它会起作用,我想它会丢弃所有东西,包括我想要保留的初始值。为什么要清除向量?它是空的,那里什么都没有XW_clear
执行X_next
,因此您指向一个空的空间,因为我看不到任何在那里分配任何内容的代码…不,它会清除子向量X_next
转到下一个子向量。在XW\u clear
中没有问题,它是在Simulate
中进行分配的。我刚刚意识到,我编辑了实际访问,其中似乎存在问题,以保持表示简短!在Simulate
的第二个循环中有一个通过Xit
的访问。我把它放进去!我以前有这个,但由于某种原因它不起作用。。。我会再次检查并让你知道原因!与此同时,我找到了另一个答案。我在XW_clear()
中更改了循环的方向,使其从末尾向后,然后pop_back
编辑了我想要更新的向量。无论如何,我认为X\u not\u end
标准是错误的,因为它清除了所有内容,我想在XSims
中保留第一个向量。迭代器上的附加操作允许更改stl实现,这是我考虑不久的将来的一个项目。关于Xstart()。事实上,我已经找到了解决方案,它相对简单:)你所说的,并向我们展示的是不一致的。在发布的示例中,在迭代器的用户之前,XSIM中没有填充元素的地方。
inline bool X_not_end(){return {Xit < XSims.end()};}
inline bool X_not_end(){return Xit != XSims.end();}
inline void Simulate(){
XSims.reserve(N+1);
Xstart();
for(int i=0;i<N; i++){
//Build a container to insert into XSims
Coefficients * XsimsIteration = new Coefficients(1000);
// time points to previous vector of simulations.
(*Xit)->it_start();
(*Xit)->it_start();
(*Xit)->it_next(); // why is this in the for loop? You are iterating over an empty vector