【发布时间】:2020-03-28 05:06:25
【问题描述】:
为什么即使在运行循环以从 s 中复制字符之后,临时字符串的大小仍为 0?我得到 0 作为临时字符串的大小。
#include <iostream>
#include<string>
using namespace std;
string CopyString(string s,int delta,int len){
string temp;
int i;
for (i = 0; i < len; i++)
{
temp[i] = s[i + delta];
}
cout<<temp.size();
return temp;
}
int main() {
string s = "Hello";
cout<<CopyString(s,2,5);
}
【问题讨论】:
-
std::string默认为空。索引运算符[]不创建任何字符,它只能访问现有字符。 -
好的,非常感谢!