Using std::string

V

Victor Hannak

I am taking a program written in Borland C++ Builder 4 and converting the
non-GUI related code to be generic c++ that can run anywhere. My main issue
at this point is dealing with the string classes used in this program. All
strings in this program are of the Borland AnsiString class. I would like
to convert them over to use std::string.

In order to keep from breaking the original program, I was hoping to use
std::string everywhere except the GUI class. In the GUI class (which is
still a BCB4 GUI), I was hoping to do some translation from std::string to
AnsiString to maintain compatibility with the visual components.

So 2 questions:

1) When moving a string from the std::string datatype to the AnsiString
datatype, is the only way to do it to copy character by character from one
datatype to the other? (I have never used the std::string class before)

2) Throughout the code, there are AnsiString variables declared with the
keyword "String". Is there a way I can set up an alias so that anytime the
compiler sees "String", it uses the std::string class (i.e. "string"
lower-case) instead? (I think the operators/methods are pretty much
compatible between the two, but correct me if I am wrong)

Thanks,

Vic
 
J

jeffc

Victor Hannak said:
1) When moving a string from the std::string datatype to the AnsiString
datatype, is the only way to do it to copy character by character from one
datatype to the other? (I have never used the std::string class before)

If you look at the interface for string, and understand the interface to
AnsiString, I think you'll find something fairly easy.
2) Throughout the code, there are AnsiString variables declared with the
keyword "String". Is there a way I can set up an alias so that anytime the
compiler sees "String", it uses the std::string class (i.e. "string"
lower-case) instead? (I think the operators/methods are pretty much
compatible between the two, but correct me if I am wrong)

I don't know, because I've never used AnsiString. But "typedef" will
probably do what you want.
 
V

Victor Hannak

Also, how do I convert other data types like floats to std::string. Using
the BCB AnsiString class, it was as simple as:

float tmp = 5.0;
String MyString = String(tmp);

This would result in:

MyString == "5.0"

If anyone knows of a good reference where I can find this kind of info,
please point me in that direction, but I could not find much on the web.

Thanks
 
J

John Harrison

Victor Hannak said:
Also, how do I convert other data types like floats to std::string. Using
the BCB AnsiString class, it was as simple as:

float tmp = 5.0;
String MyString = String(tmp);

This would result in:

MyString == "5.0"

Use an ostringstream for this kind of conversion.

float tmp = 5.0;
ostringstream oss;
oss << tmp;
string my_string = oss.str();
If anyone knows of a good reference where I can find this kind of info,
please point me in that direction, but I could not find much on the web.

Thanks


http://www.dinkumware.com/refxcpp.html

john
 
M

Mike Wahler

Victor Hannak said:
Also, how do I convert other data types like floats to std::string. Using
the BCB AnsiString class, it was as simple as:

float tmp = 5.0;
String MyString = String(tmp);

This would result in:

MyString == "5.0"

If anyone knows of a good reference where I can find this kind of info,
please point me in that direction, but I could not find much on the web.

You'll need books. See www.accu.org for peer reviews.

About your question: you can use a stringstream to
do the conversion:

float f(3.14);
std::istringstream iss;
iss >> f;
std::string s(iss.str());
std::cout << s << '\n'; /* prints 3.14 */

The best reference I know of for the C++
standard library is: www.josuttis.com/libbook

-Mike
 
R

Rolf Magnus

Victor said:
I am taking a program written in Borland C++ Builder 4 and converting
the non-GUI related code to be generic c++ that can run anywhere. My
main issue at this point is dealing with the string classes used in
this program.
All strings in this program are of the Borland AnsiString class. I
would like to convert them over to use std::string.

In order to keep from breaking the original program, I was hoping to
use std::string everywhere except the GUI class. In the GUI class
(which is still a BCB4 GUI), I was hoping to do some translation from
std::string to AnsiString to maintain compatibility with the visual
components.

So 2 questions:

1) When moving a string from the std::string datatype to the
AnsiString datatype, is the only way to do it to copy character by
character from one datatype to the other? (I have never used the
std::string class before)

Most people here probably have never used the AnsiString class.
2) Throughout the code, there are AnsiString variables declared with
the
keyword "String". Is there a way I can set up an alias so that
anytime the
compiler sees "String", it uses the std::string class (i.e. "string"
lower-case) instead? (I think the operators/methods are pretty much
compatible between the two, but correct me if I am wrong)

You can use a typedef:

typedef std::string String;
 
P

Peter Koch Larsen

Victor Hannak said:
Also, how do I convert other data types like floats to std::string. Using
the BCB AnsiString class, it was as simple as:

float tmp = 5.0;
String MyString = String(tmp);

This would result in:

MyString == "5.0"

If anyone knows of a good reference where I can find this kind of info,
please point me in that direction, but I could not find much on the web.

Thanks
As John Harrison and Mike Wahler have already pointed out, the stringstream
is often the way to go, but you might also want to have a look at boost,
where there is a function lexical cast. Using boost, converting is as simple
as:

MyString = lexical_cast< std::string >(tmp);

The downside is that you can't controle the precise formatting (number of
decimals and stuff like that) with lexical_cast.... here a stringstream is
still the way to go.

Kind regards
Peter
 
V

Victor Hannak

Use an ostringstream for this kind of conversion.
float tmp = 5.0;
ostringstream oss;
oss << tmp;
string my_string = oss.str();

Great suggestions...that's exactly the info I needed... but one more
question...

I have taken the above code and made it into a function:

string str(float In) {
ostrstream oss;
oss << In;
return(oss.str());
}

This way, I can do something like:

float Success = 95.5;
WriteGUI("success = " + str(Success).substr(0,3) + "%");

Where WriteGUI() is my own function that writes something to the GUI (this
is where I convert it back to AnsiString as described in my previous post).

So I _could_ replicate/overload the str() function for each type of data
that I would want to potentially print out. But is there a way to make it
generic so that it doesn't matter what data type I am passing in? (Since
ultimately it is limited to what ostrstream can accept)

Vic
 
D

Dave Vandervies

Victor Hannak said:
string str(float In) {
ostrstream oss;
oss << In;
return(oss.str());
}
[...]

So I _could_ replicate/overload the str() function for each type of data
that I would want to potentially print out. But is there a way to make it
generic so that it doesn't matter what data type I am passing in? (Since
ultimately it is limited to what ostrstream can accept)

This looks to me like a good place for a template:

template <class T> std::string str(T in)
{
ostringstream os;
os<<in;
return os.str();
}

/*Use like this:*/
WriteGUI("An int: "+str(my_int));
WriteGUI("A double: "+str(my_double));
WriteGUI("A user-defined type: "+str(my_user_defined_type));


I'd probably try inlining it, too, though I'm not sure that would make
a noticeable difference in either speed or size.


dave
(Still working on the jump from C to C++, so any comments on my code
would be welcome)
 
D

Duane Hebert

1) When moving a string from the std::string datatype to the AnsiString
datatype, is the only way to do it to copy character by character from one
datatype to the other? (I have never used the std::string class before)

AnsiString has a ctor that takes a char* so you can do

std::string Flarn("whatever");
AnsiString Spoo(Flarn.c_str());

2) Throughout the code, there are AnsiString variables declared with the
keyword "String". Is there a way I can set up an alias so that anytime the
compiler sees "String", it uses the std::string class (i.e. "string"
lower-case) instead? (I think the operators/methods are pretty much
compatible between the two, but correct me if I am wrong)

String is a typedef for AnsiString. (or is it that AnsiString is a typedef
for String...)

There are some differences between
std::string and AnsiString that you need to know.

AnsiString is a Delphi construct and is 1 based. BCB4 allows you to access
the first character at index 0
but this doesn't work after BCB5.

AnsiString's c_str() doesn't return a const char* so you can modify the
String via
its c_str() function.

Also, BCB4's STL implementation is pretty old. BCB5 used Rogue Wave for
it's
STL which was pretty buggy. BCB6 uses STLPort which seems much better.

At any rate, you'd get a lot more information at one of Borland's newsgroups

Try the server newsgroups.borland.com and look for
borland.public.cppbuilder.language.cpp
There are a few people there that understand both Borland stuff and STL
stuff.

HTH
 
J

John Harrison

Victor Hannak said:
Great suggestions...that's exactly the info I needed... but one more
question...

I have taken the above code and made it into a function:

string str(float In) {
ostrstream oss;
oss << In;
return(oss.str());
}

Just noticed you made a crucial change to my suggestion, you've used
ostrstream instead of ostringstream.

ostrstream is not recommended, but if you insist you need this code

string str(float In)
{
ostrstream oss;
oss << In << ends;
char* tmp = oss.str();
string Out(tmp);
delete[] tmp;
return Out;
}

ostrstream predates the string class, its based on C null terminated strings
(that's what ends is for, it adds the null terminator, and that's why you
have to delete[] the memory when you're done).

Use ostringstream if it is available to you.

john
 
R

Rolf Magnus

Dave said:
Victor Hannak said:
string str(float In) {
ostrstream oss;
oss << In;
return(oss.str());
}
[...]

So I _could_ replicate/overload the str() function for each type of
data
that I would want to potentially print out. But is there a way to
make it generic so that it doesn't matter what data type I am passing
in? (Since ultimately it is limited to what ostrstream can accept)

This looks to me like a good place for a template:

template <class T> std::string str(T in)
{
ostringstream os;
os<<in;
return os.str();
}

/*Use like this:*/
WriteGUI("An int: "+str(my_int));
WriteGUI("A double: "+str(my_double));
WriteGUI("A user-defined type: "+str(my_user_defined_type));


I'd probably try inlining it, too, though I'm not sure that would make
a noticeable difference in either speed or size.


dave
(Still working on the jump from C to C++, so any comments on my code
would be welcome)


You should use a const reference for the parameter:

template <class T> std::string str(const T& in)

With this, the object itself is used, not a copy of it. This makes a
difference if copying takes a long time or your class isn't copyable at
all.
 

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

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top