gpt4 book ai didi

c++ - 有没有办法重写一个返回指向 istringstream 的指针的函数,而不是返回一个引用?

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

这是我的有效代码:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

istringstream* get_line() {
string line;
getline(cin, line);
return new istringstream(line);
}

int main() {
istringstream* line = get_line();
int number_of_arrays, number_of_queries;
*line >> number_of_arrays >> number_of_queries;

int sum = number_of_arrays + number_of_queries;
vector<int> vectors[sum];

for (int i = 0; i < sum; ++i) {
line = get_line();
int n;
while (*line >> n) vectors[i].push_back(n);
delete line;
}

cout << endl;
for (int i = number_of_arrays; i < sum; ++i) {
int vector = vectors[i][0];
int index = vectors[i][1];
cout << vectors[vector][index] << endl;
}
return 0;
}

以下是我使用引用代替的一些尝试:

尝试 1:

istringstream& get_line() {
string line;
getline(cin, line);
return new istringstream(line);
}
...
istringstream& line = get_line();

错误:

x.cpp: In function ‘std::istringstream& get_line()’:
x.cpp:10:32: error: invalid initialization of non-const reference of type ‘std::istringstream& {aka std::__cxx11::basic_istringstream<char>&}’ from an rvalue of type ‘std::istringstream* {aka std::__cxx11::basic_istringstream<char>*}’
return new istringstream(line);
^
x.cpp: In function ‘int main()’:
x.cpp:23:12: error: type ‘std::istringstream {aka class std::__cxx11::basic_istringstream<char>}’ argument given to ‘delete’, expected pointer
delete line;

尝试 2:

const istringstream& get_line() {
string line;
getline(cin, line);
const istringstream* is = new istringstream(line);
return is;
}
...
istringstream& line = get_line();

错误:

x.cpp: In function ‘const istringstream& get_line()’:
x.cpp:11:10: error: invalid initialization of reference of type ‘const istringstream& {aka const std::__cxx11::basic_istringstream<char>&}’ from expression of type ‘const istringstream* {aka const std::__cxx11::basic_istringstream<char>*}’
return is;
^
x.cpp: In function ‘int main()’:
x.cpp:21:35: error: binding ‘const istringstream {aka const std::__cxx11::basic_istringstream<char>}’ to reference of type ‘std::istringstream& {aka std::__cxx11::basic_istringstream<char>&}’ discards qualifiers
istringstream& line = get_line();
^
x.cpp:24:12: error: type ‘std::istringstream {aka class std::__cxx11::basic_istringstream<char>}’ argument given to ‘delete’, expected pointer
delete line;
^

代码在做什么:

这是一个编码网站谜题的变体:

输入:

2 3//在这一行之后我将指定 2 个数组和 3 个查询

1 2 3 4 5//数组 0

6 7 8//数组 1

0 3//查询(返回数组 0 索引 3 处的元素)

1 1//查询(返回数组 1 索引 1 处的元素)

1 2//等等

输出:

4

7

8

最佳答案

按值返回,例子:

#include <iostream>
#include <sstream>

std::istringstream get_line() {
std::cout << "enter: ";
std::string line;
std::getline(std::cin, line);
std::istringstream rv(line);
// set the state of the stringstream to whatever state std::cin is in to
// be able to detect EOF or error conditions outside this function
rv.setstate(std::cin.rdstate());
return rv;
}

int main() {
std::istringstream ss;

while(ss = get_line()) { // loop while the state is good
std::string a;
while(ss >> a) { // extract
std::cout << a << "\n";
}
}
}

可以还可以取消引用从new 获得的指针以从您的函数返回一个引用——但是由于delete 的责任,这将完全令人困惑 stringstream 对于使用该函数的任何人来说都不是显而易见的。在这种情况下,需要delete &line;不要这样做:

#include <iostream>
#include <sstream>

std::istringstream& get_line() {
std::cout << "enter: ";
std::string line;
std::getline(std::cin, line);
std::istringstream* rv = new std::istringstream(line);
// set the state of the stringstream to whatever state std::cin is in
rv->setstate(std::cin.rdstate());
return *rv; // dereference
}

int main() {
std::istringstream& line = get_line();
std::string a;
while(line>> a) { // extract
std::cout << a << "\n";
}
delete &line;
}

关于c++ - 有没有办法重写一个返回指向 istringstream 的指针的函数,而不是返回一个引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54747841/

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