|
Mark Salsbery wrote: what happened at what fair?
It's an old thing I got from my grandfather. If my memory is correct they used to have a fair once a year so the saying was a smart ass remark indicating that you rarely made mistakes.
|
|
|
|
|
Ohhh... that makes sense. I thought there might be a good story too
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello everyone,
In Bjarne's book, it is mentioned that sort of STL may throw exception, like sorting elements in a vector.
In what situation will sort throw exception? I can not find a case.
thanks in advance,
George
|
|
|
|
|
George_George wrote: In what situation will sort throw exception?
An invalid iterator for example?! (I am just guessing.)
I do not have VC++ (and the source code of STL) at home now, but according to the definition of sort :
std::sort (SGI)[^]
Preconditions
(first, last) is a valid range. And the return type of sort is void .
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last); So how does sort express errors?! Throw something!
A valid range refers to a valid beginning position and a valid ending position. What about this?
std::vector<T> v1;
std::vector<S> v2;
std::vector<T>::iterator p1 = ... ;
std::vector<S>::iterator p2 = ... ;
std::sort(p1, p2);
Maxwell Chen
modified on Friday, January 04, 2008 1:27:06 PM
|
|
|
|
|
Hi Maxwell,
I think it is a bug of application, not an exception from the STL library implementation sort itself. In your sample, it is developer's fault to pass in two iterations belongs to two different containers.
I think Bjarne's means some *expected* errors from STL library sort implementation itself, not such kind of developer fault -- if we treat such fault as exception, every function of STL throws exception.
Do you have any ideas about some exception which is thrown internally, like bad_alloc thrown from new function?
If you do not agree with me, please feel free to correct me.
have a good weekend,
George
|
|
|
|
|
George_George wrote: In what situation will sort throw exception? I can not find a case
For instance, if a comparasion operation for the type being sorted throws; the exception would not be "swallowed" by sort .
|
|
|
|
|
Hi Nemanja,
How could comparasion operation throw an exception? Could you provide more information please?
regards,
George
|
|
|
|
|
Finding it Funny!. What's the exact statement? can you put it here? I wont be crashing , it's not usual. And from my guess, I can say , if it's a huge list that you are sorting and the same list gets manipulated in another thread, like , removing some elements , adding some. etc. It would crash.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
Hi VA_,
Sure, here is the source of the statements.
http://www.research.att.com/~bs/3rd_safe.pdf[^]
You can find it from the 1st sample. It is appreciated if you could give some comments why do you think sort will throw any exceptions?
regards,
George
|
|
|
|
|
Ahh boy! Listen the first question that should have arised and get solved immediately is that "How should a sort would fail?".. the next is that "The sort is designed to sort things and not to fail,so it WILL NOT FAIL".
If you look at the document, it says "USER DEFINDED SORTS", ok I make an example of what they are talking about. Just try to execute the below sample in your machine twice. The next time you run it, remove the i=i/0 line from the code.
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
class test
{
private:
public:
int nValue;
bool operator <(test obj)
{
int i= i/0;
if(this->nValue<obj.nValue)
{
return(true);
}
else
{
return(false);
}
}
};
int main(int argc, char* argv[])
{
printf("Hello World!\n");
std::vector<test> vec_testObj;
test testObjs[4];
testObjs[0].nValue =5;
testObjs[1].nValue =4;
testObjs[2].nValue =3;
testObjs[3].nValue =2;
vec_testObj.push_back(testObjs[0]);
vec_testObj.push_back(testObjs[1]);
vec_testObj.push_back(testObjs[2]);
vec_testObj.push_back(testObjs[3]);
std::sort(vec_testObj.begin(),vec_testObj.end());
vector<test>::iterator itr_vecTest = vec_testObj.begin();
while(vec_testObj.end()!=itr_vecTest)
{
cout<<"\n"<<itr_vecTest->nValue;
itr_vecTest++;
}
return 0;
}
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
Thanks VA_,
I think your idea is user defined < operator may fail (in your sample, it is a divide by 0 exception) with exception, which will make STL sort algorithm throw exception (I think STL sort does not catch any exception), right?
regards,
George
|
|
|
|
|
That's the point.
-Vunic
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
Thanks for your confirmation, Vunic!
Have a good weekend,
George
|
|
|
|
|
Does anyone know a safe way for a non-unicode MFC App to link with and call into a unicode enabled dll ?
|
|
|
|
|
No problem there at all. Just because something isn't built with strings set to Unicode doesn't mean it can't use wide characters. You just need to make sure that the calling side allocates any buffers used by the unicode dll in terms of wchar_t rather than char and that it converts any returned string values with WideCharacterToMultiByte and related functions or MFC/ATL macros before it uses them. Check out things like the USES_CONVERISON macro for how to do this.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Yes, I understand that, but the dll crashes when I call into it. Basically the dll is required to display a dialog which gets a unicode string from the user (among other things), I make a few calls which set up defaults for the dialog, mostly integer or real variables and one string (which is char[], and is converted by the dll), and then call a function which displays the dialog. The dll crashes even when I am just passing ints and I am unable to trace into it. No I havenot forgotten to use
AFX_MANAGE_STATE(AfxGetStaticModuleState());
I know that you believe that you understand what you think I said, but I am not sure you realize that what you heard is not what I meant. Anon.
|
|
|
|
|
Then how do you know the problem has anything to do with Unicode?
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Good point.
I know that you believe that you understand what you think I said, but I am not sure you realize that what you heard is not what I meant. Anon.
|
|
|
|
|
There seems to have been a change in behaviour between VC6 and VS2005 builds when calling SetSel / EM_EXSETSEL in a rich edit control. I have code which emboldens certain lines of text, which used to work in VC6, but since moving to VS2005 has come up with the wrong results. The code is something like:
CString sText;
pEdit->GetWindowText(sText);
CString sSection = "SectionHeading";
int nPos = sText.Find(sSection);
int nPosEnd = nPos + sSection.GetLength();
pEdit->SetSel(nPos, nPosEnd);
If the heading is at the start of the text, then it's ok, else the selection is offset by the number of lines, which makes we think that the old code counted line breaks as two characters, but the new code counts a line break as one character. In the sText, the line breaks are definately two characters in both VC6 and VS8 builds.
Any ideas anyone?
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
While working with rich edit I found out that setting the selection back to begining is often necessary.
Since I work with VC6 I cannot comment on the "rest of the story".
Cheers
Vaclav
|
|
|
|
|
Thanks for your reply. It's not that - I've found it:
VC6 used RichEdit 1.0 controls by default, whereas VS8 uses RichEdit 2.0 controls by default, so that is where the difference lies.
Version 1.0 seems to count line breaks as 2 chars, whereas 2.0 counts them as 1 char. I was getting my positions from my CString, which contains CR+LFs so counts line breaks as 2 chars.
I have changed my code to use the Find facility (EM_FINDTEXT/EM_FINDTEXTEX) in the RichEdit control, and all is now OK. (Not really sure why I didn't just use that in the first place!)
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Paul,
After I posted my reply I realized that I was responding incorrectly.
Anyway, I observed same "problem" - I was searching for string and not counting the space after the string. Consequently my display was off few characters. ( I was changing the text color)
By accident - and I am sure you are aware of that - if you search rich text control for string using substring search it will give you the first match and it may not be what you are looking for.
Cheers
Vaclav
|
|
|
|
|
Have you tried using EM_FINDTEXT to find the text position instead
of copying the text to a CString?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have now! See my other reply.
Thanks.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Hi there, i have written a dialog based aplication in vc++, my dialog contains some edit box and checkbox controlls that a user can write strings into or check the box's.
I am looking for a way to save the value of whether a box was checked or not so that if the program shutdown or the computer rebooted the program would remember settings when it was next restarted.
any suggestions would be much apriciated.
Chris
|
|
|
|