N
nw
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");
}
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");
}