Question

Could someone help me, i have problem with the string iterator.

#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;

this should be the solution:

Return iteratorposion it_begin at "$deli: baa :deli$ no matter"

Return iteratorposition it_end at "foo $deli: baa :deli$"

Was it helpful?

Solution

It is not entirely clear what you are asking - I assume you are looking for an iterator that points one past the end of the closing delimiter.
This can be done with the following steps:

  1. after you found the opening delimiter, search from there for the closing delimiter.
  2. after you have found that (the iterator you got from that search will point to the start of it), advance the iterator by N positions, N being the length of the closing delimiter:

    using namespace std;
    int main() {
      string s("foo $deli: baa :deli$ no matter")
      string deli_open ("$deli:");
      string deli_close (":deli$");
    
      size_t startOpenDelim = s.find(deli_open);
      if (startOpenDelim == string::npos)
        cerr << "opening delimiter not found\n";
    
      size_t startCloseDelim = s.find(deli_close, startOpenDelim);
      if (startCloseDelim == string::npos)
        cerr << "closing delimiter not found\n";
    
      size_t endCloseDelim = startCloseDelim + deli_close.length();
    
      string::const_iterator it_begin = s.begin() + startOpenDelim;
      string::const_iterator it_end = s.begin() + endCloseDelim;
    
      cout << string(it_begin, it_end) << endl;
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top