[Please detab your code before you post it.]

"sejal patel" <[EMAIL PROTECTED]> wrote:
> I want to replace characters in this prog.
> but the found value prints :4294967295

That's the value of string::npos on your implementation.

> i dont understand why this happen?

That's the value returned when 'find' can't find the string.
If you're not sure why it can't find the string, then you
should try printing the 'search' string.

Skipping the first two tokens, your program looks at 'cc'
and then adds angle brackets to form the search string '<cc>'.
Why do expect _that_ to be found in the original string?

> #include<iostream>
> #include <vector>
> #include <string>
> #include <sstream>
> 
> using namespace std;
> 
> int main()
> {
>     string str("aa bb cc dd <ee> ff <gg> hh <ii>
> jj"),search,replace;
>     cout<<str;
>     string buf; 
>     stringstream ss(str); // Insert the string into a stream
> 
>     vector<string> tokens; 
>     vector<string>::iterator iter;
>     while (ss >> buf)
>         tokens.push_back(buf);
> 
>   iter = tokens.begin()+2;
>   while(  iter != tokens.end())
>   {
>         cout << endl<<*iter ;
>         search="<" + *iter + ">";

Hear you add angle brackets to the string.

>         iter++;

Better is the prefix form...

  ++iter;

>         replace=*iter;
>         size_t found = str.find( search,0);

  string::size_type found = str.find(search);

>         cout<<"found:"<<found<<endl;
>         while ( found != string::npos )
>         {
>                str.replace( found, search.size(), replace);
>                cout<<"str:"<<str<<"+++++";
>                found = str.find( search, found+replace.size()+1 );
>         }
>         *iter++;

There's no point dereferencing the iterator here...

  ++iter;

> 
>   }
>  cout<<"Now string is:"<<str;
> }

-- 
Peter

Reply via email to