Click here to Skip to main content
15,903,201 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralComparing STL containers of pointers Pin
Rob Caldecott7-Jul-05 5:24
Rob Caldecott7-Jul-05 5:24 
GeneralRe: Comparing STL containers of pointers Pin
Rob Caldecott7-Jul-05 6:07
Rob Caldecott7-Jul-05 6:07 
GeneralOoops... Pin
Rob Caldecott7-Jul-05 6:33
Rob Caldecott7-Jul-05 6:33 
GeneralOoops... Pin
Jörgen Sigvardsson7-Jul-05 9:56
Jörgen Sigvardsson7-Jul-05 9:56 
GeneralRe: Comparing STL containers of pointers Pin
Stuart Dootson8-Jul-05 7:45
professionalStuart Dootson8-Jul-05 7:45 
GeneralRe: Comparing STL containers of pointers Pin
Axter4-Aug-05 18:28
professionalAxter4-Aug-05 18:28 
Generaldueto Pin
Raudel6-Jul-05 5:49
Raudel6-Jul-05 5:49 
GeneralInitializing STL containers Pin
Rob Caldecott6-Jul-05 5:22
Rob Caldecott6-Jul-05 5:22 
I have a lot of places in my code where I am using a static const table that I then use for lookup purposes. For example:

static const struct
{
    LPCTSTR psz;
    UINT n;
} table[] =
{
    _T("Rob"), 1971,
    _T("Becky"), 1977,
};

...

for (int i = 0; i < sizeof(table) / sizeof(*table); i++)
{
    // Do something with a table element
    ...
}


This is all well and good, but now I am an STL fanatic I would rather using a static vector (so I can utilize find_if, for_each, etc.

My question - is there an elegant way to initialise a container whilst still making the whole thing const. For example, the following would be sweet:

class CMyData
{
public:
    LPCTSTR m_psz;
    UINT m_n;

    CMyData(LPCTSTR psz, UINT n) : m_psz(psz), m_n(n) { }
};

static const vector<CMyData> v =
{
    CMyData(_T("Rob"), 1971),
    CMyData(_T("Becky"), 1977),
};


The above doesn't work. No surprises. If instead I do something like this:

static const vector<CMyData> v;
if (v.empty())
{
    v.push_back(...); // ERROR
    v.push_back(...); // ERROR
}


The fact the vector is defined as 'const' means I can't add anything to it the first time the function is called. I guess I could instead do:

static vector<const CMyData> v;
if (v.empty())
{
    v.push_back(...);
    v.push_back(...);
}


The above works OK, but I wondered if any STL gurus had any other solutions?


The Rob Blog
GeneralRe: Initializing STL containers Pin
Michael Dunn6-Jul-05 6:31
sitebuilderMichael Dunn6-Jul-05 6:31 
GeneralRe: Initializing STL containers Pin
Rob Caldecott6-Jul-05 6:39
Rob Caldecott6-Jul-05 6:39 
GeneralRe: Initializing STL containers Pin
Rob Caldecott6-Jul-05 6:41
Rob Caldecott6-Jul-05 6:41 
Generalfind_if and function objects ('functors') Pin
Rob Caldecott5-Jul-05 3:11
Rob Caldecott5-Jul-05 3:11 
GeneralRe: find_if and function objects ('functors') Pin
Michael Dunn5-Jul-05 6:54
sitebuilderMichael Dunn5-Jul-05 6:54 
GeneralRe: find_if and function objects ('functors') Pin
Rob Caldecott5-Jul-05 22:48
Rob Caldecott5-Jul-05 22:48 
GeneralRe: find_if and function objects ('functors') Pin
Jörgen Sigvardsson5-Jul-05 12:37
Jörgen Sigvardsson5-Jul-05 12:37 
GeneralRe: find_if and function objects ('functors') Pin
Rob Caldecott6-Jul-05 0:21
Rob Caldecott6-Jul-05 0:21 
GeneralMFC events, in ATL with MFC support controls Pin
greekgoddj4-Jul-05 22:01
greekgoddj4-Jul-05 22:01 
Generalhandling WM_MOUSEWHEEL between two views Pin
retro_coder4-Jul-05 1:01
retro_coder4-Jul-05 1:01 
GeneralRe: handling WM_MOUSEWHEEL between two views Pin
Cedric Moonen4-Jul-05 23:13
Cedric Moonen4-Jul-05 23:13 
GeneralRe: handling WM_MOUSEWHEEL between two views Pin
Jörgen Sigvardsson6-Jul-05 1:01
Jörgen Sigvardsson6-Jul-05 1:01 
GeneralRe: handling WM_MOUSEWHEEL between two views Pin
Cedric Moonen6-Jul-05 4:00
Cedric Moonen6-Jul-05 4:00 
GeneralRe: handling WM_MOUSEWHEEL between two views Pin
Jörgen Sigvardsson6-Jul-05 1:08
Jörgen Sigvardsson6-Jul-05 1:08 
GeneralSorry! An error occured while generating the object Pin
AdyOS1-Jul-05 0:12
AdyOS1-Jul-05 0:12 
GeneralRe: Sorry! An error occured while generating the object Pin
toxcct1-Jul-05 0:23
toxcct1-Jul-05 0:23 
GeneralRe: Sorry! An error occured while generating the object Pin
AdyOS1-Jul-05 0:44
AdyOS1-Jul-05 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.