我有一个类的代码,我应该使用reduce()方法来查找数组中的最小值和最大值.但是,我们只需要使用一次调用来减少.返回数组的大小应为2,但我知道reduce()方法总是返回一个大小为1的数组.我可以使用下面的代码获取最小值,但是我不知道如何获取同一个电话中的最大值.我假设一旦我获得了最大值,我只需在reduce()方法完成后将其推送到数组.
/**
* Takes an array of numbers and returns an array of size 2,
* where the first element is the smallest element in items,
* and the second element is the largest element in items.
*
* Must do this by using a single call to reduce.
*
* For example, minMax([4, 1, 2, 7, 6]) returns [1, 7]
*/
function minMax(items) {
var minMaxArray = items.reduce(
(accumulator, currentValue) => {
return (accumulator < currentValue ? accumulator : …Run Code Online (Sandbox Code Playgroud) 基本上我的问题是我试图从一个充满数字和注释的 .txt 文件中读取数据并将每一行存储到一个字符串向量中,但是我的 getline 函数在第一个空格字符处停止读取,所以像 (*评论 *) 被分解成
str[0] = "(*";
str[1] = "comment";
str[2] = "*)";
Run Code Online (Sandbox Code Playgroud)
这是我的 getline 函数代码块的样子:
int main() {
string line;
string fileName;
cout << "Enter the name of the file to be read: ";
cin >> fileName;
ifstream inFile{fileName};
istream_iterator<string> infile_begin {inFile};
istream_iterator<string> eof{};
vector<string> data {infile_begin, eof};
while (getline(inFile, line))
{
data.push_back(line);
}
Run Code Online (Sandbox Code Playgroud)
这就是 .txt 文件的样子:
101481
10974
1013
(* comment *) 0
28292
35040
35372
0000
7155
7284
96110
26175
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它没有阅读整行。