书上说,好像string对象就是指针(是不是可以这么说?)
比如
const char hello[] = { 'H', 'e', 'l', 'l', 'o', '
' }; const cha
比如
const char hello[] = { 'H', 'e', 'l', 'l', 'o', '
书上说,好像string对象就是指针(是不是可以这么说?)
比如
const char hello[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
string s=hello;
但是
为什么这里一定要写x.c_str()?
ifstream infile(x.c_str());
ps (那个程序在vs里能通过,在codeblocks里不能通过。。。)
哈哈哈
愚人节闪亮登场~
明白了,刚看书看到这里:string s=hello,这语句的意思就是用const char* 的hello指针构造函数构造一个string 多谢各位
csdn的好心人真多
比如
const char hello[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
string s=hello;
但是
#include<fstream>
#include<string>
using namespace std;
int main()
{
string x("in.txt");
ifstream infile(x);//为什么这里出错?
ofstream outfile("out.txt");
string s;
while (getline(infile, s))
outfile << s << endl;
return 0;
}
为什么这里一定要写x.c_str()?
ifstream infile(x.c_str());
ps (那个程序在vs里能通过,在codeblocks里不能通过。。。)
15 个解决方案
#1
对象是对象,指针是指针,没有一毛钱联系
#2
explicit basic_ifstream ( const char * filename,
ios_base::openmode mode = ios_base::in ); //这是原型
//如果VS可以通过的话说明VS实现了
explicit basic_ifstream ( const string &filename,
ios_base::openmode mode = ios_base::in );
#3
const char *c_str();
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
比如:最好不要这样:
char* c;
string s="1234";
c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理
应该这样用:
char c[20];
string s="1234";
strcpy(c,s.c_str());
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
比如:最好不要这样:
char* c;
string s="1234";
c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理
应该这样用:
char c[20];
string s="1234";
strcpy(c,s.c_str());
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作
#4
1楼正解。
说明你的VS实现(/部分实现)了C++ 0x.
说明你的VS实现(/部分实现)了C++ 0x.
#5
一会儿赵老师会来告诉你怎么从汇编的角度学C++
#6
哈哈哈
#7
string是个类,其中那个c_str()返回一个C风格的常量字符串指针 const char*,你可以访问他的内容,但不可更改,要更改的话可以使用_Myptr()返回非常量char*
#8
我的vs倒是编译不过去,
提示的是ifstream要求参数为const char*;而string不支持隐形转换将string转为const char*;
但是可以用string里的.c_str(),将string转为const char*就可以了,
提示的是ifstream要求参数为const char*;而string不支持隐形转换将string转为const char*;
但是可以用string里的.c_str(),将string转为const char*就可以了,
#9
简单点来说,c_str()把string编成了char* 类型!
#10
这个好理解呀,因为istream没有提供参数为 const string类构造函数,而提供了char*为参数的构造函数
如果VS里能行,就证明VS里提供了数为 const string类构造函数;
我给你讲一次,你哪里理解错了
const char hello[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
string s=hello;
在这里hello是指向“hello”的指针
而s就是一个string,不是什么指针
string s=hello,这语句的意思就是用const char* 的hello指针构造函数构造一个string
而不是简单的赋值!!!!!!!!!!!!!!!!!!!!!
如果VS里能行,就证明VS里提供了数为 const string类构造函数;
我给你讲一次,你哪里理解错了
const char hello[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
string s=hello;
在这里hello是指向“hello”的指针
而s就是一个string,不是什么指针
string s=hello,这语句的意思就是用const char* 的hello指针构造函数构造一个string
而不是简单的赋值!!!!!!!!!!!!!!!!!!!!!
#11
类型不匹配 就这么简单
#12
+1
#13
愚人节闪亮登场~
#14
string 的c_str()是把string的对象变为const char * 可以作为函数的参数进行传递,但只是临时的。。。
#15
明白了,刚看书看到这里:string s=hello,这语句的意思就是用const char* 的hello指针构造函数构造一个string 多谢各位
csdn的好心人真多
' }; const cha