need help on c++ link list

hello experts, i create a link list of char and input a lot of words in it.
and now i wanna use iterator to modify it,
if the current element is 'c' and next element is 'p', then do sth.

what can i do for getting the next element
i try iterator + 1, but compile error.

Post the code, where the error occurs, and what the error is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <stdlib.h>
#include <ctype.h>

using namespace std;

int main()
{
   list<char> words;
   words.push_back('a');
   words.push_back('b');
   words.push_back('c');
   words.push_back('d');
   words.push_back('e');
   list<char>::iterator itr;
   for(itr = words.begin(); itr != words.end(); itr++){
	//if the previous *itr is equal to 'c' then cout the *itr
   if(*itr-- == 'c')
	cout << *itr << endl;}
}


i dont know how to get the previous and next element,
i tried using ++ and --, but it doesnt work.
if you write this *itr-- it's a matter of priority which is executed first. I'd say it is (*it)--. So you decrease the content. this *(it--) would lead to an endless loop or crash.

To solve that problem you can write it like so:
1
2
3
4
5
6
7
8
9
10
11
   list<char>::iterator prev_itr = words.end();
   for(itr = words.begin(); itr != words.end(); itr++){
	//if the previous *itr is equal to 'c' then cout the *itr

   if(prev_itr != words.end())
   {
	if(*prev_itr == 'c')
	   cout << *itr << endl;
   }
   ++prev_itr;
}
oh, u save me bro!! thank u!!!
Sorry, I was wrong! I must be:

1
2
3
4
5
6
7
8
9
10
11
   list<char>::iterator prev_itr = words.end();
   for(itr = words.begin(); itr != words.end(); itr++){
	//if the previous *itr is equal to 'c' then cout the *itr

   if(prev_itr != words.end())
   {
	if(*prev_itr == 'c')
	   cout << *itr << endl;
   }
   prev_itr = itr; // Note!
}
no worry, i feel much clear now, thx ^^
Topic archived. No new replies allowed.