C++ 运算符

C++ 运算符,c++,arrays,C++,Arrays,我试图读取一个文件,该文件可以有任意数量的随机数字,但不会超过500,并将其放入一个数组中 稍后我将需要使用该数组进行一些操作 但是到目前为止,这段代码给了我一个与运算符不匹配的结果。您使用的是一个大小为0且步骤越界的索引,即使索引0也会越界。修复和评论: #include <fstream> #include <string> #include <iostream> // std::cout #include <vector> using n

我试图读取一个文件,该文件可以有任意数量的随机数字,但不会超过500,并将其放入一个数组中

稍后我将需要使用该数组进行一些操作

但是到目前为止,这段代码给了我一个与运算符不匹配的结果。您使用的是一个大小为0且步骤越界的索引,即使索引0也会越界。修复和评论:

#include <fstream>
#include <string>
#include <iostream>  // std::cout
#include <vector>

using namespace std; // not recommended

int main () {
    string line;
    ifstream myfile ("array_pgmdata.txt");
    //int index = 0;            // not needed
    //string myArray[index];    // UB - if it even compiles, it's a VLA of size 0.

    std::vector<std::string> myArray;    // use this instead to be able to grow it
                                         // dynamically

    if (myfile)                          // open and in a good state
    {
        // while (! myfile.eof() )       // It'll not be eof when you've read the last line
                                         // only when you try to read beyond the last line,
                                         // so you'll add "line" one extra time at the end
                                         // if you use that. Use this instead:
        while(getline(myfile, line))
        {
            // myArray[index++] << line; // you have 0 elements in the array and
                                         // can't add to it in any way
            myArray.push_back(line);
        }
    }
    else cout << "Unable to open file"; 

    // print what we got

    // classic way:
    for(size_t idx=0; idx < myArray.size(); ++idx) {
        std::cout << myArray[idx] << "\n";
    }

    // using a range-based for loop
    for(const std::string& s : myArray) {
        std::cout << s << "\n";
    } 

    // using a range-based for loop with auto
    for(const auto& s : myArray) {               // s is a std::string& here too
        std::cout << s << "\n";
    } 
}

除非您自己添加,否则它不存在。

myArray[index++]=line;还有:一会儿!myfile.eof是很多悲伤的原因。当你读完最后一行时,它不会是eof——只有当你试图读到最后一行之后,它才会是eof,所以你会在最后额外输入一行。Do:whilegetlinemyfile,改为第{行。我还建议使用向量并将_向后推以插入元素。还请注意,输入>可能值得一提的是,即使OP在数组中有足够的空间,他也可以使用字符串myArray[500];运算符@MartinBonnersupportsMonica确实如此。添加!我这样做了,现在它说cout没有声明。我在它前面放了std::但它仍然不起作用?@LeahLeviathan你必须包括查看我的编辑。谢谢!如果不是太多,我以后如何也可以cout字符串和做数学?还不太熟悉std
#include <fstream>
#include <string>
#include <iostream>  // std::cout
#include <vector>

using namespace std; // not recommended

int main () {
    string line;
    ifstream myfile ("array_pgmdata.txt");
    //int index = 0;            // not needed
    //string myArray[index];    // UB - if it even compiles, it's a VLA of size 0.

    std::vector<std::string> myArray;    // use this instead to be able to grow it
                                         // dynamically

    if (myfile)                          // open and in a good state
    {
        // while (! myfile.eof() )       // It'll not be eof when you've read the last line
                                         // only when you try to read beyond the last line,
                                         // so you'll add "line" one extra time at the end
                                         // if you use that. Use this instead:
        while(getline(myfile, line))
        {
            // myArray[index++] << line; // you have 0 elements in the array and
                                         // can't add to it in any way
            myArray.push_back(line);
        }
    }
    else cout << "Unable to open file"; 

    // print what we got

    // classic way:
    for(size_t idx=0; idx < myArray.size(); ++idx) {
        std::cout << myArray[idx] << "\n";
    }

    // using a range-based for loop
    for(const std::string& s : myArray) {
        std::cout << s << "\n";
    } 

    // using a range-based for loop with auto
    for(const auto& s : myArray) {               // s is a std::string& here too
        std::cout << s << "\n";
    } 
}
std::string& operator<<(std::string& dest, const std::string& src);