关于find()为找到结果返回值的有关问题
关于find()为找到结果返回值的问题

前几次调用没有问题,后面出现异常是不是因为find()没有找到对象所以没有给迭代器it赋值?
那么我想加一个if判断,如果not found就返回0,该怎么做?
也就是说if的条件该如何写??
------解决方案--------------------
至于iterator 的 比较,参考一下 msdn
#include "stdafx.h"
#include <iostream>
#include <map>
using namespace std;
bool fncomp(char lhs,char rhs)
{
return lhs<rhs;
}
struct classcomp
{
bool operator()(const char& lhs,const char& rhs)
{
return lhs<rhs;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
map<char,int> mymap;
mymap['a']=10;
mymap['b']=60;
mymap['c']=30;
mymap['d']=90;
mymap['e']=50;
map<char,int> second(mymap);
map<char,int> third(mymap.begin(),mymap.end());
map<char,int,classcomp> fourth;
bool(*fn_pt)(char,char)=fncomp;
map<char,int,bool(*)(char,char)> fifth(fn_pt);
map<char,int>::key_compare key_comp;
map<char,int>::iterator it;
it=mymap.begin();
for (it;it!=mymap.end();it++)
{
cout<<it->first<<":"<<it->second<<endl;
}
cout<<"================================="<<endl;
second.clear();
second['a']=1002;
second['b']=10023;
while (!second.empty())
{
cout << second.begin()->first << " => ";
cout << second.begin()->second << endl;
second.erase(second.begin());
}
cout<<"================================="<<endl;
mymap.insert(pair<char,int>('f',100) );
mymap.insert(pair<char,int>('g',200) );
cout<<"f => " <<mymap.find('f')->second<<endl;
cout<<"g => " <<mymap.find('g')->second<<endl;
cout<<"================================="<<endl;
key_comp=mymap.key_comp();
cout << "mymap contains:\n";
char highest=mymap.rbegin()->first; // key value of last element
it=mymap.begin();
do {
cout << (*it).first << " => " << (*it).second << endl;
} while ( key_comp((*it++).first, highest) );
cout << endl;
return 0;
}
------解决方案--------------------
find返回值和xxx.end() 作比较(==, !=)
前几次调用没有问题,后面出现异常是不是因为find()没有找到对象所以没有给迭代器it赋值?
那么我想加一个if判断,如果not found就返回0,该怎么做?
也就是说if的条件该如何写??
------解决方案--------------------
至于iterator 的 比较,参考一下 msdn
#include "stdafx.h"
#include <iostream>
#include <map>
using namespace std;
bool fncomp(char lhs,char rhs)
{
return lhs<rhs;
}
struct classcomp
{
bool operator()(const char& lhs,const char& rhs)
{
return lhs<rhs;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
map<char,int> mymap;
mymap['a']=10;
mymap['b']=60;
mymap['c']=30;
mymap['d']=90;
mymap['e']=50;
map<char,int> second(mymap);
map<char,int> third(mymap.begin(),mymap.end());
map<char,int,classcomp> fourth;
bool(*fn_pt)(char,char)=fncomp;
map<char,int,bool(*)(char,char)> fifth(fn_pt);
map<char,int>::key_compare key_comp;
map<char,int>::iterator it;
it=mymap.begin();
for (it;it!=mymap.end();it++)
{
cout<<it->first<<":"<<it->second<<endl;
}
cout<<"================================="<<endl;
second.clear();
second['a']=1002;
second['b']=10023;
while (!second.empty())
{
cout << second.begin()->first << " => ";
cout << second.begin()->second << endl;
second.erase(second.begin());
}
cout<<"================================="<<endl;
mymap.insert(pair<char,int>('f',100) );
mymap.insert(pair<char,int>('g',200) );
cout<<"f => " <<mymap.find('f')->second<<endl;
cout<<"g => " <<mymap.find('g')->second<<endl;
cout<<"================================="<<endl;
key_comp=mymap.key_comp();
cout << "mymap contains:\n";
char highest=mymap.rbegin()->first; // key value of last element
it=mymap.begin();
do {
cout << (*it).first << " => " << (*it).second << endl;
} while ( key_comp((*it++).first, highest) );
cout << endl;
return 0;
}
------解决方案--------------------
find返回值和xxx.end() 作比较(==, !=)