Hi Guys, I have recently came across one scenario where I have to insert a user defined key in a c++ map. that class looks like this class key { int id; string name; }; map is like map I'll overload "operator<" function and will sort out based on "id" which will works fine, but the condition is like, if there are 2 entry with same ID but with a different name, for ex. {"50", Jack} and {"50", John} As both the values have SAME ID and different name, map should make an entry for both. Only in the case of combination of same ID and same name, it shouldn't allow the entry. bool operator<(key& temp) { if(id < temp.id) { return true; } if( id == temp.id) return name < temp.name; } its not working properly, its overriding the last entry if same ID is there. Please help me out
Amrit Agr
Posts
-
Inserting a user defined key in a C++ map -
MultiThreading/Synchronization related problemHi Friends, I have been asked this below question in a interview related to MultiThreading/Synchronization I don't have much expertise in the same area. Please help me with the solution. Thanks in Advance Problem-> Develop a C++ program to demonstrate threading and synchronization. Char code generated by main process on press of key. (int iChar = getch(); ) Application will accept key between 0 to 9 only. Three threads are spawned by main process to display key pressed Any one of the thread will display the Number key pressed and and Thread ID If No key is pressed Threads will wait for input. Process will be terminated if Key '0' is pressed.
-
VC++ solution Klocwork code analysis is getting stuckHi Friends, I am using VS2015 and having a solution having 33 VC++ projects. I am using Klocwork insight 11.0 for code analysis work. while running it, its getting stuck on any of the file. I tried re-installing Klocwork and restarting visual studio as well. Please help me out if anyone have any idea. Regards, Amrit
-
How explicit , volatile and mutable keywords works INTERNALLY in C++ ?Hi Friends, I have been asked in interviews like "How explicit , volatile and mutable keywords works INTERNALLY in C++ ?" Please let me know. Rgeards, Amrit
-
dotted rectangle is not being cleared if any empty string added in MFC combobocHi Freinds, I have combobox control in my mfc application. Here I am adding an item with an empty string and 2 more item with some txt. if i am navigating through the items, if empty string get selected, it draws a dotted boundary, but when other item is getting selected, dotted boundary of empty string is not getting cleared. So basically I can see 2 items selected at the same time. please help me out. Regards, Amrit
-
How to implement Ctrl + tab to navigate between tabs in a CTabCtrlThanks David :)
-
How to implement Ctrl + tab to navigate between tabs in a CTabCtrlHi guys, In my application I had a CTabCtrl object & it has 2 tab window. I want to navigate between these tabs window via Ctrl + TAB. Currently i can do the same in preTranslateMsg i.e. to look for keydown messages & filter it out ctrl + tab key out of it & calling setcusel(). I need to know if there is any other way that I can implement the same functionality. Thanks, Amrit
-
Comboboxx issue in MFCI have a combobox in a mfc application. I created it at runtime with following code - if (!m_sortBox.Create(WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, rect, this, eSortBox)) return FALSE; Now the requirements, if user is selecting an item from combobox like , if combo box have focus & user is pressing up/down arrow key, it should NOT update the data. If user is pressing enter key after selecting a item, it should update data. So i didn't handled OnSelchange here. For enter key requirement, I checked enter key event in preTranslateMsg & checking if combo box have focus, it should trigger the function, who will eventually update the data. if (pMsg->message == WM_CHAR && pMsg->wParam == VK_RETURN) { CWnd *pActiveWnd = CWnd::FromHandle(GetFocus()->GetSafeHwnd()); CWnd* pcbSortBoxWnd = CWnd::FromHandle(m_sortBox.GetSafeHwnd()); //If sort combo box has focus and user press Enter key, it should trigger OnComboSelChange event //Eventually it will update the data. if (pcbSortBoxWnd == pActiveWnd) OnSortChange(); } I also handled ON_CBN_CLOSEUP(eSortBox, OnSortChange) So that mouse functionality will work(because with mouse, data should get update) Now my logic is working but its crashing in some cases. Like - If I press Alt + Down arrow key, which will expand combobox, I select an item(with help of arrow keys) and press enter. Sometimes its getting crash. please help me out. Regards, Amrit Agrawal
-
flicker issue in OnpaintHi Guys, I am getting flicker while painting a border on control, bannerbar(its derived from CWnd only). So the case if control is getting focus , border should get paint otherwise not. I am handling OnsetFocus and OnKillfocus and calling Paintborder() in which I checked if banner got focus(by checking m_BannerHasFocus flag) i will call parent->InavlidateRect() here i passed banner rect as argument. In Onpaint I am taking PS_DOT pen (if its have focus) and PS_NULL if don' have focus. I am getting flicker while its getting/loosing focus Even i pass FALSE parameter in InvalidateRect, which will not erase background. Please help me out. I pasted source code here. Thanks void CBannerBar::OnSetFocus(CWnd* pOldWnd) { CBaseWnd::OnSetFocus(pOldWnd); // TODO: Add your message handler code here m_BannerHasFocus = true; PaintBannerBorder(); } void CBannerBar::OnKillFocus(CWnd* pNewWnd) { CBaseWnd::OnKillFocus(pNewWnd); // TODO: Add your message handler code here m_BannerHasFocus = false; PaintBannerBorder(); } void CBannerBar::PaintBannerBorder() { //Invalidate the control in order to hide the focus rectangle. CRect rect; GetWindowRect(&rect); CWnd* pParent = GetParent(); if (!rect.IsRectEmpty() && pParent != NULL) { pParent->ScreenToClient(rect); pParent->InvalidateRect(rect, FALSE); } } void CBannerBar::OnPaint() { CPaintDC dc(this); // device context for painting // create a brush of the background color and paint the window with it CBrush brush (BACK_COLOR); dc.FillRect(&dc.m_ps.rcPaint, &brush); brush.DeleteObject(); CDC* pDC = GetWindowDC(); CRect rect; GetWindowRect(&rect); rect.OffsetRect(-rect.left, -rect.top); COLORREF dColour = GetSysColor(COLOR_HIGHLIGHT); if (m_BannerHasFocus) { dColour = RGB(255, 0, 0); } CPen pen(m_BannerHasFocus ? PS_DOT : PS_NULL, 1, dColour); CPen* penOld = (CPen*)pDC->SelectObject(&pen); pDC->SetBkMode(TRANSPARENT); pDC->MoveTo(rect.left, rect.top); pDC->LineTo(rect.right - 1, rect.top); pDC->LineTo(rect.right - 1, rect.bottom - 1); pDC->LineTo(rect.left, rect.bottom - 1); pDC->LineTo(rect.left, rect.top); pDC->SelectObject(penOld); ReleaseDC(pDC); // Do not call CBaseWnd::OnPaint() for painting messages }
-
First button of toolbar control get activated after another button get executedHi Friends, I am trying to set focus on the first button in a toolbar control if a key combination like ALT + SHIFT + A is pressed. I am using preTranslate Message, like this if ( pMsg->message == WM_SYSCHAR && (GetKeyState(VK_SHIFT) & 0x8000) && (pMsg->wParam == 'a' || pMsg->wParam == 'A') ) { m_wndToolBar.SetFocus(); } Now. I am getting focus on first button of toolbar. I clicked SPACEBAR and this button get pressed. It open a dialog , after my operation I closed it. After that, Unfortunately second button get activated. I pressed spacebar again.It will pressed that button and called related function , which will open another dialog. After my operation, I closed it . Now the focus went to First button. According to project requirement the focus must retain to the last clicked button. I checked in project code, nowhere first button getting activated explicitly. Please help me out. Regasrd, Amrit Agrawal
-
Inheritance tricky questionHi Sascha, I am not able to understand why we need to take address of Bar objects in line Foo *myBaz[3] = { &myBar[0], &myBar[1], &myBar[2] }; and why we need to pass to pass the address of myBaz to a Foo** ( Double Pointer )? How surprisingly its working fine now? One more thing I still didn't get it hpw it prints 1 for 2nd iteration of Bar object. If I am not wrong, even if we are passing Bar address to Foo pointer, it wil call the geti function of Foo only ? so It should print variable i value that is 0 ? Please clear my doubt. Thanks
-
Inheritance tricky questionHi Friends, I came through a interesting question in inheritance. Here is the chunk of code and out put is given below. // Inheritance tricky.cpp : Defines the entry point for the console application. // class Foo { private: int i; public: Foo() { i = 0; } void geti() { cout << i << endl; } }; class Bar : public Foo { private: int j; public: Bar() { j = 1; } void getj() { cout << j << endl; } }; void display(Foo* obj, int ctr) { for (int i = 0; i < ctr; i++) { ((Foo*)obj + i)->geti(); } } int _tmain(int argc, _TCHAR* argv[]) { Foo myFoo[3]; display(myFoo, 3); Bar myBar[3]; display(myBar, 3); return 0; } and Output is 0 0 0 0 1 0 The first 3 line is 0 that's fine. But how the 2nd last is 1 ??? Is there is any way that I can get all 0 in output if I am still executing this code Bar myBar[3]; display(myBar, 3); PLease help me out. Thanks in Advance. Regards, Amrit
-
How to swap address of pointers of 2 variables without using a temporary variable?Hi guys, I have asked this question in an interview How to swap address of pointers of 2 variables without using a temporary variable? As I know the arithmetic operation is not allowed between 2 pointers. So what is the trick ? Pls help me out. Thanks in advance. Regards, Amrit
-
int **p = (int**)new int(5); Confusing !!!Hey Guys, I am trying to execute the following chunk of code. int **p = (int**)new int(5); cout << *p; I am getting output 00000005 As per my understanding , we don't need to typecast return value from new, as it automatically does. Like in the following statement int *ap = new int(20); We don't need to do that, So why I need to do in case of double pointer?? Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005 Please someone help me. Thanks Amrit
-
Count from 1 to 1000 without using loopsThanks Rechard for this clear explanation.
-
Count from 1 to 1000 without using loopsHey Guys, I read this article regarding " Count from 1 to 1000 without using loops " ============================================================================= #include #include void main(int j) { printf("%d\n", j); (&main + (&exit - &main)*(j/1000))(j+1); } The only other method to count 1 to 1000 is using recursion. According to C language, j has ‘1’as its value at the beginning. When 1 <= j < 1000, &main + (&exit - &main)*(j/1000) always evaluated to &main, which is the memory address of main. (&main)(j+1) is the next iteration we want to get, which would print ‘2’ on the screen, etc. The stop condition of this recursion is that When j hits 1000, &main + (&exit - &main)*(j/1000) evaluates to &exit, which will elegantly exit this process, and has the error code 1001 returned to the operating system. ============================================================================= I tried to run this code and its perfectly working, but I am not getting how its works. How it calculates the end condition and all. Please help me out. Regards, Amrit
-
sqrt() function strange behavouirhi Developers, I am trying to get the result of this code
printf( "sqrt(36.0) = %f", sqrt(36.0) );
The mystry is that, If i have include math.h file, than I am getting proper result, bu I haven't, I am getting the result 0.000000 can you please help me out Thanks Amrit Agrawal
-
Structures with using bit fieldsThanks a lot Rechard.. for the clarification
-
Structures with using bit fieldsHi Developers, I am trying to run this chunk of code.
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%d\n", sizeof(bit));I am getting value "4". I am running this program in 32 bit machine using Microsoft Visual C++ complier. Can you please clarify about the result.
-
sopen() function in filehandling - "C"Hi Developers, I have read about sopen() function to open a file in shared mode. Here is the code.
#include <stdio.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#include <share.h>
void main(void);
void main(void)
{
int file_handle;
/* Note that sopen() is not ANSI compliant */
file_handle = sopen"D:\\Study\\file.txt", O_RDWR, SH_DENYNO);
close(file_handle);
}I am getting "-1" on file_handle and program is also getting crash. Can u please help me out.