|
I notice that the string is sorted. Is the algorithm permitted to assume this? If so the the following will do the trick:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const char *pstr = "aabbccddeffghijk";
for (unsigned int i=1; pstr[i]!=0; ++i)
{
if (pstr[i]!=pstr[i-1] && pstr[i]!=pstr[i+1])
{
cout << "First unique character: " << pstr[i] << endl;
return 0;
}
}
cout << "No unique characters." << endl;
return 0;
}
Steve
|
|
|
|
|
The only thing I see wrong with your approach is that you are incrementing ptsz prematurely.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
As has been pointed out, the array is sorted. If that's true, and not just a coincidence...
(This assumes a /0 terminated string)
TCHAR findfirstuniquechar_sorted (TCHAR *sz)
{
if (!sz) return 0;
for (int n = 0; sz [n]; n++)
{
if (sz [n] == sz [n+1])
continue;
if ( (n > 0) && (sz [n] == sz [n-1]))
continue;
return sz [n];
}
return 0;
}
If the string is not sorted, the problem becomes harder. As you use TCHAR, I'm assuming unicode, so you can't just have a 26 array for placement...
TCHAR findfirstuniquechar_unsorted (TCHAR *sz)
{
if (!sz)
return 0;
; This line posts badly - it should be CMap less than TCHAR, TCHAR and, int, int and greater-then CharMap; hope that made sense... pre bug reported.
CMap<TCHAR, TCHAR &, int, int &> CharMap;
int nPos;
int nCharPos;
for (nPos = 0; sz [nPos]; nPos++)
{
if (CharMap.Lookup (sz [nPos], nCharPos))
{
CharMap [sz[nPos]] = -1;
}
else
{
CharMap [sz[nPos]] = nPos;
}
}
POSITION pos = CharMap.GetStartPosition ();
nPos = -1;
TCHAR c, cFirst = 0;
while (pos)
{
CharMap.GetNextAssoc (pos, c, nCharPos);
if (nCharPos < 0)
continue;
if ( (nPos == -1) || (nCharPos < nPos))
{
cFirst = c;
nPos = nCharPos;
}
}
return cFirst;
}
OK, I've just spent FAR too much time on that. I hope it wasn't homework!
Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|
|
Environment: Windows XP, Visual Studio 2008, C++
Someone gave me a tip about getting a value from a dialog edit box. It gets me almost there, but not quite. Here is what I did:
Create an edit box in a dialog.
Right click on it and select “Add Variable”
The field named Category has the selections Control and Value.
Open the drop down and pick Value.
The field Variable type has changed to CString, and it has a pull down.
Open the pull down and from the options, select double.
In the variable name field enter: edit_double.
Back in the code, write this:
double temp;
temp = edit_double;
The compiler accepts it, links it, and runs it. I don’t have edit_double declared anywhere so it must be a valid variable, but the value is zero regardless of what I edit into the box.
What have I overlooked? What do I need to do to enter “1.12” or “1.2e4” into the dialog box and get the expected value in temp?
Thanks for your time
|
|
|
|
|
Yeah I have tried that before and it doesn't work. SO what I do is add a control variable. Then to get double value I do this.
char buffer[100];
editControl.GetWindowText(buffer, 100);
double d = atof(buffer);
-Saurabh
|
|
|
|
|
That works, but I am trying to avoid the old atof function.
The test code was not using the OK button. Someone told me to call
BOOL status = UpdateData( True );
and that made the data availble in tha variable. That way I can set the variable to be a double and windows makes the conversion for me.
Thanks for your time
|
|
|
|
|
Ah cool, didn't knew that. Thanks!
-Saurabh
|
|
|
|
|
FYI - another opinion:
Avoid UpdateData()[^]
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
bkelly13 wrote: I am trying to avoid the old atof function.
Would you rather use sprintf() ?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
RE: Would you rather use sprintf()?
That's the wrong direction. I want to convert from string to a binary value. (A double or float right now, but I also use integers.) The conversion function should flag a malformed input in some fashion other than returning a valid value. (Such as returning 0.0 to a double)
Since edit boxes return strings rather than numbers, I am at a loss as to why there seems to be no standard function to do this.
Thanks for your time
|
|
|
|
|
bkelly13 wrote: That's the wrong direction.
How so? It converts from a string to a double and reports any errors if necessary.
bkelly13 wrote: I want to convert from string to a binary value.
The computer sees all numbers as base-2. So this "conversion" makes no sense.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
A google search returned this site:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html[^]
Which says, in part:
Write formatted data to string
Writes into the array pointed by str a C string consisting on a sequence of data formatted as the format argument specifies.
To my knowledge, sprintf converts various inputs to string format, and never the other way around. If you use sprintf to convert from string to double, please show me how.
Regarding my use of binary number, we refer to "1.2" when printed on paper as a number, and in a string as a number, as well as in binary format as a number. When I say a binary value I refer to an int, float, etc rather than formated as digits in a string. That's probably not a good method, but I am open to suggestions.
Thanks for your time
|
|
|
|
|
bkelly13 wrote: If you use sprintf to convert from string to double, please show me how.
I meant to suggest sscanf() . Sorry about that.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
The value is only used/set when UpdateData() is called, this happens when the dialog box is initially shown and when the user clicks OK. Typical usage if the variable is called m_myDouble
double myValue = 23.2;
CMyDialog dlg;
dlg.m_myDouble = myValue;
if (dlg.DoModal() == IDOK)
{
myValue = dlg.m_myDouble;
}
This should have the edit box preloaded with 23.2 and when the user clicks on OK then dlg.m_myDouble should be set to whatever vale was entered
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
You are typing the values in the edit box, but are you sendind the values to the variable?
I know UpdateData should be avoid, but try it. And if it works, try to use another way to get the data.
I saw the other answers after giving mine, so it is useless.
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
I followed the link and read about UpdateData. I don't have sufficient knowledge/experience to understand it all, but my application does not have much concern for preserving the initial values.
I use a value variable and UpdateData makes the values available. The value variable associated with the edit dialog returns the binary numeric value for the number. It still has a flaw. If I type in 1.2r6 instead of 1.2e6, it stops converting at the r and gives me 1.2 with no error. For now, I will continue on with the remainder of my application and get it working.
There are seven numeric fields and I am thinking of having a validate button that will get the binary numeric values, convert back to strings, and put those string representations of the values back on the screen for the user to review.
Maybe I will have to write my own function with a prototype that looks like:
bool = string_to_float( string, &float );
with the bool reporting good or bad conversion. Not real difficult, but the idea that I need to write my own low level conversion function is shocking. It still seems to me that this should be a standard function.
Thanks for your time
|
|
|
|
|
Hello,
I have one question...
1) This code compiles and works fine.
#ifndef __A_H_INCLUDE__
#define __A_H_INCLUDE__
template <class t="">
class A
{
public:
virtual void foo();
};
template <class t=""> void A<t>::foo()
{
}
#endif
2) I second case... very similar
#ifndef __A_H_INCLUDE__
#define __A_H_INCLUDE__
class A
{
public:
virtual void foo();
};
void A::foo()
{
}
#endif
Somtimes compile good but in other situations doesn't compile correctly.
I get this message:
error LNK2005: "public: virtual void __thiscall A::foo(void)" (?foo@A@@UAEXXZ) already defined in main.obj
1>C:\Debug\test2.exe : fatal error LNK1169: one or more multiply defined symbols found
I don't understand why second case is not works in each situation... I don't see any difference in those codes. Who can help me to understand this?
|
|
|
|
|
This will happen if the header file is included in more than one cpp file. The compiler compiles cpp files, and indoing so, complles any code found in the included header. in other words, if both files 'Main.cpp' and 'another.cpp' include the header, A::foo() wil be included in both 'Main.obj' and 'another.obj'. Sofar so good no problems yet. Next we get the linker. it tries to stitch all obj files together into one executable. When it tries to resolve a call to A::foo(), it find that it has two options, it has to choose one. This is what the linker cannot do, and hence it gives the linker error and fails. If you want to keep the function definition in the header file, (although I don't know why you should want to do this) prefix it with the '__inline' directive.
Hope this explains the issue,
Regards,
Bram van Kampen
|
|
|
|
|
Bram van Kampen wrote: prefix it with the '__inline' directive.
While this will work it's not the best choice. From MSDN:
Microsoft Specific
The __inline keyword is equivalent to inline.
But inline is standard whereas __inline is not, so a better choice is to use inline . The only reason to prefer __inline over inline is that __inline works in C code.
Steve
|
|
|
|
|
hmmm,
Never knew that, but, as they say, one lives and learns.
thanks for pointing this out to me though.
regards
Bram van Kampen
|
|
|
|
|
> This will happen if the header file is included in more than one cpp file.
But... Ooo... I understand now... thanks.
> (although I don't know why you should want to do this)
I writting a dll interface and I need put some function in headers files to
give probability to write some customs classes for other people.
I need to change my project now...
> Hope this explains the issue.
Yes, thanks.
Regards.
|
|
|
|
|
Ok, in true this is my problem....
#ifndef __A_H_INCLUDE__
#define __A_H_INCLUDE__
class B;
class A
{
public:
A( B* b )
: bb( b )
{
}
virtual void foo();
};
void A::foo()
{
bb->fuu( this );
}
#endif
#ifndef __B_H_INCLUDE__
#define __B_H_INCLUDE__
#include "A.h"
class B : public A
{
public:
B( B* b )
: A( b )
{
}
virtual void fuu( A* a)
{
// do with a...
}
};
#endif
I need to break this depedency problem... and
my way is not works.
|
|
|
|
|
Member 4608927 wrote: void A::foo()
{
bb->fuu( this );
}
Member 4608927 wrote: virtual void fuu( A* a)
{
// do with a...
}
Remove these from the Header File and put in a separate CPP File.
Regards,
Bram van Kampen
|
|
|
|
|
Bram van Kampen wrote: Remove these from the Header File and put in a separate CPP File.
I can not do that. This is dll header file... I didn't see any dll heders with cpp file.
|
|
|
|
|
Bram van Kampen wrote: Remove these from the Header File and put in a separate CPP File.
I can not do that. This is dll header file... I didn't see any dll headers files with cpp file.
|
|
|
|