char *acharstring = "PARENT-25";
if(strncmp(acharstring, "PARENT", 6) == 0)
{
}
STL's version of strncmp
How would you do a strncmp with the stl''s string? I''ve tried findfirstof, and find, and neither really seemed to work just right.
For example, how would I do the following:
You could try:
std::string a = new std::string("PARENT-25";if(a.substr(0, 6) == "PARENT" { // do something}
if(astring.substr(0, 6)=="PARENT"))
...
EDIT: Someone was faster...
[edited by - bakery2k1 on November 28, 2003 12:11:40 PM]
...
EDIT: Someone was faster...
[edited by - bakery2k1 on November 28, 2003 12:11:40 PM]
Maybe this will eliminate the weird smilies:
std::string a = new std::string("PARENT-25" ) ;if(a.substr(0, 6) == "PARENT" ) { // do something}
if you want to use stl perhaps this is what you''re after:
now upon success ''i'' will return an iterator that will be != word.end(). Failure will return word.end() assigned to ''i''.
That is just the basic schematic of how one can use it.
|PicRepository|PicIndex|
IM: CScoder@aol.com/ahmed7500@hotmail.com
The Gatekeeper of quotes passé:
"How Appropriate. You fight like a cow" --Sneftel
#include <algorithm>int main(){std::string word = "parent-25";std::string subword = "parent";std::string::const_iterator i = search(word.begin(),word.end(),subword.begin(),subword.end());...return 0;}
now upon success ''i'' will return an iterator that will be != word.end(). Failure will return word.end() assigned to ''i''.
That is just the basic schematic of how one can use it.
|PicRepository|PicIndex|
IM: CScoder@aol.com/ahmed7500@hotmail.com
The Gatekeeper of quotes passé:
"How Appropriate. You fight like a cow" --Sneftel
I like the above solution the best, but I thought I''d point out that std::string has a cstr() method.
You can use it to have the std::string return a char * for you, which of course can be used to in strncmp.
Cheers
Chris
You can use it to have the std::string return a char * for you, which of course can be used to in strncmp.
Cheers
Chris
What the fuck?
The direct analogue is basic_string::compare(). What the hell are you creating substrings for? Deliberate inefficiency?
The direct analogue is basic_string::compare(). What the hell are you creating substrings for? Deliberate inefficiency?
quote:Original post by Anonymous Poster
Maybe this will eliminate the weird smilies:std::string a = new std::string("PARENT-25" ) ;if(a.substr(0, 6) == "PARENT" ) { // do something}
You must be a Java programmer :-) You need to declare ''a'' as a pointer if you are going to ''new'' it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement