|
Hi,
Below code shows how to get the last occurrence of a string. But in this case string::rfind reads the string from the starting and gives the last occurrence.
string str ("The sixth sick sheik's sixth sheep's sick.");
string key ("sixth");
size_t found;
found=str.rfind(key);
if (found!=string::npos)
str.replace (found,key.length(),"seventh");
cout << str << endl;
(EXAMPLE:1)
But I need a code to find the last occurrence of a string reading it from the last rather than reading it from starting. Using only string operations.
This is because I have a requirement where I need to read a string from the end, improve the performance as below shown scenario
#STAR
Data…
Data..
…………
……….
Data……
#END
We will receive a huge string (about 10mb) of data from server as show above (in the above format). To confirm that we have received the whole set of data, just check for #START and #END tag. As of now I am reading the #END using string::rfind(), which takes some time to give the result. So is there any to find a word in a string reading from end. Reading from the end will reduce the time for finding the #EDN tag.
So please proved a solution to read a string from the end.
-Nandu
|
|
|
|
|
Hi,
how about searching only the last part of the string, by getting its length, then adding say len-20
to the string pointer, then do a "find last occurence"? And as a safety net (if one is needed) if
no match, try the normal approach.
However, if the length has to be obtained by scanning for a NULL (as strlen would do), not much
may be gained!
|
|
|
|
|
Thanks for your reply,
But this approach will not help, boz some tags may also present at the end as show below.
#START
data
....
....
#END
#DATE
#TIME
That's why I need an approach to find a word in a string from end.
-Nandu
|
|
|
|
|
Hi,
assuming you get the length in an inexpensive way, search the last N1, on failure the last N2,
on failure the last N3 characters, where it is up to you to decide what appropriate values for
N1, N2, N3 are. You might go for 10%, 20%, 30% or something similar. The basic idea is to
have more than 50% chance N1 will find a match.
if the length is as expensive as strlen, IMO there is nothing you can do once all the data is
in a single string.
Whatever the case be, I would tend to avoid having such big strings to begin with.
There must be better ways to store the data. If, before being turned into a big string, it
comes in in buckets, why not search the last bucket, or possibly the last two?
|
|
|
|
|
above suggestion will not help.
Can help me in this regard. Is there any other solution.?
|
|
|
|
|
Is there any external data available that indicates the length of the string.
I would consider it sort of nasty to send anyone a \0 terminated string of upto 10 MByte.( A bit Error prone too) If it comes to that type of size, an extra 4 bytes to indicate the length of the string at the start would help, (i.e. Make it a Pascal String) and will hardly hit performance.)
But, How does your Receiving Software know to allocate buffer of sufficient size to hold the data in the first place. Drill into that with your debugger, and you might find your answer.
Regards,
Bram van Kampen
|
|
|
|
|
How do I do that?
Man, I feel like such a noob.
For example, strlen, strcmp, etc., all want a const char* or const wchar_t*. How do I cast the CString to that type?
[edit] Never mind, I changed to multi-char instead of unicode and simply dereferencing the CString object converts it correctly. Sigh [/edit]
Marc
modified on Saturday, July 12, 2008 12:14 PM
|
|
|
|
|
Hi Marc,
Remember, you don't have to abandon Unicode.
CString has methods to do most of the things those old char functions do,
but CString is generic.
If you truly need the char character type in a Unicode build, you can use
CStringA.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
http://www.cplusplus.com/reference/string/string/c_str.html
Check the above link, hope it will help you.
-Nandu
|
|
|
|
|
Mark Salsbery wrote: If you truly need the char character type in a Unicode build, you can use
CStringA.
Ah, thanks!
Marc
|
|
|
|
|
My C++ is rusty, about the last time I did C++ stuff was with VS7. So here I am with VS9, doing pure MFC/C++ (none of the CLR crap).
So, I've got a "lib" project and a console "test" project. The linker can only find functions that are inline'd in the header file. It can't find functions defined in the lib.cpp class!
What happened? And actually, why can't I find the lib.dll file? It creates lib.dll.embed.manifest, but no foo.dll!
In fact, I think I just answered my first question: I'm not referencing lib.dll in my test project, so it's only resolving inline functions. Why am I not getting a lib.dll lib? How do I get one?
Do I add a path to the Linker\General "Additional Library Directories" or do I add it under Common Properties "Framework and References"?
This used to be so much easier.
[edit] Ummm. Why is the output going to a project in my solution that is excluded from the build and not even referencing the lib project? Why isn't it going to the folder specific to the project itself? WTF has MS done!!! [/edit]
[edit2]The output file property in the Librarian was using (OutDir) rather than (IntDir). WTF? Why would it do that? [/edit2]
Marc
modified on Saturday, July 12, 2008 11:45 AM
|
|
|
|
|
Marc Clifton wrote: This used to be so much easier.
And this was when?
|
|
|
|
|
bob16972 wrote: And this was when?
Like, 7 years ago, the last time I touched C++ with VS6.
Marc
|
|
|
|
|
Well I still use VC5.0, No problems with that there either. Works Nifty, Produces my code quite proficciently, The features VC5.00 misses are quite easily written by myself, I personally am not writing for the Chineese or Japaneese market, so why should I be bothered with the Unicode efford.
My advise is: 'Downgrade'
Regards
Bram van Kampen
|
|
|
|
|
It seems it has been very long time since you did C++. There are two types of libraries in VC++ - Static lib and dynamic lib. "lib" project buids a static library and only produces a .lib file whereas "dll" project build a dynamic library and produces a .dll and .lib.
Marc Clifton wrote: The output file property in the Librarian was using (OutDir) rather than (IntDir).
Because output file should go into *output* (OutDir) directory and not intermediate (IntDir) folder.
Marc Clifton wrote: Do I add a path to the Linker\General "Additional Library Directories" or do I add it under Common Properties "Framework and References"?
You should add the path in additional libraries directories and you should specify the import library at Linker -> Input -> Additional Dependencies.
-Saurabh
|
|
|
|
|
Saurabh.Garg wrote: Because output file should go into *output* (OutDir) directory and not intermediate (IntDir) folder.
I would expect each project to output to it's own Debug folder. At least, that's what it does in C#. It seems with a C++ solution, it sets the output to the first project created. I saw no place where I could change that setting once it was made.
Saurabh.Garg wrote: Static lib and dynamic lib. "lib" project buids a static library and only produces a .lib file whereas "dll" project build a dynamic library and produces a .dll and .lib.
Yeah, I finally figured out I wanted a static lib.
Saurabh.Garg wrote: You should add the path in additional libraries directories and you should specify the import library at Linker -> Input -> Additional Dependencies.
Adding the project as a reference also worked, though I'll definitely look at your suggestion.
Thanks so much for the feedback!
Marc
|
|
|
|
|
I don't do much C# so I am not sure how it sets the folder for a project but in VC++ it is like this.
"Configuration Properties" -> "Output Directory" : Sets the output directory for the entire project. By default it is set to $(SolutionDir)$(ConfigurationName) so the Release or Debug folder is created in the folder where solution file is located.
"Configuration Properties" -> "Intermediate Directory" : Sets the intermediate directory for the entire project. By default it is set to $(ConfigurationName) so the Release or Debug folder is created in the folder where project file is located.
Now if I have a solution with following organization:
FooBar
FooBar.sln
Project1
Project1.vcproject
Project2
Project2.vcproject
Project3
Project3.vcproject
I configure each project like this:
OutDir = $(SolutionDir)\Bin
IntDir = $(SolutionDir)\Temp\$(ProjectName)\$(ConfigurationName)
Note: If you want both debug and release binaries the add D to the debug configuration, otherwise this setting will only contain binaries from the last build.
Having said this, in VC++, you can configure path of each file produced separately. So if for some reason you wany PDB files in some other folder then go to "Configuration Properties" -> C\C++ -> Output files and set "Program Datbase file name" to any thing you want.
Also check out macros when setting a path, there are lots of useful folders defined as macros.
Setting project as reference works as well but well thats not how native developers do it
-Saurabh
|
|
|
|
|
Saurabh.Garg wrote: "Configuration Properties" -> "Output Directory"
I think sometimes I purposely don't see the obvious. Thanks again!
Marc
|
|
|
|
|
By the way sooner or later in the debug build you are going to encounter "LNK1000: Internal error during IncrBuildImage". To fix this install KB948127[^].
-Saurabh
|
|
|
|
|
Saurabh.Garg wrote: To fix this install KB948127[^].
Will do! Thanks so much!
Marc
|
|
|
|
|
Hello,
I use EVC++4 for ARMV4I device. I have come across a situation where I find difficult to figure out few things.
I store data in a file like:
1+Oper 1+1+Dept 1+12+2+1.0+2+2.0+3.5+1+3.5
-------- ----------- - --------- ---------
Obj 1 Obj 2 S Obj 3 Obj 3 S-> # of objects stored of Obj 3 type
Now I am reading the file & I want data in a form where value of 1 from Obj 2 is against all the values of Obj 3. So,
1 Obj 3
Obj 3
For this I believe I should have a CArray in a CMap. The CArray can contain all Obj3 & value remains as the key of the CMap.
I am not able to make out how do I declare the CArray & the CMap. I am getting errors of CArray: no operator = ....
Can anyone help me create CArray & CMap respectively & also show how to add contents in the array & map rexpectively.
While reading file, I might need to access the saved CArray & add more if I come across the same number again.
I am using EVC so please provide only MFC or general C/C++ solutions - No std, ATL, etc. Evc doesn't support CArrayEx & CMapEx.
Help is highly appreciated.
Thanks
Terry
|
|
|
|
|
Well, part of the solution to your problem would be to serialize your CArray. To understand how to do that please have a look into Xavier John's Address Book[^]. If that isn't help you a lot please to MSDN page: Serialization in MFC[^].
Best regards,
Mihai Moga
|
|
|
|
|
Well Mihai, I also like Serilization, but EVC+ with +WinCE is giving a tough time with serialization. All proper code yet write & Read serialized objects is not supportive. So I have to take the long route. WinCE also doesn't support AfxAPI SerializeElements for CMap.
If ypu can give other solution, world be really helpful.
Thanks
Terry
|
|
|
|
|
Hi all,
I have an SDI type project i m also want to create another SDI type project inside it.
I want to call second SDI type project in First SDI type project.
How can both are working at same time and both are call from one project to another.
Please tell me how can i do this.
Thanks in advance.
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
|
|
|
|
|
Have you taken a look into shellexecute?
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.
|
|
|
|