c++ - 从文本文件中读取数字,并检查它们是否在范围内

标签 c++ vector

我正在尝试加载包含 1000 个数字或类似内容的文本文件,我想检查每个数字是否在范围内。我尝试使用 vector ,但无法直接使用。

所以基本上我想检查第一个数字是否大于或 = 到下一个,并多次执行此操作。

ps:我对任何类型的编程都非常陌生,所以请不要刻薄:)

这是我的代码:

using namespace std;

int main() {
    ifstream myfile;
    myfile.open ("A1.txt");

    vector<int>array;
    int count;
    double tmp;
    int i;

    myfile >> tmp;
    while (int i = 0; i < count; i++){
        myfile >> tmp;
        array.push_back(tmp);
        cout << count;
    }

    myfile.close();
    return 0;
}

最佳答案

我会建议一个循环,直到你读完每个数字:

...
double previous, tmp;
myfile >> previous;
array.push_back(previous);//this inserts the first element
//but you have to decide if you need it or not, because it is not compared with any value
while (myfile >> tmp){
    array.push_back(tmp);
    if(previous >= tmp){
        //Do something;
    }
    previous = tmp;//the next previous is the current tmp
}
...

关于c++ - 从文本文件中读取数字,并检查它们是否在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40866178/

相关文章:

c++ - 表达式必须有类类型(学生数据库)?

c++ - 学习C++,从一本书中寻找关于这个项目的说明

c++ - 我可以在浏览器呈现之前访问 Flash 数据吗?

c++ - 该程序不会读取 C++ 中的第二个 vector

C++ - 传入一个 vector 并返回一个 vector

c++ - 使用 _crtBreakAlloc、_CRTDBG_MAP_ALLOC 追踪插件中的内存泄漏

java - 使用 JNI : Is this an issue with my code, 从 Java 调用 native (C++) 函数时发生访问冲突或已知问题?

c++ - 使用 OpenGL 或 D3D 绘制椭圆的有效方法

c++ - 在类中访问 vector

c++ - 在 C++ 中对这些独立但相关的序列进行排序的简洁方法是什么?