Re: const accessor to map?
On Apr 23, 10:59 am, Stefan Naewe <nos...@please.net> wrote:
On 4/23/2008 11:37 AM, nw wrote:
I figured it out. Basically use find to access the elements:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class MyMapEncap {
public:
map<string,vector<int> > mymap;
const vector<int> &getvec(string id) const {
return (*mymap.find(id)).second;
}
};
int main() {
MyMapEncap e;
e.getvec("RAW");
Crash! Boom! Bang!
}
You need to check the return value of mymap.find() !!
/S
--
Stefan Naewe stefan dot naewe at atlas-elektronik d=
ot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html=
- Hide quoted text -
- Show quoted text -
Yes and probably you should think about passing the vector as a
parameter by reference, rather than returning it from the function.
In the latter you need to worry about what to return if the string id
is not in the map.