Re: 答复: [cpp] 关于STL中string的问题
analyst
qiaojie at gmail.com
Fri Mar 3 11:29:36 CST 2006
为什么都喜欢用这么麻烦而且低效的东西,不要为了用C++而用C++,别忘了还有C
char buf[32];
_snprintf(buf, 32, "%d", 10);//或者用itoa(10, buf, 10);
string str = buf;
在06-3-3,千里马肝 <oiramario at gmail.com> 写道:
>
> 例码:
> std::vector<char> buf(buffer_size, 0);
> _snprintf(&buf[0], buffer_size, "%d\0", 10);
> // 赋值给字符串
> std::string str = &buf[0];
> MessageBox(NULL, str.c_str(), NULL, MB_OK);
> 好处:
> 1. 使用std::vector避免手动new/delete
> 2. 使用stl可以充分利用stl的alloc(如stlport的memory pool)
> 3. 使用_snprintf防止溢出
> 4. std::string = std::vector(安逸:)
>
> -----原始邮件-----
> *发件人:* cpp-bounces at codingnow.com [mailto:cpp-bounces at codingnow.com]*代表 *
> 苏益ꪏ
> *发送时间:* 2006年3月2日 14:54
> *收件人:* cpp at codingnow.com
> *主题:* [cpp] 关于STL中string的问题
>
> 请先看这段代码。
> ----------------------------------------------------------------
> //我希望把string赋值10,然后显示
> string Buffer;
> sprintf( Buffer.begin(),"%d",10);
> MessageBox(NULL,Buffer.begin(),NULL,MB_OK);
> ----------------------------------------------------------------
>
>
> 建个程序,编译它,是通不过的。原因是Buffer没有初值,调用Buffer.begin()返回是NULL,第三句用MessageBox弹出一个NULL的字符串,这是不可取的。
>
> 所以我改正以后,程序为
> ---------------------------------------------------------------
> //改正后的程序
> string Buffer = "a"; //我随便给了个初值
> sprintf( Buffer.begin(),"%d",10);
> MessageBox(NULL,Buffer.begin(),NULL,MB_OK);
> ----------------------------------------------------------------
> 但是,上面这段程序,可读性能差。别人会想:作者为什么给个"a"而不是"b"呢?
>
> 所以我想请教大家:
> (1)这问题有没有更好的方法?最好不要MFC,也不用char数组,因为整个程序中其它地方我都用了string。
> (2)STL为什么这么设计?
> 难道定义Buffer以后,还没有分配空间?如果已经有空间,为什么不用Begin()返回首个字符的地址?这样做意义呵在?
>
>
>
>
>
> ===============================================
> 快来和我一起享受TOM免费邮箱吧! 看看除了1.5G,还有什么?<http://bjcgi.163.net/cgi-bin/newreg.cgi?rf=050602>
>
> <http://vip.tom.com/info/ggd_050409/index.html> 敲开精彩 聊天世界<http://chat.tom.com/>
> :网通用户 <http://chat.tom.com/> 电信用户 <http://schat.tom.com/>
>
> 明星送祝福(http://sr.tom.com): 周杰伦 <http://sr.tom.com/send.php?id=330&type=2>
> 蔡依林 <http://sr.tom.com/send.php?id=315&type=2> 王力宏<http://sr.tom.com/send.php?id=257&type=2>
> 田震 <http://sr.tom.com/send.php?id=247&type=2> 代您送去生日祝福
>
> 万首金曲免费送(http://mm.tom.com/ivr/) : 你到底爱谁
> <http://fs.tom.com/sms/ivr_song_step0.php?var=1&id=37987&user_id=3&code_id=iw4001&>
> 千年之恋<http://fs.tom.com/sms/ivr_song_step0.php?var=1&id=38205&user_id=3&code_id=iw4001&>
> 我是真的爱你
> <http://fs.tom.com/sms/ivr_song_step0.php?var=1&id=37938&user_id=3&code_id=is0001&>
> 一辈子做你的女孩
> <http://fs.tom.com/sms/ivr_song_step0.php?var=1&id=1431&user_id=3&code_id=iw4001&>
>
> 全部彩铃免费送(http://mm.tom.com/cailing/): 我发财了发财了<http://fs.tom.com/sms/cailing_step0.php?var=1&id=462&songtype=1&user_id=3&code_id=000000&songprovince=甘肃&>
> 你喜欢我就说嘛<http://fs.tom.com/sms/cailing_step0.php?var=1&id=300&songtype=1&user_id=3&code_id=000000&songprovince=北京&>
> 两只蝴蝶
> <http://fs.tom.com/sms/cailing_step0.php?var=1&id=1345&songtype=0&user_id=3&code_id=&songprovince=北京&>
> 冲动的惩罚
> <http://fs.tom.com/sms/cailing_step0.php?var=1&id=376&songtype=0&user_id=3&code_id=&songprovince=北京&>
> ===============================================
>
>
> _______________________________________________
> Cpp mailing list
> Cpp at codingnow.com
> http://codingnow.com/mailman/listinfo/cpp
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codingnow.com/pipermail/cpp/attachments/20060303/b6dad067/attachment.html
More information about the Cpp
mailing list