c – 字符串第n次出现的索引

前端之家收集整理的这篇文章主要介绍了c – 字符串第n次出现的索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在一行中找到给定字符串第n次出现的索引?我需要这个从该索引中获取子字符串.这可能通过c中的任何功能吗?

解决方法

Boost: http://www.boost.org/doc/libs/1_54_0/doc/html/boost/algorithm/find_nth.html中有一个find_nth模板函数
  1. #include <iostream>
  2. #include <boost/algorithm/string/find.hpp>
  3.  
  4. using namespace std;
  5. using namespace boost;
  6.  
  7. int main() {
  8.  
  9. string a = "The rain in Spain falls mainly on the plain";
  10.  
  11. iterator_range<string::iterator> r = find_nth(a,"ain",2);
  12. cout << distance(a.begin(),r.begin()) << endl;
  13.  
  14. return 0;
  15. }

猜你在找的C&C++相关文章