如何将字符串(来自具有 n 行数的文件)存储在动态数组中? C++

Posted

技术标签:

【中文标题】如何将字符串(来自具有 n 行数的文件)存储在动态数组中? C++【英文标题】:How can I store a string(from a file with n number of lines) in a dynamic array? C++ 【发布时间】:2016-09-17 21:00:22 【问题描述】:

Newb 在这里...学习有关数据结构的 C++ 课程。我正在制作一个程序,它从文本文件中获取杂务列表并将它们存储在动态数组中。

//In header/ In class:

private:
/* var to keep len of list */
int len = 99; // Not sure what to set this to or if I need to even set it.
/* add appropriate data structure to store list */
string *arr = new string[len];

//In .cpp:

ListOfChores::ListOfChores(string fileName) 
ifstream file(fileName, ifstream::in);
string line;
    if (file.is_open()) //Checking if the file can be opened
    
        while (!file.eof()) // To get all the lines.
        
            getline(file, line); // Gets a single line
            arr[len] = line; // Store a line in the array
            len++; // Increases the array size by one
        
        file.close(); // Closes file
    
    else cout << "Unable to open file" << endl; // Gives error if the file can't be opened

但是在数组中存储一行时出现错误。它说“访问冲突读取位置”。在 main.cpp 中执行了另一个函数来打印行。

【问题讨论】:

离题:while (!file.eof()) // To get all the lines. 评论说谎。这将获取文件中的所有行加上不在文件中的行。有关此错误的更多信息:***.com/questions/5605125/… 【参考方案1】:

因为len 已经是 99,所以您立即超出了数组缓冲区。您应该有capacitylength 的概念。容量是在不重新分配的情况下可以存储的最大值,length 是实际的数据线数。

请避免在 C++ 代码中使用这种 C 样式的数组。使用vector,如果我没记错的话,它已经存在了至少 20 年 (STL)。

(你不是失败者,你已经在使用std::string :))

检查一下:

#include <vector>
//In header/ In class:

private:

/* add appropriate data structure to store list */
std::vector<string> arr;   // define a vector

//In .cpp:

ListOfChores::ListOfChores(string fileName) 
ifstream file(fileName, ifstream::in);
string line;
    if (file.is_open()) //Checking if the file can be opened
    
       while (getline(file, line))
       
           arr.push_back(line);
       
        file.close(); // Closes file
    
    else cout << "Unable to open file" << endl; // Gives error if the file can't be opened

现在arr.size() 保存行数,不再限制为99行而是最大。程序存储器容量。您仍然可以通过arr[12]arr.at(12) 访问第 13 行以进行边界检查访问。

遍历它的正确方法 (C++11) 例如打印所有行:

for (auto s : arr)

   std::cout << s << std::endl;

现在,如果您真的必须使用数组,您可以模拟/模仿 vector 所做的事情(好吧,我确定不是性能那么好,但确实可以):

private:

 int len=0;
 int capacity=100;
 string *arr = new string[capacity];

现在在代码中,就在插入之前(未经测试,但想法是正确的):

if (len>=capacity)

   string *narr = new string[capacity+100];
   for (int i = 0; i < capacity; i++)
   
        narr[i] = arr[i];
   
   delete [] arr;
   arr = narr;
   capacity += 100; // growth

(您不能使用reallocmemcpy,因为您正在处理数组中的对象)

【讨论】:

我很想用这个,但作业要求我们使用数组 T_T 顺便说一句,这比我想象的要糟糕。检查我的编辑(第一行)。 非常感谢。是的,我对此很陌生——我的编程教育很少。想解释一下我如何模拟向量的作用? 我们能否对从问题源复制的while (!file.eof()) 进行更正?在答案中留下这样的错误是没有意义的。 while (getline(file, line)) arr.push_back(line); 将尽一切可能感谢the magic of operator bool

以上是关于如何将字符串(来自具有 n 行数的文件)存储在动态数组中? C++的主要内容,如果未能解决你的问题,请参考以下文章

如何读取具有不同数字行数的文件

如何构建具有特定行数的表格视图?

如何在 UITableview 中创建具有 n 行数的 UITextField

删除具有任意行数的数据框中的最后 N 行

查找具有最大行数的索引

Swift:tableView 行的高度,其 tableView 单元格具有动态行数的嵌套 tableView