请教string的赋值问题
// 编译环境 DEV C++ #include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string str_text_one;
char source[] = "Hello";
int i=0;
while (source[i]) {
str_text_one[i] = source[i]; // 这样赋值为啥不行呢
i++;
}
cout << "the length :" << str_text_one.length () << endl;
cout << "str_text_one :" << str_text_one << endl;
cout << "str_text_one :";
for (i=0; i<strlen(source); i++) // 这样输出str_text_one中有值
cout << str_text_one[i];
cout << endl;
str_text_one = source; // 这样赋值可以 上面为啥不行呢?
int str_text_one_len = str_text_one.size ();
cout << "str_text_one_len = " << str_text_one_len <<endl;
cout << "str_text_one is :" << str_text_one << endl;
system ("PAUSE");
return 0;
}
问题如注释。。。