C++ 查找子字符串的所有出现和位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4034750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:20:30  来源:igfitidea点击:

Find all a substring's occurrences and locations

c++iostreamstdio

提问by Thomas Havlik

I'm writing a program to parse some data saved as text files. What I am trying to do is find the location of every needle in a haystack. I already can read the file in and determine the number of occurrences, but I am looking to find the index also.

我正在编写一个程序来解析一些保存为文本文件的数据。我想要做的是找到大海捞针中每一根针的位置。我已经可以读入文件并确定出现次数,但我也希望找到索引。

回答by Benjamin Lindley

string str,sub; // str is string to search, sub is the substring to search for

vector<size_t> positions; // holds all the positions that sub occurs within str

size_t pos = str.find(sub, 0);
while(pos != string::npos)
{
    positions.push_back(pos);
    pos = str.find(sub,pos+1);
}

EditI misread your post, you said substring, and I assumed you meant you were searching a string. This will still work if you read the file into a string.

编辑我误读了你的帖子,你说的是子字符串,我认为你的意思是你在搜索一个字符串。如果您将文件读入字符串,这仍然有效。

回答by Nim

I know an answer has been accepted, but this will also work, and will save you having to load in the file to a string..

我知道一个答案已被接受,但这也可以工作,并且可以省去您将文件加载到字符串中的麻烦。

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int main(void)
{
  const char foo[] = "foo";
  const size_t s_len = sizeof(foo) - 1; // ignore ##代码##
  char block[s_len] = {0};

  ifstream f_in(<some file>);

  vector<size_t> f_pos;

  while(f_in.good())
  {
    fill(block, block + s_len, 0); // pedantic I guess..
    size_t cpos = f_in.tellg();
    // Get block by block..
    f_in.read(block, s_len);
    if (equal(block, block + s_len, foo))
    {
      f_pos.push_back(cpos);
    }
    else
    {
      f_in.seekg(cpos + 1); // rewind
    }
  }
}