Insert static array of struct in a vector

J

John Doe

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

But I am sure there is an easier way, maybe I should call reserve before ?
 
M

Maxim Yegorushkin

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

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

John Doe

Maxim said:
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);

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

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))
{

}
 
M

Maxim Yegorushkin

Maxim said:
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);
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
        );

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

Juha Nieminen

Maxim said:
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)
);

It's easier to use the assign() member function rather than insert().
assign() works in the same way as the constructor taking an iterator
range, and thus it's simpler to use.

(Also assign() might be more efficient if there already were some
elements in the data structure. Ok, maybe not with std::vector, but I
know in most implementation of std::list an assign() call will be more
efficient than clear()+insert(), if there were already some elements in
the list.)
 
M

Maxim Yegorushkin

  It's easier to use the assign() member function rather than insert().
assign() works in the same way as the constructor taking an iterator
range, and thus it's simpler to use.

Well spotted. I wanted to advise assign() actually, however, I could
not find it on http://www.sgi.com/tech/stl/Vector.html

I should have taken the trouble to open my ISO-IEC-14882_2003_C+
+_Standard.pdf ;)))
  (Also assign() might be more efficient if there already were some
elements in the data structure. Ok, maybe not with std::vector, but I
know in most implementation of std::list an assign() call will be more
efficient than clear()+insert(), if there were already some elements in
the list.)

I agree.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top