Why is \ added both times?

W

William Payne

Hello, consider the following code:

#include <iostream>
#include <string>

void foo(std::string& s);

int main()
{
const char* directory = "c:\\blah";
std::string s = directory;

foo(s);
foo(s);

std::cout << s << std::endl;

return 0;
}

void foo(std::string& s)
{
if(*(s.end()) != '\\')
{
s += '\\';
}
}

When I run that, the program outputs:
c:\blah\\

I thought (and need it to be) would be:
c:\blah\

What am I doing wrong?

/ William Payne
 
R

Rob Williscroft

William Payne wrote in
Hello, consider the following code:

#include <iostream>
#include <string>

void foo(std::string& s);

int main()
{
const char* directory = "c:\\blah";
std::string s = directory;

foo(s);
foo(s);

std::cout << s << std::endl;

return 0;
}

void foo(std::string& s)
{
if(*(s.end()) != '\\')

if ( !s.empty() && *s.rbegin() == '\\' )
{
s += '\\';
}
}

When I run that, the program outputs:
c:\blah\\

I thought (and need it to be) would be:
c:\blah\

What am I doing wrong?

string::end() returns an iterator that is 1 passed the last char
in the string. You should never dereference such an iterator as you
did above (*s.end()) as it isn't actually pointing to anything.

The rbegin() member I use above returns a reverse_iterator (the first
item in the reversed sequence is the last in the forward sequence).

The !s.empty() is required as dereferencing this iterator if s is empty
would be just as bad as derefrenceing s.end().


HTH.

Rob.
 
G

Gianni Mariani

William said:
Hello, consider the following code:

#include <iostream>
#include <string>

void foo(std::string& s);

int main()
{
const char* directory = "c:\\blah";
std::string s = directory;

foo(s);
foo(s);

std::cout << s << std::endl;

return 0;
}

void foo(std::string& s)
{
if(*(s.end()) != '\\')

s.end() points to a position *AFTER* the '\\'.
{
s += '\\';
}
}

When I run that, the program outputs:
c:\blah\\

I thought (and need it to be) would be:
c:\blah\

What am I doing wrong?

try *( s.end() -1 ) or s[s.size()-1] instead.
 
W

William Payne

Rob Williscroft said:
William Payne wrote in

if ( !s.empty() && *s.rbegin() == '\\' )


string::end() returns an iterator that is 1 passed the last char
in the string. You should never dereference such an iterator as you
did above (*s.end()) as it isn't actually pointing to anything.

The rbegin() member I use above returns a reverse_iterator (the first
item in the reversed sequence is the last in the forward sequence).

The !s.empty() is required as dereferencing this iterator if s is empty
would be just as bad as derefrenceing s.end().


HTH.

Rob.

Thanks alot, Rob, now it works great.

/ William Payne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top