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 13 13052
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
"Victor Hannak" <vi***********@nospam.kodak.com> wrote in message
news:bj**********@news.kodak.com... 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.
"Victor Hannak" <vi***********@nospam.kodak.com> wrote in message
news:bj**********@news.kodak.com... 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
Victor Hannak <vi***********@nospam.kodak.com> wrote in message
news:bj**********@news.kodak.com... 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
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:4i*****************@newsread4.news.pas.earthl ink.net... std::istringstream iss; iss >> f;
The above fails (backwards from what the user wanted anyhow)
std::ostringstream oss;
oss << f;
Victor Hannak wrote: 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;
"Victor Hannak" <vi***********@nospam.kodak.com> skrev i en meddelelse
news:bj**********@news.kodak.com... 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
> 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
In article <bj**********@news.kodak.com>,
Victor Hannak <vi***********@nospam.kodak.com> wrote: 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)
--
Dave Vandervies dj******@csclub.uwaterloo.ca
[Y]ou can write bad code that just barely works in both languages, or good
code that works only in one language -- so pick one, and write good code in
that language. --Chris Torek in comp.lang.c (crossposted to comp.lang.c++)
Ron Natalie <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m... "Mike Wahler" <mk******@mkwahler.net> wrote in message
news:4i*****************@newsread4.news.pas.earthl ink.net... std::istringstream iss; iss >> f;
The above fails (backwards from what the user wanted anyhow) std::ostringstream oss; oss << f;
Oops, yes, sorry about that.
-Mike 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
"Victor Hannak" <vi***********@nospam.kodak.com> wrote in message
news:bj**********@news.kodak.com... 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()); }
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
Dave Vandervies wrote: In article <bj**********@news.kodak.com>, Victor Hannak <vi***********@nospam.kodak.com> wrote:
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Angus Leeming |
last post by:
Hello,
Could someone explain to me why the Standard conveners chose to typedef
std::string rather than derive it from std::basic_string<char, ...>?
The result of course is that it is...
|
by: Forecast |
last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.
: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using...
|
by: Neil Zanella |
last post by:
Hello,
Consider the following program. There are two C style string stack variables
and one C style string heap variable. The compiler may or may not optimize
the space taken up by the two stack...
|
by: Jason Heyes |
last post by:
Does this function need to call eof after the while-loop to be correct?
bool read_file(std::string name, std::string &s)
{
std::ifstream in(name.c_str());
if (!in.is_open())
return false;
...
|
by: Fei Liu |
last post by:
In Accellerated C++, the author recommends that in a header file one should
not declare
using std::string, using std::vector etc instead one should directly specify
the namespace specifier in...
|
by: Assertor |
last post by:
Hi, all.
Is there any way to create an instance of std::ifstream using
std::string.
(through std::ifstream's constructor or assignment operator or
iterator, etc...)
i.e.
std::string str =...
|
by: =?Utf-8?B?QW5keQ==?= |
last post by:
Hello,
I'm trying to use a string type variable inside a class. I can do this fine
with no problems when the class is inside a cpp file simply by adding
#include <string>.
However, my program has...
|
by: Peter Olcott |
last post by:
Does C++ have anything like this?
|
by: Angus |
last post by:
I am working with a C API which often requires a char* or char buffer. If a
C function returns a char* I can't use string? Or can I? I realise I can
pass a char* using c_str() but what about...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |