|
std::find(v.begin(),v.end(),value);
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hello Joaquín ...
my pal
thanks for your reply ...
But could u tell me how could i get the location of value in the vector ?
I know, i must check the return value of find function ...
But in which kind of variables ?
I must use iterator ?
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
std::find returns an iterator pointing to the position within the vector where the value was found. If the value is not found, end() is returned. Use std::distance in <iterator> to easily determine the location within the vector.
// warning untested code
std::vector<type>::const_iterator it = std::find(v.begin(), v.end(), value);
if (it != v.end())
{
std::vector<type>::differenc_type loc = std::distance(v.begin(), it);
}
Phil Boyd
MCP
CPT, AR
You may be gone, but we will never forget your sacrifice.
"Proud to be an American..." Lee Greenwood
|
|
|
|
|
Hello all,
Thanks for the dialog tricks answers. They were very helpful. Thanks a bunch.
So now I got more questions:
1)This code from MSDN help files
void CMainFrame::OnFileNewchild()
{
CMDIChildWnd* pMDIChildWnd = new CMDIChildWnd;
VERIFY(pMDIChildWnd->Create(
NULL, // standard CMDIChildWnd class
_T("My MDIChildWnd"), // caption of MDI child window
WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW, // window styles
rectDefault, // default rectangle size
this)); // parent window; can be NULL
// the default PostNcDestroy handler will delete this object when destroyed
}
This will create a new MDI Child Window if I paste it into the Mainframe.cpp class of my MDI app. What I'd like to know, is how to pass the messages to it to get it to do something?
I'd like it to open Bitmaps, and I have a bitmap file viewer that will open Bitmaps into the default MDI child windows. How do I send a message to the onDraw function to draw into THIS custom MDI child window?? Basically, how can I get this Window to do somethign apart from appear?
If any of you know how to go about doing this and on top of that, want to let ME know , I'd be mega-grateful. I really want to know how to do this. Does this involve creating a new Doc and view class? Arghhh...I'm kinda lost and some diretion would be nice
|
|
|
|
|
NickOne wrote:
This will create a new MDI Child Window if I paste it into the Mainframe.cpp class of my MDI app. What I'd like to know, is how to pass the messages to it to get it to do something?
If it'll always do the same thing, then put the code in the child windows constructor. Otherwise, there is code on MSDN to iterate through all open documents ( that is to say, I forget how to do it, but that is where I learned )
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Hey all,
I have been trying to figure out how to write a report and print it using Microsoft word from my Visual C++ project. Simply put all I want to do is write lines of text to word document and then print the document all for the user.
I have already included the Microsoft Word type library via the Class Wizard, but I have no idea on where to begin as far as using this information. I was wondering if anyone could point me to examples along this line... Or if anyone has an explanation of the type libarary classes that the class wizard created for me.
Any help and any leads are much appreciated...
Thanks,
Brian
Remember the next time something breaks that actions speak loud than words, so remember to use gestures while discussing the properties of clean code with your computer.
|
|
|
|
|
Hey Guys
I need some help with Multiple Inheritance in MFC like HOW THE HELL DO YOU DO IT?!?!?!?
I would like to create a class derived from an MFC class say CView and a generic class i have written which is a standard class deriverd from nothing. I have done it and got it to compile but it will crash when MFC calls the ISKindOf statment.
Any links to tutorials on this would be extremly useful.
|
|
|
|
|
MFC's RTTI (CObject::IsKindOf and friends) doesn't support multiple inheritance. You can get better results with C++ RTTI - dynamic_cast is the way to go.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
MFC RTTI is older than standard C++ RTTI and is more limited and manual. If MFC does the call to IsKindOf when it crashes, then you may not ba able to replace it with C++ RTTI (dynamic_cast) as suggested in another reply.
As an alternative, you may check your derivation order. Does your class first derives from the MFC class then from your class (which should probably not derives from CObject). If not, changing that may be a fix...
It is possible that MFC RTTI rely on the fact that the derived class start at the same adress as the base class but I am not sure that there is such a restriction. It's just a guess.
And does your class is properly defined for MFC RTTI uses? Maybe you should add some MFC macros in your derived class. I don't know... I think that it is optional as long as the information is not required. But maybe the problem is that the information is required by document templates to create the view.
Philippe Mori
|
|
|
|
|
Is IsKindOf duplicated or something? If not, I see no reason why what you describe should fail. Some code would be most helpful.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I have a function which is supposed to fill in abc:
MyFunc(char* abc)
{
char* val = "aaa";
strcpy(abc,val);
}
Why do I have to strcpy?
If I just do:
{
char* val = "aaa";
abc = val;
}
then there is garbage in abc when it returns. WHat exactly is happening if you do it my second way? Why doesnt it work?
|
|
|
|
|
You can't do that and teh reason why is that in your second implementation you are trying to set the value of the pointer and not of the character array. And you need to actaully copy the memory containg the "AAA" string to the memory that the pointer points to.
Hope this helps
Peter
|
|
|
|
|
in the second, you are assigning the address of a local variable (val is local to the function) to a non-local variable. a local address is meaningless when used outside the function.
-c
To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses. /. #3848917
|
|
|
|
|
Many thanks! I think I get it.
So if I had
func(int & abc)
{
int a=9;
abc=a;
}
then when called like
int zz;
func(zz);
zz will be equal to 9.
Is this correct? And we didnt have to copy anything because we're not dealing with addresses, but values.
Thanks again,
ns
|
|
|
|
|
Yep, that's correct.
In your previous strcpy() question, you must ensure that the target string has enough storage allocated. For example, this will fail:
char strMyString [8];
myFunc (strMyString);
...
void myFunc (char* pString)
{
strcpy (pString, "Hello, world!");
}
/ravi
Let's put "civil" back into "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Function scope would be a good place to start.
You wrote the following function...
ns wrote:
MyFunc(char* abc)
{
char* val = "aaa";
abc = val;
}
It has to do with both pointers and with memory allocation.
First by reassigning pointers such as:
ns wrote:
abc = val
you are now pointing the incoming character pointer to the same memory as 'val'. Remember though, there are two things going on here.
First: Function scope does not allow the changes to abc to be reflected to the caller. The actual value that is passed in as 'abc' is not stored at the same location as the orginal 'abc'. The following code trys to demonstrate the timing and values of the pointers.
Main
{
char* abc = "Home";
// abc = memory addr = 0x00000a, *abc (contents of abc or memory 0x0000a is "H")
MyFunc(abc);
// FUNCTION EMBEDDED TO DEMOSTRATE TIMING
MyFunc(char* abc)
{
char* val = "aaa";
// val = memory addr = 0x01000, *val (contents of val) is "a"
// abc = memory addr = 0x0000a, *val (contes of abc) is "H"
abc = val;
// val = memory addr = 0x01000, *val (contents of val) is "a"
// abc = memory addr = 0x01000, *val (contes of abc) is "a"
}
// abc is reset by the stack back to the original. This is function scope.
// abc = memory addr = 0x00000a, *abc (contents of abc or memory 0x0000a is "H")
}
Second even if you corrected this to pass a pointer to a pointer:
(pass in a pointer to the pointer where the memory for the string is)
pabc->abc->"The string"
pabc = 0x02000 which is the memory addr of abc
abc = 0x0000a which is the memory where the string resides.
MyFunc(char** pabc)
{
char* val = "aaa";
*pabc = val;
}
It changes to pabc->val->"aaa" and pabc->abc->"aaa" while still in the function call. However remember that the function releases its stack values when it exits so we cannot guarantee that someone else will not write to that memory. So the memory that was at the orginal abc pointer is still allocated, but the memory at val or the new abc is released and can be unexpected garbage.
When you use:
ns wrote:
strcpy(abc,val)
you are physically reassigning memory from one place to another. You are retaining the orginal area in memory for each string and moving the contents. A very simple rewrite of strcpy is this:
strcpy(char* a, char* b)
{
// look for an ending null termination
while (*b != '0')
{
*a++ = *b++;
}
}
Notice it is changing the value of what it is pointing to and not where it is pointing. This allows it to move or copy the string in to memory that exists and across the stack reset from functions. Also notice I can increment the pointers and not affect the values that were passed in because of function scope.
Just to let you know I hate text and wish I could draw some pictures, but I tried my best to illustrate the point in English. Let me know if you need more clarification or maybe someone else can clarify what I said.
Brian
If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.
|
|
|
|
|
Generally, it is better to uses std::string class instead of using raw pointers. Far more easier to uses and less error-prone.
And for the rest, many people have already answer the question.
Philippe Mori
|
|
|
|
|
Even though MS is the most resourceful software comapny in the world and hires the most accadmecally qualified and supposedly the most intelligent developerers, it does not produce innovative ,imaginative or quality software on a consistant basis. Instesd it produces buggy software tested on it's smug users through a a number of versions. Not that MS software isn't good or the company is evil, but with its awesome financial resources, though the company could do better,it doesnt. Why?
|
|
|
|
|
troll-1 wrote:
Why?
Because they didn't hire you? Anyway, VC board isn't a place to ask this kind of question. Go to lounge or soapbox instead.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Hello,
I'm here to ask what message is sent when the dialog bar is moved to a position where it can be docked, because I want to change its appearance and size depending on the side it gonna be docked.
thanks,
Guy.
|
|
|
|
|
Hi,
i want to write two programs, which are able to switch to the other program (if they are running). Both programs have a button. If you press this button:
1. The other program is launching if it is not running.
2. If the other program is running (multiple instance possible), the window of one of these instances should be pop to the front of the desktop.
Has anybody an idea?
Ciao
f.o.b
|
|
|
|
|
The most important thing is to get a handle to teh window in the other application. You can do this with GetWindow. You may have to do some searching through all of the windows, but you will be able to find the window in the other app with this API.
Next you can call SetForegroundWindow on that window handle. This will bring that window to teh foreground and give it keyborad access.
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!
|
|
|
|
|
Do a search for AttachThreadInput in here over the last weeks or so.
There are issues with SetForegroundWindow that must be worked around
for your plan to succeed. (unless you still run Win95 )
|
|
|
|
|
I want to get the keyvalue out:
void DLLGetValue(int imageIndex,CString key,char* keyvalue)
{
if (imageIndex == 0)
{
if (key=="abc") value="aa";
if(key=="def")value = "ss";
if (key == "aaa")value = "dd";
}
strcpy(keyvalue, value);
}
and its called like:
std::set<cstring>::iterator it = pDoc->m_keySet.begin();
for (; it != pDoc->m_keySet.end()
{
char* p = new char[2]; p[0] = p[1] = '\0';
int size=30;
if (size)
{
delete[] p;
p = new char[size + 1];
DLLGetValue(imageIndex,*it,p);
}
When p comes back, its full of garbage. WHat did I do wrong?
Thanks,
ns
|
|
|
|
|
From the top of my head - you should have at least three character buffer for "aa", "ss" or "dd" - don't forget about trailing null character. However, why on Earth would one mix these manual, low-level, stinky character operations with STL? Can't you just use string& as keyvalue?
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|