|
I need some help with sort function in Visual C++.
Here is the situation :
I have a variable "m_strEditData" whose type is CString. I want to sort the string and move the data to another CString "m_strResultData".
I want this to be an inline code and not called as a function. Can anyone help me with this?
Greatly appreciated.
M
|
|
|
|
|
Do you mean sorting the characters your string is composed of? If so, this should do:
#include <algorithm>
...
m_strResultData=m_strEditData;
int nLength=m_strResultData.GetLength();
LPSTR buffer=m_strResultData.GetBuffer(nLength);
std::sort(buffer,buffer+nLength);
m_strResultData.ReleaseBuffer();
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thank you.
The Sort worked but as I was testing I rank into a situation (explained below). Is there a way to solve it.
Input data---> acbde
After sort function---> abcde
This is fine. But if I enter in input data
Input data----> This is my dog
Output ===> Sort all the characters together.
Is there a way to just sort only on the first character.
The result of my example after the sort would be :
"dog is my This".
Is this easily possible?
Thank you
M
|
|
|
|
|
This can be done, but involves a little more work. Here's an outline of the method:
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
...
std::vector<std::string> v;
std::istringstream istr((LPCSTR)m_strEnterData);
for(;;){
std::string s;
istr>>s;
if(s.empty())break;
v.push_back(s);
}
std::sort(v.begin(),v.end());
m_strDisplayResult="";
for(std::vector<std::string>::const_iterator it=v.begin();
it!=v.end();
++it){
if(it!=v.begin())m_strDisplayResult+=" ";
m_strDisplayResult+=it->c_str();
} (Sort of, I haven't compiled this.)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thank you. I am going to review it and try to understand it.
I am not familiar with Vector operation but I am excited about learning it. I have a book. I will read more and will let you know.
Thank you
M
|
|
|
|
|
Thank you. I am going to review it and try to understand it.
I'm glad you decided to do so. STL can help you be more productive and write more robust code. Usually, trying to do this kind of things by hand results in off-by-one errors and mistakes like that.
In case you're interested, Christian Grauss has written several tutorials on different aspects of STL. Check them out at the STL section here at CodeProject. Good luck!
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
|
Which function in CListBox returns the selected items textstring?
|
|
|
|
|
CString buff;
GetText(GetCurSel(), buff);
GetSelCount(); will return the number of selected items in a listbox if it's a multi-selection enabled.
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
Thanks
|
|
|
|
|
CListBox::GetText;
CString str;
m_list.GetText(m_list.GetCurSel(),str);
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
|
Anyone knows a homepage with downloadable CRC64/CRC128/CRC256 C/C++ source code?
Thanks
Dominik
|
|
|
|
|
Hi All,
Is there a trick to handeling windows messages in a CButton derrived class that is owned by a CView Class??
Right now I am handeling the messages in the CView class, and I am finding it to be messy code that is not very OOP.
Any help is appreciated.
Thanks, Paul
|
|
|
|
|
Anonymous wrote:
Right now I am handeling the messages in the CView class, and I am finding it to be messy code that is not very OOP.
That's MFC for ya. What messages are you handling? It is possible to use message reflection to handle many messages in the CButton -derived class if that would be appropriate.
--------
People they come together
People they fall apart.
No one can stop us now
'cause we are all made of stars... -- Moby, We are all made of stars
|
|
|
|
|
WM_MOUSEMOVE is the message I was hoping to handle.
Started reading about reflections, but wasn't sure if that was what I needed. Is it as simple as adding an ON_WM_MOUSEMOVE_REFLECT() message handler to my CButton class? Probably NOT !!
Do I need to do anything to set up the parent window??
Thanks again
|
|
|
|
|
Anonymous wrote:
WM_MOUSEMOVE is the message I was hoping to handle.
Oh, well that's sent directly to your button anyway, so you should have no problem handling it in your CButton -derived class. The only thing i can think of that might get tricky with this would be if you wanted to know when the mouse was moved outside of the button, since you'll no longer recieve messages then. Two solutions to this are mouse capture (if the user will be holding a mouse button down) or setting a timer and periodically checking the mouse position (appropriate for "hot tracking", etc.).
--------
People they come together
People they fall apart.
No one can stop us now
'cause we are all made of stars... -- Moby, We are all made of stars
|
|
|
|
|
Hmmmm
Actually it's a CBitmapButton derrived class, but I don't believe that would matter.
This is how I created it in the view class
fbOpen.Create(_T(""), BS_OWNERDRAW, rect, GetSafeOwner(), IDC_NEW_BUTTON)
and this is my message handler in my derrived class
void CBitmapFlipButton::OnMouseMove(UINT nFlags, CPoint point)
{
AfxMessageBox("The Mouse has moved, Yahoo !!",
MB_OK);
CButton::OnMouseMove(nFlags, point);
}
Please let me know if something is wrong. Otherwise I will keep searching for an example.
Thank you for you time shog, and every one else posting.
|
|
|
|
|
Anonymous wrote:
and this is my message handler in my derrived class
And it isn't getting called? Check the message map i guess, otherwise it looks fine.
--------
People they come together
People they fall apart.
No one can stop us now
'cause we are all made of stars... -- Moby, We are all made of stars
|
|
|
|
|
Figured it out with spy++
Didn't get any WM_MOUSEMOVE messages until the cursor was over the titlebar.
Changed the buttons create function to use the this pointer inplace of GetParent() for the pParentWnd parameter and it seems to be working. Hope there are no side effects.
|
|
|
|
|
|
why ReleaseDC is not saving my information.
|
|
|
|
|
[edited - my brain works faster than my fingers]
ReleaseDC() does not save information. Please provide more info.
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
|
|
|
|
I'm making a window that acts as a menu, see:
this is my command:
ShowMenu(BOOL bFromKey, int x, int y,CWnd* pParent, HMENU hMenu)
and i have a CMenu named m_pMenu
i convert the hMenu into a CMenu:
m_pMenu->m_hMenu = hMenu;
this is ok, now i create my window using the CreateEx command, after my window is created i call another class called DrawMenuItem, this one get the size of all the menuitems stored in the m_pMenu, after get the size of the items, i assing the size to the window using the MoveWindow, after this the window is showed using the ShowWindow, then i call the ReleaseDC to make m_pMenu free for use for every class, but here's the problem when the menu is showed it call the OnPaint, in this i paint the background and i draw the menu text for the menuitems, but the m_pMenu is empty in this, why if it's supposed to be the m_pMenu of the ShowMenu command?
|
|
|
|
|
So let me get this clear, you are painting the background and text in the menu, but ...
A) you never see the text appear on the menu
B) you see the text appear on the menu, but the next time it appears the text is gone
First, I would say that if you are painting the background in the OnPaint handler, then you need to handle the OnEraseBkgnd handler as well and simply return TRUE in your implmentation of this function.
second if your answer to the above statement is choice A, I would say make sure that you are drawing in the proper coordinates for you window. It is possible that you have miscalculated the region that you should be drawing and you are not drawing on a region where the menu window exists.
If your answer is B, then I will have to ask some more questions.
Good Luck!
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
If you want the data for a DC from a window to be persistent after you call ReleaseDC, then you need to create your window with the CS_OWNDC or CS_CLASSDC style.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|