C++ 错误:‘;结果’;不命名类型

C++ 错误:‘;结果’;不命名类型,c++,vector,C++,Vector,我想在我的数据集中找到最小值 #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> int main(){ std::vector<double> v; std::ifstream inputFile1("263_V01_C02_R099_THx_BL_128H.dat");

我想在我的数据集中找到最小值

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> 
#include <iterator>  


int main(){
std::vector<double> v;
std::ifstream inputFile1("263_V01_C02_R099_THx_BL_128H.dat");

if (inputFile1) {        
    double value;
    while ( inputFile1 >> value ) {
        v.push_back(value);
    }
}

auto result = std::min_element(std::begin(v), std::end(v));

}
#包括
#包括
#包括
#包括
#包括
int main(){
std::向量v;
标准::ifstream输入文件1(“263_V01_C02_R099_THx_BL_128H.dat”);
如果(inputFile1){
双重价值;
while(inputFile1>>值){
v、 推回(值);
}
}
自动结果=标准::最小元素(标准::开始(v),标准::结束(v));
}

我已经看到了以前的回答,人们指出迭代器没有被包括在内。如何解决这个问题?

auto
是一个C++11说明符。你需要一个C++11编译器

使用gcc可以使用
-std=C++11
启用C++11功能:

gcc示例:

g++ -std=c++11 main.cpp

自动说明符(从C++11开始) 指定将从其初始值设定项自动推断要声明的变量类型

如果没有C++11编译器,则需要定义具体类型。 例如:

std::vector::迭代器结果=。。。

顺便说一句:
std::begin()
std::end()
也是C++11的特性。

auto
是一个C++11说明符。你需要一个C++11编译器

使用gcc可以使用
-std=C++11
启用C++11功能:

gcc示例:

g++ -std=c++11 main.cpp

自动说明符(从C++11开始) 指定将从其初始值设定项自动推断要声明的变量类型

如果没有C++11编译器,则需要定义具体类型。 例如:

std::vector::迭代器结果=。。。

顺便说一句:
std::begin()
std::end()
也是C++11的特性。

正如sergej已经指出的那样,
auto
是C++11的说明符/关键字

如果您没有访问C++11编译器的权限,您仍然可以使用它使代码正常工作

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> 
#include <iterator>  


int main() {
    std::vector<double> v;
    std::ifstream inputFile1("263_V01_C02_R099_THx_BL_128H.dat");

    if (inputFile1) {
        double value;
        while (inputFile1 >> value) {
            v.push_back(value);
        }
    }

    double result = 0; //Or other default value
    std::vector<double>::iterator minIter = std::min_element(v.begin(), v.end());
    if (minIter != v.end())
    {
        result = (*minIter);
    }
    std::cout << "Result is " << result << std::endl;

}
#包括
#包括
#包括
#包括
#包括
int main(){
std::向量v;
标准::ifstream输入文件1(“263_V01_C02_R099_THx_BL_128H.dat”);
如果(输入文件1){
双重价值;
while(inputFile1>>值){
v、 推回(值);
}
}
double result=0;//或其他默认值
std::vector::iterator minIter=std::min_元素(v.begin(),v.end());
如果(minIter!=v.end())
{
结果=(*minIter);
}

正如sergej已经指出的那样,
auto
是一个C++11说明符/关键字

如果您没有访问C++11编译器的权限,您仍然可以使用它使代码正常工作

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> 
#include <iterator>  


int main() {
    std::vector<double> v;
    std::ifstream inputFile1("263_V01_C02_R099_THx_BL_128H.dat");

    if (inputFile1) {
        double value;
        while (inputFile1 >> value) {
            v.push_back(value);
        }
    }

    double result = 0; //Or other default value
    std::vector<double>::iterator minIter = std::min_element(v.begin(), v.end());
    if (minIter != v.end())
    {
        result = (*minIter);
    }
    std::cout << "Result is " << result << std::endl;

}
#包括
#包括
#包括
#包括
#包括
int main(){
std::向量v;
标准::ifstream输入文件1(“263_V01_C02_R099_THx_BL_128H.dat”);
如果(输入文件1){
双重价值;
while(inputFile1>>值){
v、 推回(值);
}
}
double result=0;//或其他默认值
std::vector::iterator minIter=std::min_元素(v.begin(),v.end());
如果(minIter!=v.end())
{
结果=(*minIter);
}

std::cout您如何知道GCC正在使用?它的版本足以支持带有此标志的
自动
?@LightnessRacesinOrbit是的,我正在使用GCC进行编译,现在工作正常!@RichardRublev:请在您的问题中包含这一关键细节。@LightnessRacesinOrbit GCC只是一个例子。您如何知道GCC正在使用?以及它的版本足以支持带有此标志的
auto
。@LightnessRacesinOrbit是的,我正在使用gcc编译,现在工作正常了!@RichardRublev:请在您的问题中包含这一关键细节。@LightnessRacesinOrbit gcc只是一个例子。它与包含
完全无关。您使用的是哪种编译器ng?@GCC现在运行良好,谢谢!答案不在问题中。这与包含
无关。您正在使用哪个编译器?@GCC现在运行良好,谢谢!答案不在问题中。
std::begin()
std::end()
是C++11函数
std::begin()
std::end())
是C++11函数吗