// 子字符串个数.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<iostream>#include <string>#include<fstream>#include <vector>#define MAX 50using namespace std;int main(){ifstream infile("E://text.txt");ofstream outfile("E://result.txt");if (!infile) { cout << "text.txt open failed" << endl; return 1; }if (!outfile) { cout << "result.txt open failed" << endl; return 1; }string str;string pattern;getline(infile,str);getline(infile, pattern);int pos = 0;unsigned int number= 0;vector<int>vec;while (pos!= string::npos){pos = str.find(pattern, pos);vec.push_back(pos);if (pos != string::npos){number++;pos += (pattern.size() + 1);}}outfile << "字符串中含有的子字符串的个数为:" << number<<endl;outfile << "字符串中每个子字符串的起始位置为:";for (int j = 0; j < number; j++){outfile << vec[j] << " ";} return 0;}
/*txt文本内容:abchuhabchabcijoab
abc
*/