gpt4 book ai didi

c++ - 从文件读取到字符串并传递给函数

转载 作者:行者123 更新时间:2023-11-28 04:47:18 25 4
gpt4 key购买 nike

当我尝试传递包含 getline() 语句内容的字符串时,我在尝试理解为什么会向此部分抛出异常时遇到了一些问题。

`reference operator[](const size_type _Off)
{ // subscript mutable sequence
auto& _My_data = this->_Get_data();
_IDL_VERIFY(_Off <= _My_data._Mysize, "string subscript out of range");
return (_My_data._Myptr()[_Off]);
}`

void set_token(string n);
string token;

while (fin.peek() != '0' && !fin.eof())
{
getline(fin, token);
set_token(token);
}

`void set_token(string n)
{
string strarray[20];
string token;
int size = sizeof(n);
int i = 0;
int j = 0;
while (i < size)
{
if (n[i] != ' ' && n[i] != '\0')
{
token += n[i];
}
else
{
strarray[j] = token;
j++;
token.clear();
lexical(strarray[j]);
}
i++;
}`

最佳答案

我在 getline() 中没有看到任何问题,但在 set_token() 中,当您提取 n 的大小时,存在明显的运行时问题。 sizeof 不会给你字符串中的字符总数,但 size() 会。

试试这个: 大小 = n.size():

关于c++ - 从文件读取到字符串并传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com