在获得整个文件的char*之后,我想找到一些单词。我知道如何使用string类函数执行此操作,但我不想再次将数据复制到string变量中。有没有类似的函数可以用于char*字符串,或者我应该仍然使用string类?
发布于 2010-04-01 03:49:56
http://www.cplusplus.com/reference/clibrary/cstring/strstr/
基本字符串的字符串操作函数位于cstring / string.h标头中:http://www.cplusplus.com/reference/clibrary/cstring/
发布于 2010-04-01 03:51:13
您可以使用strstr进行搜索(举个例子)。如果数据足够大,复制时间很长,那么可能值得使用像Boyer-Moore-Horspool这样的搜索。
发布于 2010-04-01 04:37:36
既然您知道如何使用它,为什么不首先将文件加载到一个字符串中呢?
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <algorithm>
int main( int argc, char* argv[] ){
std::ifstream ifsFile( <file_name> );
ifsFile.unsetf( std::ios_base::skipws ); // make sure we're not skipping whitespaces
std::istream_iterator< char > begin( ifsFile ), end;
std::string strFile( begin, end ); // load the file into the string
// now print the file/search for words/whatever
std::copy( strFile.begin(), strFile.end(), std::ostream_iterator< char >( std::cout, "" ) );
return 0;
}https://stackoverflow.com/questions/2556007
复制相似问题