一个关于迭代器中指针出错的问题。
#include <map>
#include <iostream>
#include <string>
using namespace std;
typedef multimap <char,string> multimapCharStr;
typedef multimapCharStr::iterator multimapIterator;
typedef multimapCharStr::reverse_iterator multimapReverseIterator;
typedef pair <char,string> pairCharString;
template <class ITERATOR>
void printMultimapItem(ITERATOR it)
{
cout < <it-> first < < ", " < <it-> second < <endl;
}
int main()
{
int cc=0;
multimapCharStr c1;
pairCharString pairs[5]={pairCharString( 'a ',string( "alt ")),pairCharString( 'a ',string( "ado ")),
pairCharString( 'b ',string( "basic ")),pairCharString( 'c ',string( "com ")),
pairCharString( 'd ',string( "dao "))};
multimapCharStr c2(pairs,pairs+5);
multimapCharStr c3(c2);
if (c1.empty())
{
cout < < "c1 is empty " < <endl;
}
else
{
cout < < "c1 is not empty " < <endl;
}
cout < < "c2(using begin,end)=: " < <endl;
multimapIterator iter1;
for (iter1=c2.begin();iter1!=c2.end();iter1++)
{
printMultimapItem(iter1);
}
cout < < "c2 (using rbegin,rend)= " < <endl;
multimapReverseIterator revIter1;
for (revIter1=c2.rbegin();revIter1!=c2.rend();revIter1++)
{
printMultimapItem(revIter1);
}
iter1=c1.insert(multimapCharStr::value_type( 'i ',string( "internet ")));
if (iter1!=c1.end())
{
cout < < "a pair of key/data was inserted in c1,*iter1= ";
printMultimapItem(iter1);
}
else
{
cout < < "pair ( 'i ',\ "internet\ ") was not inserted in c1 " < <endl;
}
c1.insert(pairs,pairs+5);
c1.insert(c1.begin(),pairCharString( 'j ',string( "java ")));
cout < < "Does c1 contain any pair with key = j? " < <endl;
iter1=c1.find( 'j ');
if (iter1!=c1.end())
{
cout < < "c1 contains pair: ";
printMultimapItem(iter1);
}
else
{
cout < < "c1 does not contain any element with key = j " < <endl;
}
cout < < "max elements which c1 can hold using current allocator = " < <c1.max_size() < <endl;
cout < < "number of elements in c1= " < <c1.size() < <endl;
c1.swap(c2);
cout < < "last key/data in c1= ";
printMultimapItem(c1.rbegin());
c3.clear();
cout < < "After calling c3.clear(),number of element in c3= " < <c3.size() < <endl;
multimapCharStr::allocator_type a1=c3.get_allocator();
multimapCharStr::key_compare kc=c1.key_comp();
cout < < "use function object kc to find less of ( 'a ', 'b ')... " < <endl;
if (kc( 'a ', 'b ')==true)
{
cout < < "kc( 'a ', 'b ')==true which means 'a ' < 'b ' " < <endl;
}
else
{
cout < < "kc( 'a ', 'b ')==false,which means 'a '> 'b ' " < <endl;
}
multimapCharStr::value_compare vc=c1.value_comp();
cout < < "use function object vc to compare char-string pairs... " < <endl;
cout < < "pairs[0](= " < <pairs[0].first < < ", " < <pairs[0].second < < ") " < <endl;
cout < < "pairs[1](= " < <pairs[1].first < < ", " < <pairs[1].second < < ") " < <endl;
if (vc(pairs[0],pairs[1])==true)
{
cout < < "pairs[0] <pairs[1] " < <endl;
}
else
{
cout < < "pairs[0]> pairs[1] " < <endl;
}
iter1=c2.upper_bound( 'c ');
cout < < "first multimap element with key > 'c '= ";
printMultimapItem(iter1);
iter1=c2.lower_bound( 'c ');
cout < < "first multimap element with key = 'c ' ";
printMultimapItem(iter1);
pair <multimapIterator,multimapIterator> pair2=c2.equal_range( 'c ');
cout < < "using c2.equal_range( 'c '),first multimap element with key> 'c '= ";
printMultimapItem(pair2.second);
cout < < "using c2.equal_range( 'c '),first multimap element with key= 'c ' = ";
printMultimapItem(pair2.first);
cout < < "number of pairs in c2 with key 'a ' = " < <c2.count( 'a ') < <endl;
c2.erase(c2.begin());
cout < < "first key/data pair of c2 is: ";
printMultimapItem(c2.begin());
c1.erase(c1.begin(),c1.end());
cout < < "after c1.erase(c1.begin(),c1.end()),number of elements in c1 = " < <c1.size() < <endl;
if (c2.erase( 'j ')==1)
{
cout < < "element with key 'j ' in c2 was erased " < <endl;
}
else
{
cout < < "c2 does not contain any element with key 'j ' " < <endl;
}
cin> > cc;
return 0;
}
源代码如上所示,程序可以正常运行,但是在编译器中观察执行了
printMultimapItem(c2.begin());
之后反向迭代器evIter1钟的指针就出错了,请问大侠这里的指针出错会对程序产生负面影响么?还有就是怎么样delete这个bug?
[解决办法]
stl容器该如何用的问题,你还是先认真学学《effective stl》吧,代码太长了,不看。
