Connecting Tech Pros Worldwide Forums | Help | Site Map

const accessor to map?

nw
Guest
 
Posts: n/a
#1: Jun 27 '08
Hi comp.lang.c++,

I'm using map to store a bunch of vectors. I'd like to be able to
return const references to the vectors from my object. However I can't
see a way of doing this with map, as the vectors aren't const. Is
there a way round this, or a better container I should be using?

Advice appreciated.

Example follows:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

class MyMapEncap {
public:

map<string,vector<int mymap;

const vector<int&getvec(string id) const { // Doesn't work because
this isn't allow to be const
// I
want to keep it const is there a work around?
return mymap[id];
}
};

int main() {
MyMapEncap e;

e.getvec("RAW");

}


nw
Guest
 
Posts: n/a
#2: Jun 27 '08

re: const accessor to map?


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");

}

Stefan Naewe
Guest
 
Posts: n/a
#3: Jun 27 '08

re: const accessor to map?


On 4/23/2008 11:37 AM, nw wrote:
Quote:
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!
Quote:
>
}
You need to check the return value of mymap.find() !!

/S
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot 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
Harsh Puri
Guest
 
Posts: n/a
#4: Jun 27 '08

re: const accessor to map?


On Apr 23, 10:59*am, Stefan Naewe <nos...@please.netwrote:
Quote:
On 4/23/2008 11:37 AM, nw wrote:
>
>
>
>
>
Quote:
I figured it out. Basically use find to access the elements:
>
Quote:
#include <iostream>
#include <vector>
#include <map>
>
Quote:
using namespace std;
>
Quote:
class MyMapEncap {
* public:
>
Quote:
* map<string,vector<int mymap;
>
Quote:
* const vector<int&getvec(string id) const {
* * return (*mymap.find(id)).second;
* }
};
>
Quote:
int main() {
* MyMapEncap e;
>
Quote:
* e.getvec("RAW");
>
Crash! Boom! Bang!
>
>
>
Quote:
}
>
You need to check the return value of mymap.find() !!
>
/S
--
Stefan Naewe * * * * * *stefan dot naewe at atlas-elektronik dot 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.
Closed Thread


Similar C / C++ bytes