std::list复制到std::vector跳过元素 我在Objtovi-C应用程序中运行C++代码时遇到了一个非常奇怪的异常。我正在使用libxml2读取XSD文件。然后,我将相关标记作为标记类的实例存储在std::list中。然后使用列表上的迭代器将该列表复制到std::vector中。但是,有时列表中的某些元素不会复制到向量中。任何帮助都将不胜感激 printf("\n length list = %lu, length vector = %lu\n",XSDFile::tagsList.size(), XSDFile::tags.size() ); std::list<Tag>::iterator it = XSDFile::tagsList.begin(); //result: length list = 94, length vector = 0 /* for(;it!=XSDFile::tagsList.end();++it) { XSDFile::tags.push_back(*it); //BAD_ACCESS code 1 . . very bizarre . . . . 25 } */ std::copy (XSDFile::tagsList.begin(), XSDFile::tagsList.end(), std::back_inserter (XSDFile::tags)); printf("\n Num tags in vector = %lu\n", XSDFile::tags.size()); if (XSDFile::tagsList.size() != XSDFile::tags.size()) { printf("\n length list = %lu, length vector = %lu\n",XSDFile::tagsList.size(), XSDFile::tags.size() ); //result: length list = 94, length vector = 83 } printf(“\n长度列表=%lu,长度向量=%lu\n”,XSDFile::tagsList.size(),XSDFile::tags.size()); std::list::iterator it=XSDFile::tagsList.begin(); //结果:长度列表=94,长度向量=0 /* 对于(;it!=XSDFile::tagsList.end();++it) { XSDFile::tags.push_back(*it);//错误的访问代码1..非常奇怪..25 } */ std::copy(XSDFile::tagsList.begin(),XSDFile::tagsList.end(),std::back_插入器(XSDFile::tags)); printf(“\n向量中的Num标记=%lu\n”,XSDFile::tags.size()); 如果(XSDFile::tagsList.size()!=XSDFile::tags.size()) { printf(“\n长度列表=%lu,长度向量=%lu\n”,XSDFile::tagsList.size(),XSDFile::tags.size()); //结果:长度列表=94,长度向量=83 }
我发现了问题。内存已损坏,导致std::list在XSD解析过程中损坏。我使用函数start_元素解析XSDstd::list复制到std::vector跳过元素 我在Objtovi-C应用程序中运行C++代码时遇到了一个非常奇怪的异常。我正在使用libxml2读取XSD文件。然后,我将相关标记作为标记类的实例存储在std::list中。然后使用列表上的迭代器将该列表复制到std::vector中。但是,有时列表中的某些元素不会复制到向量中。任何帮助都将不胜感激 printf("\n length list = %lu, length vector = %lu\n",XSDFile::tagsList.size(), XSDFile::tags.size() ); std::list<Tag>::iterator it = XSDFile::tagsList.begin(); //result: length list = 94, length vector = 0 /* for(;it!=XSDFile::tagsList.end();++it) { XSDFile::tags.push_back(*it); //BAD_ACCESS code 1 . . very bizarre . . . . 25 } */ std::copy (XSDFile::tagsList.begin(), XSDFile::tagsList.end(), std::back_inserter (XSDFile::tags)); printf("\n Num tags in vector = %lu\n", XSDFile::tags.size()); if (XSDFile::tagsList.size() != XSDFile::tags.size()) { printf("\n length list = %lu, length vector = %lu\n",XSDFile::tagsList.size(), XSDFile::tags.size() ); //result: length list = 94, length vector = 83 } printf(“\n长度列表=%lu,长度向量=%lu\n”,XSDFile::tagsList.size(),XSDFile::tags.size()); std::list::iterator it=XSDFile::tagsList.begin(); //结果:长度列表=94,长度向量=0 /* 对于(;it!=XSDFile::tagsList.end();++it) { XSDFile::tags.push_back(*it);//错误的访问代码1..非常奇怪..25 } */ std::copy(XSDFile::tagsList.begin(),XSDFile::tagsList.end(),std::back_插入器(XSDFile::tags)); printf(“\n向量中的Num标记=%lu\n”,XSDFile::tags.size()); 如果(XSDFile::tagsList.size()!=XSDFile::tags.size()) { printf(“\n长度列表=%lu,长度向量=%lu\n”,XSDFile::tagsList.size(),XSDFile::tags.size()); //结果:长度列表=94,长度向量=83 },c++,xcode,objective-c++,stdlist,C++,Xcode,Objective C++,Stdlist,我发现了问题。内存已损坏,导致std::list在XSD解析过程中损坏。我使用函数start_元素解析XSD xmlSAXHandler handler = {0}; handler.startElement = start_element; 我在xcode中使用malloc-guard来定位释放内存的使用。它指出了这条线: std::strcpy(message, (char*)name); 所以我删除了malloc(实际上是在代码中注释的)并且它工作了。vector现在一致地复制列表中的
xmlSAXHandler handler = {0};
handler.startElement = start_element;
我在xcode中使用malloc-guard来定位释放内存的使用。它指出了这条线:
std::strcpy(message, (char*)name);
所以我删除了malloc(实际上是在代码中注释的)并且它工作了。vector现在一致地复制列表中的所有94个条目。如果有人能解释一下为什么会这样,那就太好了
static void start_element(void * ctx, const xmlChar *name, const xmlChar **atts)
{
// int len = strlen((char*)name);
// char *message = (char*)malloc(len*sizeof(char));
// std::strcpy(message, (char*)name);
if (atts != NULL)
{
// atts[0] = type
// atts[1] = value
// len = strlen((char*)atts[1]);
// char *firstAttr = (char*)malloc(len*sizeof(char));
// std::strcpy(firstAttr, (char*)atts[1]);
if(strcmp((char*)name, "xs:include")==0)
{
XSDFile xsd;
xsd.ReadXSDTypes((char*)atts[1]);
}
else if(strcmp((char*)name, "xs:element")==0)
{
doElement(atts);
}
else if(strcmp((char*)name, "xs:sequence")==0)
{
//set the default values
XSDFile::sequenceMin = XSDFile::sequenceMax = 1;
if (sizeof(atts) == 4)
{
if(strcmp((char*)atts[3],"unbounded")==0)
XSDFile::sequenceMax = -1;
int i = 0;
while(atts[i] != NULL)
{
//atts[i] = name
//atts[i+i] = value
std::string name((char*)atts[i]);
std::string value((char*)atts[i+1]);
if(name=="minOccurs")
XSDFile::sequenceMin = (atoi(value.c_str()));
else if(name=="maxOccurs")
XSDFile::sequenceMax = (atoi(value.c_str()));
i += 2;
}
}
}
}
//free(message);
}
例外情况是什么?我认为XSDFile类实现了自己的迭代器,因此可以跳过某些元素的迭代。为什么不这样做呢:
std::copy(XSDFile::tagsList.begin(),XSDFile::tagsList.end(),std::back_inserter(XSDFile::tags))代码>?@johndilling是的,这样更好,但是OP的代码也应该可以工作。在运行此代码之前,您确定XSDFile::tags
为空吗?