我嘗試從函數獲取迭代器,然後從列表中刪除元素。std :: list :: erase(iter)異常處理C++
- 有什麼辦法可以捕捉到這個異常嗎?
- 有什麼辦法來知道迭代器是否有效?
當我運行無效的迭代器的程序,它拋出以下異常:
Exception thrown: read access violation.
_Right.**_Myproxy** was 0xCCCCCCCC. occurred
我FUNC是:
list<AccountStruct>::const_iterator SearchAccount(list<AccountStruct>& Acc,string p)
{
for (list<AccountStruct>::const_iterator j = Acc.begin(); j !=Acc.end(); ++j)
{
if ((*j).phone == p) return j;
}
}
void RemoveAccount(list<AccountStruct>& Acc)
{
string Phone;
cout << "Enter Phone Number ->" << endl;
cin >> Phone;
//cout << "Searching..." << endl;
list<AccountStruct>::const_iterator iter = SearchAccount(Acc,Phone);
try
{
Acc.erase(iter);
}
catch()// <- What to put inside??
{
cout << "No Phone Number;" << endl;
}
}
你的時間會花在弄清楚是什麼導致了訪問衝突。 'SearchAccount'是否將迭代器返回給函數local'list'? – Praetorian
這不是C++異常,因此您無法捕捉它。只有當'SearchAccount'返回另一個列表的迭代器時,您纔有此行的未定義行爲。 – Rakete1111
我在問題 – axcelenator