well i have to process a large chunk of text, analysing it linear from begin to end. And i wonder what is the better approach for this: using char* or std::string.
while using char* i can alter the pointer to a position further in the string eg.
//EDIT later: mallocing some space for text
char str[] = "text to analyse";
char * orig = str;
//process
str += processed_chars; //quite fast
//process again
// later: free(orig);
but using string i might have to use std::string::erase – but it create a copy, or move bytes or something (i don’t know the actual implementation)
string str = "text to analyse";
//process
str = str.erase(0,processed_chars);
or is there a way to alter the std::string’s hidden pointer?
EDIT: as Sylvain Defresne requested here more code:
class tag {
public:
tag(char ** pch) {
*pch = strstr(*pch,"<");
if(pch == NULL) return;
char *orig = *pch+1;
*pch = strstr(*pch,">");
if(pch == NULL) return;
*pch+=sizeof(char); //moving behind the >
//process inner tag data
if(*(*pch-2)!='/'){// not selfclose
while (!(**pch == '<' && *(*pch+1) == '/')){ //sarch for closing tag
tag* kid = new tag(pch);
sublings.push_back(*kid);
}
*pch = strstr(*pch,">");
if(pch == NULL) return;
*pch+=sizeof(char); //moving behind the >
//add check if the clothing tag is matching
}
}
}
i use it for recursive xml-like notation parsing
char str[] ="<father><kid /></fatherr>";
char * pch = str;
tag *root = new tag(&pch);
this code is ugly as hell, i am just starting with low-level pointer arithmetic and stuff, used visual components till now so don’t judge too hard
With
std::string, you would probably usestd::string::iterator. Your code would be: