473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Why is \ added both times?

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
Jul 22 '05 #1
4 1309
William Payne wrote in news:bp**********@news.island.liu.se:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
William Payne wrote:
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.

Jul 22 '05 #3

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.200...
William Payne wrote in news:bp**********@news.island.liu.se:
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.
--
http://www.victim-prime.dsl.pipex.com/


Thanks alot, Rob, now it works great.

/ William Payne
Jul 22 '05 #4
William Payne wrote:
Hello, consider the following code:

#include <iostream>
#include <string>

void foo(std::string& s);

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


http://www.parashift.com/c++-faq-lit...html#faq-15.16

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Celebrate | last post by:
I'm noticing that when my homepage first starts up, all the menu links to other pages have the session info appended to the URL. For example: ...
8
by: alanstew | last post by:
With the body tag calling out 'window onload', a function with a 'window.open' fails at the 'window.open' line. If I cut out the body tag, the function executes as normal. At first I thought it...
0
by: etamp | last post by:
Etamp.net, the fast rss-reader for RSS content including weblogs and news, the fast search engine for RSS content including weblogs and news, No software download, All OS' & Browsers, IE, Netscape,...
7
by: who be dat? | last post by:
I need some help here. I'm creating a list on a page where the list is created from a dataset with two tables linked with a datarelation. The first table is a list of groups while the second...
4
by: Martin Hill | last post by:
Hi there, How can I find the components added to web form from my custom web control? Suppose I added a Timer component (with name "timer1") onto the web form, from my user web control, how...
5
by: Simon Harvey | last post by:
Hi everyone, I'm having a really weird problem with one of my pages. Whenever I hit the submit button, it reloads the page but another side menu control appears underneath the original one. This...
4
by: viuxrluxvbbc | last post by:
Hi im trying to write a program that will read in numbers and display them in ascending order along with a count of how many times it repeats. i got the numerical order portion done but cant figure...
3
by: OJ | last post by:
Hi, I have added code to run on a button click event which adds usercontrols to an asp:Panel control held in a contentplaceholder defined by the masterpage. for (int x = 0; x <...
21
by: bilgekhan | last post by:
After doing a succcessful insert() or find() on a set<Tcontainer is it possible to get the item number of this item in the set? (ie. its zero-based sequence number (position/location/rank/index)...
1
by: Shraddha | last post by:
Hi, I am adding some ASP.Net user controls (.ascx file) dynamically on the button click. The user control will get added as many times userhits the button. Now on the click of the submit button, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.