Maxim Yegorushkin
10/31/2008 11:14:00 AM
On Oct 31, 10:52 am, John Doe <mos...@anonymous.org> wrote:
> Maxim Yegorushkin wrote:
> > On Oct 31, 10:37 am, John Doe <mos...@anonymous.org> wrote:
> >> Hi,
>
> >> I have a static array of struct defined like this :
>
> >> CViewMgr::ViewInfo g_ViewInfo[] =
> >> {
> >> { EMainView, ECreateOnce, IDR_MAINFRAME, RUNTIME_CLASS(CMainView),
> >> NULL,0, 0 },
> >> { EWelcomeView, ECreateAndDestroy, IDR_MENU_OKCANCEL,
> >> RUNTIME_CLASS(CWelcomeView), NULL,0, 0 },
>
> >> };
>
> >> I would like to put all these declarations in a vector how can I do it ?
> >> typedef std::vector<ViewInfo> ViewList;
> >> ViewList viewList;
>
> >> soemthing like :
>
> >> for (int i = 0; i < _countof(g_ViewInfo); i++)
> >> viewList.push_back(g_ViewInfo[i]);
>
> >> But I am sure there is an easier way, maybe I should call reserve before ?
>
> > Try this:
>
> > ViewList viewList(
> > g_ViewInfo
> > , g_ViewInfo + sizeof g_ViewInfo / sizeof *g_ViewInfo
> > );
>
> > --
> > Max
>
> So I suppose it means I have to put init in constructor because my list
> is a variable member.
>
> CViewMgr::CViewMgr():
> m_viewList(g_ViewInfo, g_ViewInfo + _countof(g_ViewInfo))
> {
>
> }
This syntax invokes the constructor of the vector which accepts two
iterators.
There is also vector::insert() which accepts iterators. It is
perfectly fine to append to an empty vector as well:
viewList.insert(
viewList.end()
, g_ViewInfo
, g_ViewInfo + _countof(g_ViewInfo)
);
--
Max
--
Max