【问题标题】:Dynamically allocate memory for vectors为向量动态分配内存
【发布时间】:2014-06-16 07:49:43
【问题描述】:

我正在尝试用向量替换数组,但我不知道怎么做。

替换此函数为向量动态分配内存:

string readFile(string filename, string** list, int size){
    *list = new string[size];
    ifstream file(filename);
    string line;
    for (int i = 0; i < size; i++){
        getline(file, line);
        *(*list + i) = line;
    }

    file.close();
    return **list;
}

这是我尝试将其更改为矢量但没有运气的尝试。非常感谢任何反馈:

string processFile(string filename, vector<string>** list, int size){
        *list = new vector<string>(size);
        ifstream file(filename);
        string line;
        for (int i = 0; i < size; i++){
            getline(file, line);
            *list[i] = line; // error
        }

        file.close();
        return **list; // error
    }

【问题讨论】:

  • 只需放下指针和动态分配。使用std::vector 等容器的原因之一是您不必做那种事情。
  • 感谢您的反馈,但是我们的老师要求我们使用vector new。
  • 天哪。好吧,我希望他们有充分的理由,但我对此表示严重怀疑。顺便说一句,您的代码还有另一个问题:您循环了一定数量的行而不检查文件中是否还有要读取的行。

标签: c++ vector dynamic-memory-allocation


【解决方案1】:

您将需要一些适当的错误处理,但基本上,如果您使用容器,则不需要指针或固定大小:

std::vector<std::string> readLinesFromFile(const std::string& filename)
{
    std::vector<std::string> result;
    std::ifstream file(filename);

     for (std::string line; std::getline(file, line); )
     {
        result.push_back(line);
     }

     return result;
}

【讨论】:

  • +1,但我怀疑他们需要该函数来读取用户指定的给定行数,而不是整个文件。
【解决方案2】:

有几个问题:

  1. 你不需要使用vector**,vector相当于前面代码中的列表。
  2. 返回类型是字符串,但是你返回的是向量**

此代码应该可以工作,但未经测试:

    void processFile(string filename, vector<string>& list, int size){
    //list = new vector<string>(size); // no need if you get a vector reference
    ifstream file(filename);
    string line;
    for (int i = 0; i < size; i++){
        getline(file, line);
        list.push_back(line); //the error was because you are assigning string to a vector<string>*
    }

    file.close();
    // you dont have to return, as vector is passed by reference
}

如果还需要使用vector的指针

    void processFile(string filename, vector<string>** list, int size){
    *list = new vector<string>(size); // bad practice
    ifstream file(filename);
    string line;
    for (int i = 0; i < size; i++){
        getline(file, line);
        (*list)->push_back(line); 
    }

    file.close();
    // you dont have to return,  as vector is passed by pointer
   }

【讨论】:

  • 第二个代码sn-p中,list是本地的,所以被调用看不到任何效果。此外,无需致电file.close()
  • @RakibulHasan 不,你错过了&amp;。这是 C++ :-)
【解决方案3】:

将 *list[i] = line 更改为 *list->push_back(line),第一个错误应该没问题。

第二个错误将取决于您对返回值的意图。我认为 return *list->front();将给出与第一个示例相同的结果,但如果您计划返回的不仅仅是第一行,那么您将需要进行一些连接。您可以创建一个本地字符串并在阅读时附加每一行。

希望你的老师知道在 C++ 中使用新向量几乎总是一种代码味道,并且出于特定原因使用它并计划稍后修复它。

【讨论】:

    【解决方案4】:

    这是一个工作示例。请享用 :) 顺便说一句 - 您不需要传递长度,只需实例化内存并使用 push_back 方法。

    #include <vector>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void processFile(string filename, vector<string>** list, int size);
    void main()
    {
        vector<string>* list = NULL;
        processFile("C:\\temp.txt", &list, 13);
        int i = 1;
    }
    
    void processFile(string filename, vector<string>** list, int size){
        *list = new vector<string>();
        ifstream file(filename);
        string line;
        for (int i = 0; i < size; i++){
            getline(file, line);
            (**list).push_back(line); // error
        }
    
        file.close();
    }
    

    【讨论】:

    • 感谢您的所有投入,非常感谢。
    猜你喜欢
    • 2015-05-17
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2016-05-05
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多