【发布时间】:2026-02-07 04:50:01
【问题描述】:
我为地图上的不同操作编写了程序。
下面给出了我的程序的示例代码。
在运行此代码时,我收到一个错误,例如地图擦除超出范围异常。
请帮我解决这个问题。
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: iterator m1_Iter;
map <int, int> :: const_iterator m1_cIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_cIter = m1.end( );
m1_cIter--;
cout << "The value of the last element of m1 is:\n"
<< m1_cIter -> second << endl;
m1_Iter = m1.end( );
m1_Iter--;
m1.erase ( m1_Iter );
m1_cIter = m1.begin( );
m1_cIter--;
m1.erase ( m1_cIter );
m1_cIter = m1.end( );
m1_cIter--;
cout << "The value of the last element of m1 is now:\n"
<< m1_cIter -> second << endl;
getchar();
}
【问题讨论】:
-
您应该尝试在您的代码中添加一些调试
couts 以帮助追踪您的问题。我们通常不会为您调试代码。找出问题,然后提出问题。 -
我在这部分遇到运行时错误 m1_cIter = m1.begin( ); m1_cIter--; m1.erase ( m1_cIter );