string s1="hello,world";
string s2=new string;
for(unsigned int i=0;i<=s1.length();i++) s2[i]=s1[i];
编译ok,但运行error,好像是指向了非法地址,这是什么道理?作者: 死了ye要爱 时间: 2009-11-3 01:16
You made more than 1 mistake here.
=======================================
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "hello,world";
string* s2 = new string;
s2->resize(s1.size());
for (unsigned int i = 0; i < s1.length(); i++)
{
s2->at(i) = s1[i];
}
cout << * s2 << endl;
return 0;
}作者: 我是哈密瓜耶 时间: 2009-11-3 01:16
er.... resize(), at() function and C++ style string. I have checked some documents and I found that I confused char pointer and char array with string pointer and string. Thx HJin.