Simplified code snippet produces assertion string out of bounds error if I do not uncomment the if statement in main.

using namespace std;

size_t find_next_of(string & src, size_t pos) {

    //size_t tmppos;
    //if (pos != string::npos) {
        return src.find_first_of('a', pos);
    //}

    //return string::npos;
}

int main( void ) {

    string str ("a word and another.");

    cout << str << '\n';

    size_t found = str.find_first_of('a');

    str[found]='X';

    cout << "first = " << found << endl;

    while (found != string::npos) {
        found = find_next_of(str, found + 1);
        //if (found != string::npos) {
            str[found]='X';
            cout << "next = " << found << endl;
        //}
    }

    cout << str << '\n';

    getchar();
    return 0;

}

I'm ater suggestions for a way I'd not need to evaluate found in the while loop, while keeping my function call.
Appreciate any input.

line 26: what happens when found+1 > str.length()? There is nothing in the loop to stop the loop at the end of the str. Replace lines 19-31 with this loop

found = str.find_first_of('a');
while( found != string::npos && (found+1) < str.length())
{
   str[found]='X';
   found = find_next_of(str, found + 1);
}

I understand a bit better now, but the problem is that if the src string ends in an 'a' (in this case) the function seems fine, but if not there is still an iteration I could do without, which will output...

"next = 4294967295"

because found is less than npos and str.length() before enter the function but npos after it leaves.

So it appears I would have to evaluate found after the call still.

I'll have a think about changing to a do...while loop.

Thank you for your time AD, I appreciate it.

I believe I may have figured a solution, but unsure if there is a simplified way.

The idea is that, the first time find_next_of is called, in it find_first_of is called twice, once to determine the current position and once to determine the next position.

The current position is updated via pos declared in main and passed by reference, and the next position is returned from find_next_of.

Any further calls and pos is updated by the static nextpos and find_first_of is only called once to determine the next position and returned.

It twisted my brains a little trying to figure the logic, and I'd be happy to hear any thoughts or critisisms to help improve it.

Thanks for reading.
Appeciate any input.

using namespace std;

size_t find_next_of(string & src, size_t & pos) {

    static size_t nextpos = 0;
    pos++;

    if (!nextpos) {
        pos = src.find_first_of('a', pos + 1);
        nextpos = src.find_first_of('a', pos + 1);
        return nextpos;
    }else{
        pos = nextpos;
    }

    nextpos = src.find_first_of('a', pos + 1);

    return nextpos;
}

int main( void ) {

    string str ("a word and an another one and a last one.");

    cout << str << '\n';

    size_t BREAK = 0;

    size_t found = str.find_first_of('a');

    if (found == string::npos) {
        BREAK = 1;
    }

    if (!BREAK) {

        str[found]='X';

        cout << "first = " << found << endl;

        while(BREAK != string::npos)
        {

            BREAK = find_next_of(str, found);
            str[found]='X';
            cout << "next = " << found << endl;
        }
    }

    cout << str << '\n';

    getchar();
    return 0;

}

line 5: how does that variable ever get reset back to 0 so that you can call the function again for a different string?

Thanks AD, I never thought about that, but it will eventually be in a dynamic library so I won't worry to much about that.

Although I'm already thinking about it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.