typecast int to string [英] typecast int to string

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

问题描述

大家好,


我想将int类型转换为std :: string我该怎么办呢。


这是样本代码。


int NewList [500];


//用整数值填充NewList。

。 ......

.......


//用指定位置的新列表值替换文件内容。 />

int i = 0;

std :: string line;

ifstream inFile(sample); //打开一个文件来读取

while(getline(inFile,line))

{


int comma1Pos = line.find('',''' );

int comma2Pos = line.find('','',comma1Pos + 1);


int numChars = comma2Pos - comma1Pos - 1;


line.erase(comma1Pos + 1,numChars); //

line.insert(comma1Pos + 1,(std :: string)NewList [i]);

i ++;

}

inFile.close();


在上面的代码中,函数insert将第二个参数作为一个字符串,所以我

正在尝试类型转换NewList [i]到字符串。

编译器抛出一个类型转换错误说类型cast int到string不是

可能。


我有什么方法可以输入相同的内容。

问候,

Venkat


Hi All,

I want to typecast int to std::string how can i do it.

Here is the sample code.

int NewList[500];

//Fill the NewList with integers values.
.......
.......

//Replace the file contents with new list values at a specified location.

int i=0;
std::string line;
ifstream inFile(sample);//opens a file to read
while (getline (inFile, line) )
{

int comma1Pos = line.find('','');
int comma2Pos = line.find('','', comma1Pos+1);

int numChars = comma2Pos - comma1Pos - 1;

line.erase(comma1Pos+1, numChars); //
line.insert(comma1Pos+1, (std::string)NewList[i]);
i++;
}
inFile.close();

In the above code the function insert takes 2nd argument as a string so i
was trying to type cast NewList[i] to string.
The complier throws a type cast error saying type cast int to string is not
possible.

Is there any way i can type cast the same.
regards,
Venkat


推荐答案

尝试类似
Try something like
int i = 0;
std :: string line;
ifstream inFile (样本); //打开一个文件读取
while(getline(inFile,line))

int comma1Pos = line.find('','') ;
int comma2Pos = line.find('','',comma1Pos + 1);

int numChars = comma2Pos - comma1Pos - 1;

行。 erase(comma1Pos + 1,numChars); //
std :: ostringstream os;

os<< NewList [i];

line.insert(comma1Pos + 1,os.str()); i ++;
}
inFile.close();
int i=0;
std::string line;
ifstream inFile(sample);//opens a file to read
while (getline (inFile, line) )
{

int comma1Pos = line.find('','');
int comma2Pos = line.find('','', comma1Pos+1);

int numChars = comma2Pos - comma1Pos - 1;

line.erase(comma1Pos+1, numChars); // std::ostringstream os;
os << NewList[i];
line.insert(comma1Pos+1, os.str()); i++;
}
inFile.close();




-

要获得我真实的电子邮件地址,删除两个onkas

-

Dipl.-通知。 Hendrik Belitz

中央电子学院

研究中心Juelich



--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich


2004年1月7日星期三19:39: 14 +0530,Venkat写道:
On Wed, 07 Jan 2004 19:39:14 +0530, Venkat wrote:
大家好,

我想将int类型转换为std :: string我该怎么办呢。

以下是示例代码。

int NewList [500];

//用整数值填充NewList。
..... 。
......

//用指定位置的新列表值替换文件内容。

int i = 0;
std :: string line;
ifstream inFile(sample); //打开一个文件读取
while(getline(inFile,line))


int comma1Pos = line.find('','');
int comma2Pos = line.find('','',comma1Pos + 1);

int numChars = comma2Pos - comma1Pos - 1;

line.erase(comma1Pos + 1,numChars); //
line.insert(comma1Pos + 1,(std :: string)NewList [i]);
i ++;
}
inFile.close();

在上面的代码中,函数insert将第二个参数作为一个字符串,所以我试图将newList [i]类型转换为字符串。
编译器抛出一个类型转换错误,说明类型转换int to string不可能。

我有什么方法可以输入相同的内容。
Hi All,

I want to typecast int to std::string how can i do it.

Here is the sample code.

int NewList[500];

//Fill the NewList with integers values.
......
......

//Replace the file contents with new list values at a specified location.

int i=0;
std::string line;
ifstream inFile(sample);//opens a file to read
while (getline (inFile, line) )
{

int comma1Pos = line.find('','');
int comma2Pos = line.find('','', comma1Pos+1);

int numChars = comma2Pos - comma1Pos - 1;

line.erase(comma1Pos+1, numChars); //
line.insert(comma1Pos+1, (std::string)NewList[i]);
i++;
}
inFile.close();

In the above code the function insert takes 2nd argument as a string so i
was trying to type cast NewList[i] to string.
The complier throws a type cast error saying type cast int to string is not
possible.

Is there any way i can type cast the same.




不,那是不是铸造的内容。施法可以改变一些东西到相关的东西。虽然对于人类整数和他们的

字符串表示可能是相关的,但对于计算机来说它们是非常不同的。


作为另外,你不应该在C ++中使用C风格的演员表,C ++有更多的演员阵容:static_cast<>,dynamic_cast<>,const_cast<>和

reinterpret_cast<>。熟悉这些并且再也不使用

C风格的演员。它会为你节省很多的悲伤。


所以问题现在变成了,如何将数字转换为字符串。你有多少种方法可以做到这一点,最容易和最多的C ++是:


#include< sstream>


std :: string toString(int i)

{

std :: stringstream s;

s<< i;

返回s.c_str();

}


好​​的,这样可行,但也许你想用这个对于无符号的整数。

或多头。您可以多次创建相同的函数,

重载参数:


std :: string toString(long){...}

std :: string toString(unsigned int){...}

std :: string toString(unsigned long){...}

std :: string toString(float){...}

std :: string toString(double){...}


幸运的是,有一个更方便。我们可以通过使用模板的魔力让编译器为

我们做这件事:


模板< typename T>

std :: string toString(T t)

{

std :: stringstream s;

s<< t;

返回s.c_str();

}


这将使编译器生成所有上述函数

自动,但只有我们实际使用的! (请注意,在使用它之前,编译器必须看到此代码

,您不能只使用

原型并在另一个C ++中定义该函数所以这通常是

进入一些标题。)


所以如果你使用toString(i),其中i是一个整数,编译器会

用T代替int,最后得到与上面相同的结果。但是如果

我们使用toString(l),其中l是long,编译器会自动生成

生成上述内容很长时间。模板可以非常强大!


HTH,

M4



No, that is not what casting is about. Casting can change something to
something related. Although for humans integers and their
string-representations may be related, for computers they are very
different.

As a side note, you should never use C-style casts in C++, C++ has much
better casts: static_cast<>, dynamic_cast<>, const_cast<> and
reinterpret_cast<>. Familiarize yourself with those and never use the
C-style casts again. It will save you a lot of grief.

So the question now becomes, how to convert a number to a string. Thee are
a number of ways to do so, the easiest and most C++ish:

#include <sstream>

std::string toString(int i)
{
std::stringstream s;
s << i;
return s.c_str();
}

OK, this works, but maybe you want to use this for unsigned ints as well.
Or for longs. You could create the same function multiple times,
overloading on the argument:

std::string toString(long) { ... }
std::string toString(unsigned int) { ... }
std::string toString(unsigned long) { ... }
std::string toString(float) { ... }
std::string toString(double) { ... }

Fortunately, there is an easier way. We can get the compiler to do it for
us by using the magic of templates:

template<typename T>
std::string toString(T t)
{
std::stringstream s;
s << t;
return s.c_str();
}

This will make the compiler produce all of the above functions
automagically, but only the ones we actually use! (Do note that this code
must be ''seen'' by the compiler before you use it, you cannot just use a
prototype and define the function in another C++ file. So this typically
goes in some header.)

So if you use toString(i), where i is an integer, the compiler will
substitute int for T and we end up with exectly the same as above. But if
we use toString(l), where l is a long, the compiler automagically
generates the above for a long. Templates can be so incredibly powerful!

HTH,
M4


Venkat,

或许,您想尝试一下Boost'的词汇演员设施。

提升设计师(STL的免费添加)提供了保存手段来实现你所要求的
。所以看一个例子,看看

http://www.boost.org/libs/conversion/lexical_cast.htm

Evan Carew


Venkat写道:
Venkat,

Probably, you want to try looking at Boost''s lexical cast facility. The
boost designers (a free add on to the STL) have provided save means to
do what you are asking. so see an example, take a look at

http://www.boost.org/libs/conversion/lexical_cast.htm

Evan Carew

Venkat wrote:
大家好,

我想将int类型转换为std :: string我该怎么做。

这是示例代码。

int NewList [500];

//用整数值填充NewList。
......
..... 。

//用指定位置的新列表值替换文件内容。

int i = 0;
std :: string line;
ifstream inFile(sample); //打开一个文件读取
while(getline(inFile,line))

int comma1Pos = line.find('', '');
int comma2Pos = line.find('','',comma1Pos + 1);

int numChars = comma2Pos - comma1Pos - 1;

line.erase(comma1Pos + 1,numChars); //
line.insert(comma1Pos + 1,(std :: string)NewList [i]);
i ++;
}
inFile.close();

在上面的代码中,函数insert将第二个参数作为一个字符串,所以我试图将newList [i]类型转换为字符串。
编译器抛出一个类型转换错误,说明类型转换int to string不可能。

我有什么方法可以输入相同的内容。

问候,
Venkat
Hi All,

I want to typecast int to std::string how can i do it.

Here is the sample code.

int NewList[500];

//Fill the NewList with integers values.
......
......

//Replace the file contents with new list values at a specified location.

int i=0;
std::string line;
ifstream inFile(sample);//opens a file to read
while (getline (inFile, line) )
{

int comma1Pos = line.find('','');
int comma2Pos = line.find('','', comma1Pos+1);

int numChars = comma2Pos - comma1Pos - 1;

line.erase(comma1Pos+1, numChars); //
line.insert(comma1Pos+1, (std::string)NewList[i]);
i++;
}
inFile.close();

In the above code the function insert takes 2nd argument as a string so i
was trying to type cast NewList[i] to string.
The complier throws a type cast error saying type cast int to string is not
possible.

Is there any way i can type cast the same.
regards,
Venkat






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

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