473,785 Members | 2,842 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
13 13096

Ron Natalie <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"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;


Oops, yes, sorry about that.

-Mike

Jul 19 '05 #11
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_st r());

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.borl and.com and look for
borland.public. cppbuilder.lang uage.cpp
There are a few people there that understand both Borland stuff and STL
stuff.

HTH

Jul 19 '05 #12

"Victor Hannak" <vi***********@ nospam.kodak.co m> wrote in message
news:bj******** **@news.kodak.c om...
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( ));
}


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
Jul 19 '05 #13
Dave Vandervies wrote:
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)

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.

Jul 19 '05 #14

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

Similar topics

10
8184
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
13334
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
12439
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
1749
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
9480
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
10327
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
10092
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
9950
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...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3647
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.