C++分离字符串

C++分离字符串

#include <iostream>
#include <string>
#include <vector>
using namespace std;


//字符串分割函数
vector<string> split(string str,string pattern){
	vector<string> result;
	str+=pattern;
	int pos;
	for(int i=0;i<str.size();i++){
		pos=str.find(pattern,i);
		string s;
		s=str.substr(i,pos-i);
		result.push_back(s);
		i=pos-1+pattern.size();
	}
	return result;
}

int main()

{
  string str;
  cout<<"Please input str:"<<endl;
  //std::cin>>str;
  getline(cin,str);
  string pattern;
  cout<<"Please input pattern:"<<endl;
  getline(cin,pattern);
  vector<string> result=split(str,pattern);
  cout<<"The result:"<<endl;
  for(int i=0; i<result.size(); i++)
  {
    cout<<result[i]<<endl;
  }
  cin.get();
  cin.get();
 
  return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页