string的问题

leez0301 2008-09-28 01:17:22
用C语言的字符串函数时需包含string.h头文件 ,在c++中用到string类型时需要包含string文件,这两个文件有什么关系,在c++中用了string之后也能调用c语言相关的字符串处理函数。 还有 如何将string转换为char*(除了挨个赋值之外)。
...全文
81 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
luxiaoxun 2008-09-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 once_and_again 的回复:]
3 copy
比如
string str="hello";
char p[40];
str.copy(p,5,0);
*(p+5)='\0';
cout < <p;

正确吗?疑惑、
[/Quote]

就是那样用的
once_and_again 2008-09-28
  • 打赏
  • 举报
回复
3 copy
比如
string str="hello";
char p[40];
str.copy(p,5,0);
*(p+5)='\0';
cout < <p;

正确吗?疑惑、
luxiaoxun 2008-09-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 leez0301 的帖子:]
用C语言的字符串函数时需包含string.h头文件 ,在c++中用到string类型时需要包含string文件,这两个文件有什么关系,[/Quote]

下面是C++头文件的现状:

· 旧的C++头文件名如<iostream.h>将会继续被支持,尽管它们不在官方标准中。这些头文件的内容不在名字空间std中。

· 新的C++头文件如<iostream>包含的基本功能和对应的旧头文件相同,但头文件的内容在名字空间std中。(在标准化的过程中,库中有些部分的细节被修改了,所以旧头文件和新头文件中的实体不一定完全对应。)

· 标准C头文件如<stdio.h>继续被支持。头文件的内容不在std中。

· 具有C库功能的新C++头文件具有如<cstdio>这样的名字。它们提供的内容和相应的旧C头文件相同,只是内容在std中。

《Effective c++》
太乙 2008-09-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kkndciapp 的回复:]
string 是c++标准库里面其中一个,封装了对字符串的操作
把string转换为char* 有3中方法:
1。data
如:
string str="abc";
char *p=str.data();
2.c_str
如:string str="gdfd";
char *p=str.c_str();
3 copy
比如
string str="hello";
char p[40];
str.copy(p,5,0);
*(p+5)='\0';
cout < <p;
[/Quote]

studying!~!
kkndciapp 2008-09-28
  • 打赏
  • 举报
回复
string 是c++标准库里面其中一个,封装了对字符串的操作
把string转换为char* 有3中方法:
1。data
如:
string str="abc";
char *p=str.data();
2.c_str
如:string str="gdfd";
char *p=str.c_str();
3 copy
比如
string str="hello";
char p[40];
str.copy(p,5,0);
*(p+5)='\0';
cout<<p;
XiaoG602 2008-09-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str1="Hello";
char *str2=const_cast<char*>(str1.c_str());
cout<<str2;
return 0;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧