[英]Debug assertion failed with list
Hello everybody can you help me?大家好,可以帮帮我吗? the problem is that when a person enters a element from list he can insert before this element elements from another list, but I constantly knock out mistakes.
问题是,当一个人从列表中输入一个元素时,他可以在该元素之前插入另一个列表中的元素,但我不断地敲出错误。 Maybe someone will help?
也许有人会帮忙? It looks like: 3 6 7 9 user enters 6 he can create another list like 8 7 5 and output is 3 8 7 5 6 7 9 The error
它看起来像: 3 6 7 9 用户输入 6 他可以创建另一个列表,如 8 7 5 和 output 是 3 8 7 5 6 7 9错误
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
int size
int size1;
int el1;
int znach=0;
cout << "Enter size: " << endl;
cin >> size;
list<int>lis;
list<int>lis1;
list <int> ::iterator it;
int s = lis.size() / 2;
auto it1 = lis.begin();
advance(it1, s);
for (int i = 0; i <size; i++)
{
cout << "Enter " << i << " element: ";
cin >> t;
lis.push_back(t);
}
cout << "Enter element: " << endl;
cin >> el1;
for (it = lis.begin(); it != lis.end(); it++)
{
if (*it = el1)
{
znach++;
}
}
if (znach == 0)
{
cout << "There is no element;" << endl;
}
else
{
cout << "Enter size of new vector: " << endl;
cin >> size1;
for (int i = 0; i < size1; i++)
{
int t1;
cout << "Enter " << i << " element: ";
cin >> t1;
lis1.push_back(t1);
}
auto it2 = lis.begin();
while (*it2 != el1)
{
it2++;
}
--it2;
lis.splice(it2, lis1);
}
for (it = lis.begin(); it != lis.end(); it++)
{
cout << *it<<" ";
}
}
I don't know what your code is doing, and maybe there are other problems.我不知道你的代码在做什么,也许还有其他问题。 However, the runtime error you get is caused by
但是,您得到的运行时错误是由
auto it2 = lis.begin();
while (*it2 != el1)
{
it2++;
}
--it2;
When *lis.begin() == el1
then you never increment it2
and then decrement it, but you cannot decrement the begin iterator.当
*lis.begin() == el1
然后你永远不会增加it2
然后减少它,但你不能减少开始迭代器。 Usually thats just undefined, but as you compiled a debug build you got an assertion fired up that tells you what went wrong.通常那只是未定义的,但是当你编译一个调试版本时,你会得到一个断言,告诉你出了什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.