想了好久的问题啊
定义string ofile 后ofile.c_str()是什么东西啊?
另外一个string 对象给他输入赋值的时候他是遇到空格和enter键就认为你输入完成了,以后你再输入的他就忽略了,怎么让一个string对象包含一段英语句子啊?
问题点数:20、回复次数:7Top
1 楼carbon107(<软件开发思想.h>)回复于 2003-11-04 19:08:27 得分 8
1.basic_string::c_str
const E *c_str() const;
The member function returns a pointer to a nonmodifiable C string constructed by adding a terminating null element (E(0)) to the controlled sequence. Calling any non-const member function for *this can invalidate the pointer.
2
gamma = new String("This is a string");
delta = new String("This is also a string");
Top
2 楼buaaaladdin(阿拉丁的灯)回复于 2003-11-04 19:17:31 得分 3
2.用循环每次读入一行,然后把它连接到上一次的输入上去。或者直接用字符数组读入,终止条件为EOF,然后以这个数组构建string。Top
3 楼mjfsun(清风)回复于 2003-11-04 19:39:13 得分 0
可以用gets()函数阿Top
4 楼AHigher(我不抗日who抗日)回复于 2003-11-04 20:29:10 得分 0
能再具体点吗
最好举个例子,我是运行时输入的。Top
5 楼siegf(nowhy!)回复于 2003-11-05 00:16:42 得分 7
第一个问题,返回的结果是 (char *) 类型的字符串。
第二个问题 可以用 gets() 函数,
给个例子,刚刚在 dev-cpp 下通过
#include <iostream>
#include <string>
#include <malloc.h>
#include <stdio.h>
using namespace std;
int main()
{
char *tmp = (char *)malloc(255);
gets(tmp);
string a(tmp);
cout<<a<<endl;
system("pause");
return 0;
}Top
6 楼sunjx119(睿锐)回复于 2003-11-05 01:07:15 得分 2
将string转化成字符串
string ofile ;
ofile.c_str();//这个式子返回一个指针char *
Top
7 楼AHigher(我不抗日who抗日)回复于 2003-11-05 08:12:21 得分 0
要是想用cin怎么做呢
不想用gets().Top




