程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c++-一個簡單的C++程序,第二種情況出現錯誤。

c++-一個簡單的C++程序,第二種情況出現錯誤。

編輯:編程綜合問答
一個簡單的C++程序,第二種情況出現錯誤。

void func(forward_list &lst, string s1, string s2)
{
auto current = lst.begin();
auto prev = lst.before_begin();
while (*current != s1 && current != lst.end())
prev = current++;
if (current != lst.end())
lst.insert_after(current, s2);
else
lst.insert_after(prev,s2);
}
第一中情況無差錯,第二種情況出現forward_list iterator not dereferencable的提示,我
的原意是用prev存儲指向最後一個元素的迭代器。

最佳回答:


此題由我自己回答吧,樓上全答錯了。while循環的判斷條件中當current到達end()後對end()進行了取值操作,因此引發錯誤。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved