传递ostringstream [英] Passing on ostringstream

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

问题描述




我的c ++程序看起来像这样


void api1(){

cin< ;< info;

ostringstream ostr;

ostr<< info;

api2(ostr.str()。c_str()); //从ostreamstream获取字符串和

从字符串获取char *

}


void api2(const char * ch){

插入地图(ch);

}


api2a的输入样本之后我在最后检查了地图的内容

。我看到了一些垃圾值。


有人可以解释我的程序出了什么问题吗?我应该

不要在ostringstream上做c_str()吗?


谢谢,

Suresh

Hi,

My c++ program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);
}

Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.

Thanks,
Suresh

推荐答案

vs ******** @ gmail.com 写道:
vs********@gmail.com writes:




我的c ++程序看起来像这样


void api1(){

cin<< info;

ostringstream ostr;

ostr<< info;

api2(ostr.str()。c_str()); //从ostreamstream获取字符串和

从字符串获取char *

}


void api2(const char * ch){

插入地图(ch);

}


api2a的输入样本之后我在最后检查了地图的内容

。我看到了一些垃圾值。


有人可以解释我的程序出了什么问题吗?我应该

不要在ostringstream上做c_str()吗?
Hi,

My c++ program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);
}

Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.



std :: string.c_str()的返回值指向

std :: string所拥有的存储空间。一旦对象超出范围,指针就不再有效了。从std :: ostringstream.str()返回的std :: string对象在下一个序列点,即函数调用结束时超出范围,所以如果

你的api2()函数藏在那个指针的某个地方,指针将在api2()返回后不再有效,并且调用者的函数调用是

完成。

----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.4.9(GNU / Linux)


iEYEABECAAYFAkiAeoQACgkQx9p3GYHlUOLSWACdHrTzPjHMD3 tGn8CvQm7lQjL7

eUwAniynmx9HMFdHI / dpM5P + 1KE5dzV /

= D03 +

----- END PGP SIGNATURE ---- -

The return value from std::string.c_str() points to storage owned by the
std::string. Once the object goes out of scope, the pointer is no longer
valid. The std::string object returned from std::ostringstream.str() goes
out of scope at the next sequence point, the end of the function call, so if
your api2() function stashed away that pointer somewhere, that pointer will
no longer be valid once api2() returns, and the caller''s function call is
complete.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkiAeoQACgkQx9p3GYHlUOLSWACdHrTzPjHMD3 tGn8CvQm7lQjL7
eUwAniynmx9HMFdHI/dpM5P+1KE5dzV/
=D03+
-----END PGP SIGNATURE-----


2008-07-18 12:58, vs ******** @ gmail.com 写道:
On 2008-07-18 12:58, vs********@gmail.com wrote:




我的c ++程序看起来像这样


void api1(){

cin<< info;

ostringstream ostr;

ostr<< info;

api2(ostr.str()。c_str()); //从ostreamstream获取字符串和

从字符串获取char *

}


void api2(const char * ch){

insert map(ch);
Hi,

My c++ program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);



^^^^^^^^^^^^^^^^^语法错误!

^^^^^^^^^^^^^^^^^ Syntax Error!


api2a的输入样本之后我在最后检查了地图的内容

。我看到了一些垃圾值。


有人可以解释我的程序出了什么问题吗?我应该

不要在ostringstream上做c_str()吗?
Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.



我不能肯定地说,因为我不知道是什么地图或如何插入

(请参阅第5.8节)常见问题)。我猜你会在地图中插入ch,因为ch是一个指向临时的指针(或者在

最小的一个对象,它将在末尾被销毁) api1())你只有

以后会得到垃圾。


而不是使用c_str()来获取一个con char *你应该使用return-

来自str()的值作为地图中的关键字,使用char数组来玩

除非你不得不坏。


-

Erik Wikstr ?? m


I can not say for sure since I do not know what map is or how you insert
into it (please see section 5.8 in the FAQ). I would guess that you
insert ch in the map, and since ch is a pointer to a temporary (or at
least an object which will be destroyed at the end of api1()) you only
get garbage later on.

Instead of using c_str() to get a cont char* you should use the return-
value from str() as the key in the map, playing around with char arrays
unless you have to is bad.

--
Erik Wikstr??m


嗨Sam / Erick,


我是详细编写我的程序


std :: map< string,intcache;


void api1(){

cin<<键; // string

cin<<值; // int

ostringstream ostr;

ostr<< key;

api2(ostr.str()。c_str(),value);


}


void api2(const char * info,int value){

cache [info] = value;

}


所以,在这里我不存储信息*但是将

指针的值复制到字符串并将其保存在我的MAP中。

但是当我打印此缓存时它指的是垃圾价值。


+ suresh
Hi Sam/Erick,

I am writing my program in detail

std::map<string,intcache;

void api1() {

cin << key; // string
cin << value; // int
ostringstream ostr;
ostr << key;
api2(ostr.str().c_str(), value);

}

void api2(const char* info, int value) {
cache[info] = value;
}

So, here i am not storing the info* but copying the value of that
pointer to string and keeping it in my MAP.
But when i print this cache it refers to junk value.

+suresh


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

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