From: Carl R. Davies on
Apologies for cross posting between m.p.vc.lang and m.p.vc.stl not sure
which newsgroup is appropriate.

I'm using VS 2005 and have the following code:

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
std::string url = "http:\\\\test.com\\index.htm";
std::string oldVal = "\\", newVal = "/";
std::string newUrl;

std::replace_copy( url.begin(),
url.end(),
newUrl,
oldVal,
newVal );

std::cout << newUrl << std::endl;

return EXIT_SUCCESS;
}


Which produces the following errors:

------ Build started: Project: Scratch, Configuration: Debug Win32 ------
Compiling...
Main.cpp
c:\program files\microsoft visual studio 8\vc\include\xutility(572) :
error C2039: 'iterator_category' : is not a member of
'std::basic_string<_Elem,_Traits,_Ax>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\program files\microsoft visual studio
8\vc\include\algorithm(896) : see reference to class template
instantiation 'std::iterator_traits<_Iter>' being compiled
with
[
_Iter=std::string
]
c:\documents and settings\carl\my documents\visual studio
2005\projects\scratch\scratch\main.cpp(11) : see reference to function
template instantiation '_OutIt
std::replace_copy<std::_String_iterator<_Elem,_Traits,_Alloc>,std::string,std::string>(_InIt,_InIt,_OutIt,const
_Ty &,const _Ty &)' being compiled
with
[
_OutIt=std::string,
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>,

_InIt=std::_String_iterator<char,std::char_traits<char>,std::allocator<char>>,
_Ty=std::string
]
c:\program files\microsoft visual studio 8\vc\include\xutility(572) :
error C2146: syntax error : missing ';' before identifier
'iterator_category'
c:\program files\microsoft visual studio 8\vc\include\xutility(572) :
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
c:\program files\microsoft visual studio 8\vc\include\xutility(572) :
error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a
member of a base class of 'std::iterator_traits<_Iter>'
with
[
_Iter=std::string
]
c:\program files\microsoft visual studio
8\vc\include\xutility(572) : see declaration of
'std::iterator_traits<_Iter>::iterator_category'
with
[
_Iter=std::string
]
c:\program files\microsoft visual studio 8\vc\include\xutility(572) :
error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal
syntax for using-declaration; expected qualified-name
with
[
_Iter=std::string
]
Build log was saved at "file://c:\Documents and Settings\Carl\My
Documents\Visual Studio 2005\Projects\Scratch\Scratch\Debug\BuildLog.htm"
Scratch - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thank you for your time.
Carl.


From: Carl R. Davies on
Carl R. Davies wrote:
> Apologies for cross posting between m.p.vc.lang and m.p.vc.stl not sure
> which newsgroup is appropriate.
>
> I'm using VS 2005 and have the following code:
>
> #include <string>
> #include <algorithm>
> #include <iostream>
>
> int main()
> {
> std::string url = "http:\\\\test.com\\index.htm";
> std::string oldVal = "\\", newVal = "/";
> std::string newUrl;
>
> std::replace_copy( url.begin(),
> url.end(),
> newUrl,
> oldVal,
> newVal );
>
> std::cout << newUrl << std::endl;
>
> return EXIT_SUCCESS;
> }
>
>
> Which produces the following errors:
>
[snip]
> Thank you for your time.
> Carl.

Think I've fixed it:

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
std::string url = "http:\\\\test.com\\index.htm";
std::string newUrl;

std::replace_copy( url.begin(),
url.end(),
back_inserter(newUrl),
'\\',
'/' );

std::cout << newUrl << std::endl;

return EXIT_SUCCESS;
}

Any other beginner bugs :) ?
From: Tom Widmer [VC++ MVP] on
Carl R. Davies wrote:
> Think I've fixed it:
>
> #include <string>
> #include <algorithm>
> #include <iostream>
>
> int main()
> {
> std::string url = "http:\\\\test.com\\index.htm";
> std::string newUrl;
>
> std::replace_copy( url.begin(),
> url.end(),
> back_inserter(newUrl),
> '\\',
> '/' );
>
> std::cout << newUrl << std::endl;
>
> return EXIT_SUCCESS;
> }
>
> Any other beginner bugs :) ?

It looks ok now. You could do:

newUrl.reserve(url.size());

as a potential optimization, prior to the replace_copy.

Finally, there's rarely much need to use std::endl. Instead, unless you
explicitly need to flush (e.g. flushing won't be done automatically),
just use '\n' instead. Programs that use std::endl when writing to files
tend to be slow, due to all the unnecessary flushing to disk. Note that
flushing automatically happens when a stream is closed or destroyed.

Tom