gpt4 book ai didi

c++ - token 之前的预期初始值设定项

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

当我尝试像这样为 vector 构建 forloop 时:

  vector<int> v;

int a;
while (cin>>a){
if(i<=0 || i>=10)
cout << "Please enter int between 0 and 10:" << endl;
else
v.push_back(a);
}

int min;
int sum;
for (int &i: v){
if(v.empty())
min = i;
else(i<min){
min = i;
sum += i;
}
}

它在 â token 之前显示:expected initializer

我哪里错了?非常感谢!

.bashrc:

这是我的.bashrc:

  # .bashrc

# User specific aliases and functions
export PATH=/usr/remote/gcc-4.8/bin:$PATH
export LD_LIBRARY_PATH=/usr/remote/gcc-4.8/lib:$LD_LIBRARY_PATH
alias g++='g++ -std=c++11'


# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fio9

最佳答案

您似乎没有在将 min 用于 else if 条件之前对其进行初始化。

但是,您只是有一个带有语法错误条件的 else。

这对我有用:

#include <vector>
#include <iostream>
#include <limits.h>

using namespace std;

int main()
{
vector<int> v;

int i;
while (cin >> i) {
if(i < 0 || i > 10)
cout << "Please enter int between 0 and 10:" << endl;
else
v.push_back(i);
}

int sum = 0;
int min = INT_MAX;

for (int &i: v) {
if (i < min)
min = i;
sum += i;
}

cout << "min: " << min << endl;
cout << "sum: " << sum << endl;
}

我使用以下命令构建此代码:g++ -std=c++11 main.cpp

输出是:

1
2
3
min: 1
sum: 6

关于c++ - token 之前的预期初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777005/

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