使用std :: string [英] Using std::string

查看:78
本文介绍了使用std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Borland C ++ Builder 4编写的程序,并将

非GUI相关代码转换为可在任何地方运行的通用c ++。我的主要问题

此时正在处理此程序中使用的字符串类。此程序中的所有

字符串都是Borland AnsiString类。我想

将它们转换为使用std :: string。


为了避免破坏原始程序,我希望使用

std :: string除了GUI类以外的所有地方。在GUI类(这仍然是一个BCB4 GUI)中,我希望从std :: string到

AnsiString进行一些翻译,以保持与可视组件的兼容性。


所以2个问题:


1)将字符串从std :: string数据类型移动到AnsiString时

数据类型,是唯一一种将字符从一个

数据类型复制到另一个的唯一方法吗? (我之前从未使用过std :: string类)


2)在整个代码中,有一个用

关键字String声明的AnsiString变量;。有没有办法我可以设置一个别名,以便任何时候

编译器看到String,它使用std :: string类(即string>

小写)而不是? (我认为两者之间的操作符/方法非常相似,但如果我错了,请纠正我)


谢谢,


Vic

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

推荐答案

另外,如何将其他数据类型(如浮点数)转换为std :: string。使用

BCB AnsiString类,它很简单:


float tmp = 5.0;

String MyString = String( tmp);


这将导致:


MyString ==" 5.0"


如果有人知道一个很好的参考资料,我可以找到这种信息,

请指出我的方向,但我在网上找不到多少。

谢谢
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" < 6 *********** @ nospam.kodak.com>在消息中写道

news:bj ********** @ news.kodak.com ...

"Victor Hannak" <vi***********@nospam.kodak.com> wrote in message
news:bj**********@news.kodak.com...

1)移动时从std :: string数据类型到AnsiString
数据类型的字符串是唯一的方法来将字符从一种
数据类型复制到另一种数据类型? (我之前从未使用过std :: string类)


如果你查看字符串的接口,并了解接口

AnsiString,我认为你会发现一些相当容易的东西。

2)在整个代码中,有一些用
关键字String声明的AnsiString变量。有没有办法可以设置一个别名,以便编译器看到String时随时都是
,它使用std :: string类(即string,
小写)? (我认为两者之间的操作符/方法非常兼容,但如果我错了,请纠正我)

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)




我不知道,因为我从未使用过AnsiString。但是typedef将

可能做你想做的事。



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




" Victor Hannak" < 6 *********** @ nospam.kodak.com>在消息中写道

news:bj ********** @ news.kodak.com ...

"Victor Hannak" <vi***********@nospam.kodak.com> wrote in message
news:bj**********@news.kodak.com...
另外,我如何转换其他数据类型喜欢浮动到std :: string。使用
BCB AnsiString类,它很简单:

float tmp = 5.0;
字符串MyString = String(tmp);

这会导致:

MyString ==" 5.0"


使用ostringstream进行此类转换。


float tmp = 5.0;

ostringstream oss;

oss<< tmp;

string my_string = oss.str();

如果有人知道一个很好的参考资料,我可以找到这种信息,
请指点我在那个方向,但我在网上找不到多少。

谢谢
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


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

john


这篇关于使用std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆