64,654
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<list>
using namespace std;
int main()
{
std::list<int > nlist;
std::list<int >::iterator it = nlist.begin();
std::list<int >::iterator ite = nlist.end();
//nlist.push_back(10);
//printf("list.begin = %d\n", *it);//error if first get begin and then push_back
it++;
if(it==ite)
{
printf("list::end");
}
return 0;
}
//output
/*
/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/debug/safe_iterator.h:224:
error: attempt to increment a past-the-end iterator.
Objects involved in the operation:
iterator "this" @ 0x0xffd37c84 {
type = N11__gnu_debug14_Safe_iteratorIN10__gnu_norm14_List_iteratorIiEEN15__gnu_debug_def4listIiSaIiEEEEE (mutable iterator);
state = past-the-end;
references sequence with type `N15__gnu_debug_def4listIiSaIiEEE' @ 0x0xffd37c84
}
Disallowed system call: SYS_kill
*/
#include<iostream>
#include<list>
using namespace std;
int main()
{
std::list<int > nlist;
;
std::list<int >::iterator it = nlist.begin();
nlist.push_back(1);
printf("nlist %d\n", *it);
return 0;
}
//output
/*
/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/debug/safe_iterator.h:181:
error: attempt to dereference a past-the-end iterator.
Objects involved in the operation:
iterator "this" @ 0x0xffc73294 {
type = N11__gnu_debug14_Safe_iteratorIN10__gnu_norm14_List_iteratorIiEEN15__gnu_debug_def4listIiSaIiEEEEE (mutable iterator);
state = past-the-end;
references sequence with type `N15__gnu_debug_def4listIiSaIiEEE' @ 0x0xffc73294
}
Disallowed system call: SYS_kill
*/
#include<iostream>
#include<list>
using namespace std;
int main()
{
std::list<int > nlist;
nlist.push_back(1);
nlist.push_back(2);
nlist.push_back(3);
nlist.push_back(4);
std::list<int >::iterator it = nlist.begin();
std::list<int >::iterator ite = nlist.end();
nlist.push_back(5);
while(it !=ite )
{
printf("nlist %d\n", *it++);
}
return 0;
}
//output
/*
nlist 1
nlist 2
nlist 3
nlist 4
nlist 5
*/
这两段代码的差别在于是否是在std::list为空时先获取了begin,但差别这么大??