string pointer的问题
想把一个string赋值到new string返回的地址空间
string s1="hello,world";
string s2=new string;
for(unsigned int i=0;i<=s1.length();i++) s2[i]=s1[i];
编译ok,但运行error,好像是指向了非法地址,这是什么道理?
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;
}