【发布时间】:2014-03-11 09:10:17
【问题描述】:
有人可以帮助我吗,我对字符串迭代器有疑问。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s("foo $deli: baa :deli$ no matter")
string deli_open ("$deli:");
string deli_close (":deli$");
string::const_iterator it_begin = s.begin();
string::const_iterator it_end = s.begin();
// calculate won't work because I need the Iterposition!
size_t found = s.find(deli_open);
if (found != string::npos)
cout << found << '\n';
// your idea
// return $deli: baa :deli$
for( string::const_iterator i = it_begin; i != it_end; ++i)
{
cout << (*i) ;
}
cout << endl;
这应该是解决方案:
返回iteratorposition it_begin at "$deli: baa :deli$ no matter"
在“foo $deli: baa :deli$”处返回迭代器位置 it_end
【问题讨论】:
-
string::const_iterator it_end = s.begin();?? -
C++ 有很多不错的functions for handling iterators,例如可以用来获取distance between two iterators 的一个。在循环中使用它来与位置进行比较。
标签: c++ string iterator delimiter