这是一个协议发送消息: *消息代码,pe。 1为HELLO *消息内容,pe。 5(种子) 两个字段都被@ +分隔开来 / * ******************** *************************** * / istringstream in; in.str(" 1 @ + @ 5); int msgcode = -1; int seed = -1; string spacer; in>> msgcode>> spacer>>种子; cout<<"" msgcode ="<<< msgcode<<",spacer ="<< spacer<<"" s eed =" ;<<种子<< endl; / * ********************************* ************** * / 然而,当我运行它时,输出是: msgcode = 1,spacer = @ + @ 5, seed = -1 5总是属于字符串spacer 也用空格替换@ + @或取一个char *作为spacer没有把5放在种子中的字符串spacer中。总是到达终点(如果我们设置555 而不是5),这可能是因为流不能告诉结束字符串并启动整数。 。 如果您使用的是VS 2005,这是库中的已知错误。 查看此主题详情: http://groups.google.com/group/micro...d09bf204?tvc=1 ro ********** @ gmail.com 写道: k: arel写道: / * ********************************* ************** * / istringstream in; in.str(" 1 @ + @ 5"); msgcode>> spacer>>种子; 字符串" spacer"总是到达终点(如果我们设置555 而不是5),这可能是因为流不能告诉结束字符串并启动整数。 。 读取一个字符串会占用任何空格。我不认为这将是你问题的最佳答案。 啊,好的,我错过了。但是,VS2005也存在问题。 today i''ve discovered the istringstream objecti''m trying to read a string and split it up in a number of fields,consisting of integers and stringsit''s for a protocol that sends messages with:* message code, pe. 1 for HELLO* message content, pe. 5 (seed)both field are separated by @+@/* *********************************************** */istringstream in;in.str("1@+@5");int msgcode=-1;int seed=-1;string spacer;in >> msgcode >> spacer >> seed;cout<<"msgcode="<<msgcode<<",spacer="<<spacer<<",s eed="<<seed<<endl;/* *********************************************** */yet, when i run this, the output is:msgcode=1,spacer=@+@5,seed=-1the 5 always belongs to the string "spacer"also replacing the @+@ by a space or taking a char* as data type for"spacer" doesn''t put the 5 in the "seed"the string "spacer" alway reaches to the end (also if we set there 555instead of just 5), it''s probably because the stream can''t tell whereto end the string and start the integer...has anybody any idea how i could bypass this? 解决方案k:arel wrote: /* *********************************************** */ istringstream in; in.str("1@+@5"); in >> msgcode >> spacer >> seed; the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it''s probably because the stream can''t tell where to end the string and start the integer...Reading a string gobbles up everything up to any whitespace. I don''tthink it will be the best answer to your problem. k:arel wrote: today i''ve discovered the istringstream object i''m trying to read a string and split it up in a number of fields, consisting of integers and strings it''s for a protocol that sends messages with: * message code, pe. 1 for HELLO * message content, pe. 5 (seed) both field are separated by @+@ /* *********************************************** */ istringstream in; in.str("1@+@5"); int msgcode=-1; int seed=-1; string spacer; in >> msgcode >> spacer >> seed; cout<<"msgcode="<<msgcode<<",spacer="<<spacer<<",s eed="<<seed<<endl; /* *********************************************** */ yet, when i run this, the output is: msgcode=1,spacer=@+@5,seed=-1 the 5 always belongs to the string "spacer" also replacing the @+@ by a space or taking a char* as data type for "spacer" doesn''t put the 5 in the "seed" the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it''s probably because the stream can''t tell where to end the string and start the integer...If you''re using VS 2005, this is a known bug in the library.See this thread for details: http://groups.google.com/group/micro...d09bf204?tvc=1ro**********@gmail.com wrote: k:arel wrote: /* *********************************************** */ istringstream in; in.str("1@+@5"); in >> msgcode >> spacer >> seed; the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it''s probably because the stream can''t tell where to end the string and start the integer... Reading a string gobbles up everything up to any whitespace. I don''t think it will be the best answer to your problem.Ah, good point, I missed that. However, there is also an issue with VS2005. 这篇关于istringstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!