gpt4 book ai didi

c++ - 为什么 std::istringstream 只能在此处与构造函数一起使用?

转载 作者:行者123 更新时间:2023-12-02 02:07:59 25 4
gpt4 key购买 nike

下面的代码可以按预期编译并运行。在第 11 行,当我改变时
std::istringstream iss(temp)

std::istringstream iss()
,它停止工作。第 18 行 iss.str(temp) 发生错误:
expression must have class type but it has type "std::istringstream (*)()"

为什么对构造函数的更改会产生影响?我检查了docs ,这应该没有什么区别,但我一定错过了一些东西。有什么想法吗?

#include <vector>
#include <iostream>
#include <string>
#include <sstream>

int main() {

int n, m;
std::string temp;
std::vector<int> ranked, player;
std::istringstream iss();

// Get the Input

std::getline(std::cin, temp);
n = std::stoi(temp);
std::getline(std::cin, temp);
iss.str(temp);
while(!iss.eof()) {
std::getline(iss, temp, ' ');
ranked.push_back(std::stoi(temp));
}

return 0;
}

最佳答案

std::istringstream iss();

是函数iss的声明。删除 () 来声明一个对象。

另请注意,您对 while(!iss.eof()) 的使用是 wrong 。循环应该是:

while(std::getline(iss, temp, ' ')) {
ranked.push_back(std::stoi(temp));
}

关于c++ - 为什么 std::istringstream 只能在此处与构造函数一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68132615/

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