|
// get data from wherever
unsigned char* buf = get_datafromserver();
vector<unsigned char> vbuffer;
// i want to copy the data from the buffer into the vector here
What is the most efficient way to do this?
Thank you
Thomas
modified 29-Aug-18 21:01pm.
|
|
|
|
|
if you know the length of the data in buf (lets call it bufLen, for arguments sake):
vbuffer.reserve(bufLen);
std::copy(buf, buf + bufLen, std::back_inserter(vbuffer));
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
I strongly doubt this is the most efficient way to do the copying, since the back inserter must check for overflow every time it is incremented (altough overflow won't happen, but this is something you and me know, not the program.) A speedier way is as follows:
vbuffer.resize(bufLen);
memcpy(&vbuffer[0],buf,bufLen);
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thank you. sounds great
Thomas
modified 29-Aug-18 21:01pm.
|
|
|
|
|
You want to turn the string into a vector of chars ? Why not use std::string instead ? It's true vector<char> is sometimes a better choice, I'm just curious if you're aware that std::string is an option, and you get the constructor for free.
Otherwise, I'd guess you need to step through buf, calling push_back for each char. As someone else has pointed out, std::copy will do this for you, but it's no more efficient than doing it yourself ( efficiency being what you asked for ). It's obviously neater tho.
Christian
I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002
I have given you two days - please could you let me have the details of your attorney and pastor. - Alex - 01/06/2002
|
|
|
|
|
The buffer can have null values . It is a binary stream that is read as a BYTE buffer. vector is easier to manipulate.
Thomas
modified 29-Aug-18 21:01pm.
|
|
|
|
|
You may want to use the vector member routine insert to put the data into the vector. This is at least as efficient as using std::copy, and can be more efficient.
To insert the elements into the vector (assuming that there are count elements):
vbuffer.insert( vbuffer.end(), buf, buf+count );
Using memcpy to do the copy after reserving the space will work, and may be faster, but I generally stay away from memcpy in C++ programs. It won't break anything when used with an unsigned char, but if you ever change what you are storing in the vector to a non-trivial data type, it could break.
Using insert also has the advantage of only needing a single call to do all the work.
You may want to get the book "Effective STL" by Scott Meyers. I have found this book to be very useful at learning better ways to use STL.
Best regards,
John
|
|
|
|
|
Efficiency - ooh, what a palaver. I did some timing tests on a 10 Mbyte sample and found (machine = 733MHz PIII, Windows 2000):
memcpy - 12nanoseconds/byte
std::copy with a vector output_iterator (see below) - 15nanoseconds/byte
explicit loop using vector operator[] - 18 nanoseconds/byte
std::copy with a vector insert_iterator (see below) - 27nanoseconds/byte
Given the closeness of the timings, I'd take the std::copy over memcpy anytime, as (IMHO) it's more correct when populating a vector...anyway, thats just my opinion, so hey, whatever.
(sample code for vector output_iterator:
unsigned char* buffer;
std::vector<unsigned chat> v;
v.resize(whatever the required size is);
std::copy(buffer, buffer + whatever the required size is, v);
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
How can I get the 8.3 format of a long filename ?
Thks in advance!
Appstmd
|
|
|
|
|
|
hi all,
could anybody give a hint on how to retrieve the cursor coordinate (CPoint) in a CListCtrl ? prog. running on Win CE 3.0
i want to display a pop-up menu where the user click somewhere in the List, to do so i need to get the cursor coord.
i've triied :
void C..::OnLButtonUp(UINT nFlags, CPoint point) // it only works in the View, not inside the control
void C..::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) //here there is no direct way to get the position..
any idea ?
thanx
Fred
|
|
|
|
|
Put this in your OnClickList or what ever its called.
CPoint point;
GetCursorPos( &point );
m_listCtrl.ScreenToClient( &point ); Then display your pop-up menu at point
-Raffi
|
|
|
|
|
Oops, just realize that you dont have to put the ScreenToClient part in there.
I should be:
CPoint point;
GetCursorPos( &point ); And display your pop-up menu at point .
-Raffi
|
|
|
|
|
Try:
NM_LISTVIEW *plv = ( NM_LISTVIEW * )pNMHDR;
UINT uflag ;
m_wndListCtrl.HitTest( plv->ptAction , &uflag );
if( uflag == LVHT_ONITEMICON )
....
Here plv->ptAction is the cursor coordinate and you don't need to use
ScreentToClient()
Cheers
|
|
|
|
|
thanx guys !
just a simple GetCursorPos() do it !
@+
Fred
|
|
|
|
|
I want to know how can i sense number pressed by caller using tapi 3.0 in VC.
|
|
|
|
|
Have a look at ITDigitDetectionEvent in MSDN or on the platform SDK.
Michael
Logic, my dear Zoe, merely enables one to be wrong with authority. - The Doctor
|
|
|
|
|
Hio.. I have 2 win98 machines that are having major toolbar problems.. I am using the following little function to switch out the toolbar:
void CMainFrame::SetToolbarDefault()
{
m_wndToolBar.SetButtons(NULL, 3);
m_wndToolBar.SetButtonInfo(0, ID_OPTIONS, TBBS_BUTTON|BTNS_SHOWTEXT, 0);
m_wndToolBar.SetButtonInfo(1, ID_APP_ABOUT, TBBS_BUTTON|BTNS_SHOWTEXT, 2);
m_wndToolBar.SetButtonInfo(2, ID_EXITBTN,TBBS_BUTTON|BTNS_SHOWTEXT, 3);
m_wndToolBar.SetButtonText(0,"Options");
m_wndToolBar.SetButtonText(1,"About");
m_wndToolBar.SetButtonText(2,"Exit");
m_wndToolBar.Invalidate(TRUE);
m_wndToolBar.UpdateWindow();
Invalidate(TRUE);
UpdateWindow();
} i have another function which is basicly the same which switches the toolbar to a different set of menus.. both have the following problem:
as soon as the SetToolbarDefault() gets called the menus become grey.. and they dont ever redraw.. if you move the window around, the area where the toolbar should be is all distorted, showing other things behind where the toolbar should be.. its like it never gets redrawn again, ever, until i exit the program and re-enter..
plz keep in mind that these buttons do have handlers, and they are not just greyed out as if they didnt have a message handler.. also these menus work fine under win 2000.. plz tell me someone has an idea on this one?
-dz
|
|
|
|
|
Hi, i want to let me richEdit control scroll to the very bottom whenever new text is added. Most examples here use LineScroll(1) to scroll down by 1 line whenever text is added but when your richedit has the style WS_HSCROLL turned off, then whenever the text is longer than the window width it'll need to scroll down more than one line...
I can't just do LineScroll(5) or anything like that either cause then it'll scroll down till the last line is at the top of the control, and thats not pretty either now is it..
Any ideas?
thanks.
P.S.: LineScroll doesn't even seem to work under WIN2000, i'd like to have something that works under WIN2000/WINXP as well
Kuniva
--------------------------------------------
|
|
|
|
|
I found this works:
// Give it focus
theApp.m_hwndPWOutputWindow->GetRichEditCtrl().StreamIn(SF_TEXT | SFF_SELECTION, es);
// Send some keys to cause it to move right to the bottom <-,->
theApp.m_hwndPWOutputWindow->SendMessage(WM_KEYDOWN,37,0);
theApp.m_hwndPWOutputWindow->SendMessage(WM_KEYDOWN,39,0);
if(!gbTalk)
{
theApp.m_hwndPWOutputWindow->SetFocus();
theApp.m_hwndPWOutputWindow->UpdateWindow();
}
theApp.m_hwndPWOutputWindow->UpdateWindow();
|
|
|
|
|
umm.. thanks for replying but i don't fully understand a few things here:
whats m_hwndPWOutputWindow?
whats es?
whats gbTalk for?
If you'd like to clear this up i'd be very thankful..
Kuniva
--------------------------------------------
|
|
|
|
|
i'm beginner
I search source code for display a jpeg in a dialog application with visual c++.
I have a list box with path file (c:\ttest.jpeg) and on click i search source code to show this image in a control 300*200 pixels.
Thanks a lot for your response.
please send me an email at noudard@wanadoo.Fr
nico
|
|
|
|
|
Your in luck a project of this type has receintly been submitted to code project. You can find it at http://www.codeproject.com/audio/cximagemng.asp. It is a bit much to go through but boy the things you can learn from this source.
|
|
|
|
|
if what I gave you in the first post is more than you were looking for try something a little simpler.
Like this
LVBKIMAGE lv;
ZeroMemory(&lv, sizeof(lv));
lv.ulFlags = LVBKIF_SOURCE_URL | LVBKIF_STYLE_NORMAL;
lv.pszImage = szFileName;
lv.cchImageMax = lstrlen(szFileName);
ListView_SetBkImage(hndLVCtrl, &lv);
|
|
|
|
|
2: Steven Szelei
You are right.
But what's wrong with my code, It seems be same as yours
It's me, code_cold
|
|
|
|