473,770 Members | 5,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using std::string

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
Jul 19 '05 #1
13 13095
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
Jul 19 '05 #2

"Victor Hannak" <vi***********@ nospam.kodak.co m> wrote in message
news:bj******** **@news.kodak.c om...

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.
Jul 19 '05 #3

"Victor Hannak" <vi***********@ nospam.kodak.co m> wrote in message
news:bj******** **@news.kodak.c om...
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
Jul 19 '05 #4

Victor Hannak <vi***********@ nospam.kodak.co m> wrote in message
news:bj******** **@news.kodak.c om...
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::istringstr eam 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


Jul 19 '05 #5

"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:4i******** *********@newsr ead4.news.pas.e arthlink.net...
std::istringstr eam iss;
iss >> f;


The above fails (backwards from what the user wanted anyhow)
std::ostringstr eam oss;
oss << f;

Jul 19 '05 #6
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;

Jul 19 '05 #7

"Victor Hannak" <vi***********@ nospam.kodak.co m> skrev i en meddelelse
news:bj******** **@news.kodak.c om...
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
Jul 19 '05 #8
> Use an ostringstream for this kind of conversion.

float tmp = 5.0;
ostringstream oss;
oss << tmp;
string my_string = oss.str();


Great suggestions...t hat'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("succe ss = " + str(Success).su bstr(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
Jul 19 '05 #9
In article <bj**********@n ews.kodak.com>,
Victor Hannak <vi***********@ nospam.kodak.co m> 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_in t));
WriteGUI("A double: "+str(my_double ));
WriteGUI("A user-defined type: "+str(my_user_d efined_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++)
Jul 19 '05 #10

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

Similar topics

10
8182
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
7
10629
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 namespace std; : : vector<string>* getTyphoon()
2
2551
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 variables by placing them at the same address (my g++ compiler does this). Therefore the output of the given C program is compiler dependent. What is worse, the program does not do what its writer most likely intended, since, std::set's find()...
22
13333
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; char c; std::string str;
9
3785
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 code. for example, this is bad practice: header.h #include <string> using std::string;
5
12436
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 = "this is a string"; std::ifstream ifs = str; //<----- this is a just pseudo code.
3
1748
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 the class specification in a header file and the functions in a cpp file, for better organisation and linking. When I specify a string in the class definition in the header file I get "syntax error : missing ';' before identifier 'avOwnerName'"...
11
2454
by: Peter Olcott | last post by:
Does C++ have anything like this?
11
2716
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 receiving a char buffer into a string? Reason I ask is otherwise I have to guess at what size buffer to create? And then copy buffer to a string. Doesn't seem ideal.
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10001
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.