首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索LPSTR字符串

搜索LPSTR字符串
EN

Stack Overflow用户
提问于 2010-04-01 03:48:52
回答 4查看 1.3K关注 0票数 0

在获得整个文件的char*之后,我想找到一些单词。我知道如何使用string类函数执行此操作,但我不想再次将数据复制到string变量中。有没有类似的函数可以用于char*字符串,或者我应该仍然使用string类?

EN

回答 4

Stack Overflow用户

发布于 2010-04-01 03:49:56

http://www.cplusplus.com/reference/clibrary/cstring/strstr/

基本字符串的字符串操作函数位于cstring / string.h标头中:http://www.cplusplus.com/reference/clibrary/cstring/

票数 2
EN

Stack Overflow用户

发布于 2010-04-01 03:51:13

您可以使用strstr进行搜索(举个例子)。如果数据足够大,复制时间很长,那么可能值得使用像Boyer-Moore-Horspool这样的搜索。

票数 1
EN

Stack Overflow用户

发布于 2010-04-01 04:37:36

既然您知道如何使用它,为什么不首先将文件加载到一个字符串中呢?

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2556007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档