|
George_George wrote: When using mapvariable[somekey] = somevalue to insert into a map variable, how to check whether there is conflicting key value in the map in an elegant way?
You can use map::find() to check whether the key is already present in map. Well, the [] operator will overwrite the existing entry if the key already exists.
OT: Be a little careful while using [] operator with map, especially while reading from map. For instance, if you try to get a key-value which is not in map, it create a new entry and return the default value. For instance,
map<cstring,cstring> TempMap;
CString LookupKey = _T("key");
CString Value = TempMap[LookupKey];
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Thanks Jijo,
Good to know that. Previously I wrongly think we can check whether the expression mapvariable[somekey] returns null or not to check whether the element exists. Now I think I am wrong, and using find is the optimum solution to check existence, and using [] could never tell us whether we insert new pair or overwriting an old pair, all are correct understanding?
regards,
George
|
|
|
|
|
George_George wrote: using find is the optimum solution to check existence, and using [] could never tell us whether we insert new pair or overwriting an old pair, all are correct understanding?
Yes! You are right. Since map is tuned for lookup, map::find() will not be a big performance hit. So it seems optimal.
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Thanks Jijo!
Question answered.
regards,
George
|
|
|
|
|
i want to allow user that he can press enter in edit control
and i want to set text in edit control like this
SetDlgItemText(IDC_EDIT1,' abc \n xyz ');
\n normaly use for new line my edit control in multil line but \n does not add new line
how i can set mulity line text in mulityline edit control?
|
|
|
|
|
ani_ikram wrote: SetDlgItemText(IDC_EDIT1,' abc \n xyz ');
Did you try "\r\n" ???
SetDlgItemText(IDC_EDIT1," abc \r\n xyz ");
|
|
|
|
|
yes it is working but like thie
SetDlgItemText(IDC_EDIT1,_T("abc \r\n sdfj");
|
|
|
|
|
ani_ikram wrote: yes it is working but like thie
Is this supposed to mean something?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
In the program, there are two CArrays.
One CArray is having another CArray in it.
Sometimes unhandled exception happens in RemoveAt() of outer CArray.
The code looks like:
CArray_Outer[i]->CArray_Inner.RemoveAll();<br />
CArray_Outer.RemoveAt(i);
Is it standard way to delete some item in CArray?
Thanks,
Suman
--
"Programming is an art that fights back!"
|
|
|
|
|
What is the exception and what is the code causing it?
Are you doing that code in a loop?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am attempting to access the file names as they get displayed in the listview of the Common File Dialog and change the file name text in certain ways. I was able to do this when the listview was not of style Ownerdata (which seems to be in XP), but in Vista they seem to be of style Ownerdata and I am not able to do a SetItemText anymore. Has anyone done something like this, and can help?
|
|
|
|
|
Hi..
I want to display the all content of a list.
I have this function in Band.cpp:
void Band::show_available_music()
{
list<Music>::iterator it;
for ( it=available_music_list.begin() ; it != available_music_list.end(); it++ )
cout << *it << endl;
}
Now in Main.cpp I want to have an option of all the music in the list.
The only thing that comes to my mind is:
band->show_available_music();
But obviously it doesn't work.. can anyone help with this? Thanks
"Failure is always an option."
|
|
|
|
|
what does not work ?
have you tried debugging your code ?
is the method show_available_music called ?
is the list available_music_list filled with data ?
what is Music and does it make sens to use std::cout to print out its data ?
instead of
cout << *it << endl;
have a method in your Music class (I assume it's a class and it have fields) that prints
out each fields individually.
(something like this
for ( it=available_music_list.begin() ; it != available_music_list.end(); it++ )
(*it).PrintMe();
void Music::PrintMe()
{
cout << artist << endl;
cout << band << endl;
cout << release_year << endl;
}
This signature was proudly tested on animals.
|
|
|
|
|
Music is another class, but Band.cpp as the music list, so show_available_music its implemented here .
My question is how do I call show_available_music from Main.cpp.
"Failure is always an option."
|
|
|
|
|
FrankMookie wrote: My question is how do I call show_available_music from Main.cpp.
Since it is not a static member, you'll need to call it in the context of a Band object.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
But if I call it in the context of a Band object, it will only display the correspondent objects of that particular band... and I want the list of all band's musics...
"Failure is always an option."
|
|
|
|
|
Your Band class implies a single band. Therefore, it only makes sense that it would only know about its own music. Perhaps you need a collection of Band objects.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Yes, you are right. Thats my problem. I have a similar function that shows all the bands in a list.
I'm able to show all the musics from a band... I use: band->show_availavle__music() This gives me the music from that specific band.
But I want an option to show all the musics from all the bands. I mean, all the objects( music) from all the correspondent objects (band).
"Failure is always an option."
|
|
|
|
|
FrankMookie wrote: I have a similar function that shows all the bands in a list.
Then use the returned Band object from that function to call the Band::show_availavle__music() function.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
I'm not sure i understand that.. Can you give me an example?
This is the code I have to display the music from ONE specific Band :
Main.cpp
cout<<"Whats the name of the band you want to search music from? ";
fflush(stdin); getline(cin,name);
band = myManager.select_band_name(name);
if(band!=0)
{
system("cls");
cout << "List of " << name <<"'s music.\n" << endl;
band->show_available_music();
}
Band.cpp
void Band::show_available_music()
{
list<Music>::iterator it;
for ( it=available_music_list.begin() ; it != available_music_list.end(); it++ )
cout << *it << endl;
}
"Failure is always an option."
|
|
|
|
|
But what of the function you mentioned that "shows all the bands in a list?" Much like select_band_name() returns a single Band object, you need a function that returns a Band object based on an index or an iterator. That way you could use it in a loop.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Ok.. I see what you mean. I'll try to do a loop. Thank you so much.
"Failure is always an option."
|
|
|
|
|
Hello again,
I have a lab assignment in my C++ class that asks me to do the following:
-Create an application that prompts the user for two 3-digit numbers, then display the result in the following format:
ex. 456
x 433
---------
1368
13680
182400
---------
197448
The problem I am having is the method I'm trying to use. I am using the carry over technique, and I need a way to split up the 3-digit numbers into single numbers, then multiply/carry/add accordingly
Anything unclear let me know,
Thanks!
|
|
|
|
|
You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.
For example:
513 % 10 = 3 -> Last digit
513 / 10 = 51
51 % 10 = 1 -> Second digit
51 / 10 = 5
5 % 10 = 5 -> First digit
5 / 10 = 0 -> END
Think a bit.
|
|
|
|
|
Stewie Griffin wrote: You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.
Yes, typically. But in this case, the program is prompting the user for the numbers so it is already in string form.
He can take the input string...
char[] n1 = "456";
...and "split the number" by accessing it character by character. Then covert each character to an int:
digit0 = n1[0] - '0';
digit1 = n1[1] - '1';
|
|
|
|