y*h 发帖数: 107 | 1 除了c_str(), 还有没有别的方法? thanks | q*****g 发帖数: 72 | 2 there is one way:
contrcut a vector from string,
then pass address of first element of vector:
string s("hello world");
vector v(s.begin(),s.end());
dosomething(&v[0], v.size());
【在 y*h 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 除了c_str(), 还有没有别的方法? thanks
| o**o 发帖数: 3964 | 3 可是为什么要这样?
【在 q*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : there is one way: : contrcut a vector from string, : then pass address of first element of vector: : string s("hello world"); : : vector v(s.begin(),s.end()); : : dosomething(&v[0], v.size());
| q*****g 发帖数: 72 | 4 sometimes people have their legacy C function and try to make use of them
【在 o**o 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 可是为什么要这样?
| t****t 发帖数: 6806 | 5 but c_str() is equivalent i think?
【在 q*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : sometimes people have their legacy C function and try to make use of them
| q*****g 发帖数: 72 | 6 ya, c_str() is first choice
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : but c_str() is equivalent i think?
| c*****e 发帖数: 11 | 7 1:another approach is data(), which return a memory block without '\0',
usually used in some cases requiring high efficiency, since c_str() is just
terminate()+data().
2:be careful with c_str() in some cases.
3:the vector is a kind of copy, which should be safer to some extent.
4:since c_str() return a reference pointer to the content of the string, you
'd better not change the content by c_str() and not save c_str() and use it
again. besides, due to different implementation of string, may
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : but c_str() is equivalent i think?
|
|