一级a做爰片免费网站天天看,做安居客网站需要什么浏览器,评价高的企业网站开发,网站名称 注册转载#xff1a;http://blog.csdn.net/shanzhizi/article/details/17189267 C的字符串没有分割函数#xff0c;因此需要自己写方便使用。而受到开发工具的影响#xff0c;有很多用起来比较麻烦啦#xff0c;下面这个比较不错奥。 用STL进行字符串的分割 涉及到string类的… 转载http://blog.csdn.net/shanzhizi/article/details/17189267 C的字符串没有分割函数因此需要自己写方便使用。而受到开发工具的影响有很多用起来比较麻烦啦下面这个比较不错奥。 用STL进行字符串的分割 涉及到string类的两个函数find和substr 1、find函数 原型size_t find ( const string str, size_t pos 0 ) const; 功能查找子字符串第一次出现的位置。 参数说明str为子字符串pos为初始查找位置。 返回值找到的话返回第一次出现的位置否则返回string::npos 2、substr函数 原型string substr ( size_t pos 0, size_t n npos ) const; 功能获得子字符串。 参数说明pos为起始位置默认为0n为结束位置默认为npos 返回值子字符串 实现如下 /*File : split1.cppAuthor : MikeE-Mail : Mike_Zhanglive.com*/
#include iostream
#include string
#include vector//字符串分割函数
std::vectorstd::string split(std::string str,std::string pattern)
{std::string::size_type pos;std::vectorstd::string result;strpattern;//扩展字符串以方便操作int sizestr.size();for(int i0; isize; i){posstr.find(pattern,i);if(possize){std::string sstr.substr(i,pos-i);result.push_back(s);ipospattern.size()-1;}}return result;
}int main()
{std::string str;std::coutPlease input str:std::endl;//std::cinstr;getline(std::cin,str);std::string pattern;std::coutPlease input pattern:std::endl;//std::cinpattern;getline(std::cin,pattern);//用于获取含空格的字符串std::vectorstd::string resultsplit(str,pattern);std::coutThe result:std::endl;for(int i0; iresult.size(); i){std::coutresult[i]std::endl;}std::cin.get();std::cin.get();return 0;
}