新微赢技术网

标题: string pointer的问题 [打印本页]

作者: 黒設會    时间: 2009-11-3 01:16
标题: 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,好像是指向了非法地址,这是什么道理?
作者: 死了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.




欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2