Val*_*rij 2 c++ string performance std char
好吧,我必须处理大量文本,从头到尾分析线性.我想知道什么是更好的方法:使用char*或std :: string.使用char*时我可以将指针改为字符串中的位置,例如.
//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);
Run Code Online (Sandbox Code Playgroud)
但使用字符串我可能必须使用std :: string :: erase - 但它创建一个副本,或移动字节或东西(我不知道实际的实现)
string str = "text to analyse";
//process
str = str.erase(0,processed_chars);
Run Code Online (Sandbox Code Playgroud)
或者有没有办法改变std :: string的隐藏指针?
编辑:正如Sylvain Defresne在这里要求更多代码:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我用它来进行递归的类xml表示法解析
char str[] ="<father><kid /></fatherr>";
char * pch = str;
tag *root = new tag(&pch);
Run Code Online (Sandbox Code Playgroud)
这个代码很难看,我只是从低级指针算法和东西开始,到目前为止使用视觉组件所以不要太难判断
有std::string,你可能会使用std::string::iterator.你的代码是:
std::string str = "text to analyse";
std::string::iterator iter = str.begin();
// process
iter += processed_chars;
Run Code Online (Sandbox Code Playgroud)