S
Steve
Hi Guys,
I have a string which contains data elements separated by spaces. I also
have a function which returns the number of characters from the beginning of
the string for a given number of spaces. I am using a loop with strchr for
the number of spaces, and I was just wondering if there was a more efficient
(ie faster) way of achieving this. Any ideas please? Current function code
below:
int GetNewPosition(const std::string& DataStr, const int Spaces)
{
int i = 0; // space counter
char* b = const_cast<char*>(DataStr.c_str()); // beginning of string
do {
b = strchr(b, ' '); // find next space
++i; ++b; // increment space counter and pointer
} while(i<Spaces && b != NULL);
if(b) return b - DataStr.c_str();
else return -1;
}
Any help is appreciated,
Thanks for reading,
Steve.
I have a string which contains data elements separated by spaces. I also
have a function which returns the number of characters from the beginning of
the string for a given number of spaces. I am using a loop with strchr for
the number of spaces, and I was just wondering if there was a more efficient
(ie faster) way of achieving this. Any ideas please? Current function code
below:
int GetNewPosition(const std::string& DataStr, const int Spaces)
{
int i = 0; // space counter
char* b = const_cast<char*>(DataStr.c_str()); // beginning of string
do {
b = strchr(b, ' '); // find next space
++i; ++b; // increment space counter and pointer
} while(i<Spaces && b != NULL);
if(b) return b - DataStr.c_str();
else return -1;
}
Any help is appreciated,
Thanks for reading,
Steve.