在其他函数中获取变量后创建数组

Creating array after getting variable in other function

文本文件如下所示:

3
String
String2
String3

我必须创建一个函数来从文本文件中读取所有字符串,将它们保存到数组中,然后在主函数中显示它。例如

void reading(int & count, string strings[]) {
ifstream fd "text.txt";
fd >> count;
for (int i=0; i<count; i++)
fd >> strings[i];
fd.close();

主要功能:

int main() {
int count=0;
string strings[100];
reading(count, strings);
for (int i=0; i<count; i++) 
cout << strings[i] << endl;

字符串数写在文本文件的第一行。如何创建一个恰好包含该数字的数组?我需要它才能在 main/other 函数中访问它。 (例如写入另一个文本文件的函数)。

在堆上分配一个数组,像这样:

std::string* stringArr = new std::string[(place amout of strings here)];

不要忘记在 main()

结束时删除
delete stringArr[];

堆上的变量/数组是动态的,因此大小不必固定!

std::string* → 这是一个指针,指向你内存中这个数组的开头地址。 (只是为了让你的电脑知道它在哪里)

stringArr → 数组名

new → 分配新内存

std::string[size] → 表示要分配多少

我看到了一些谈论 "vectors" 的答案。如果您想使用它们,可以查看此页面以获取更多信息! → Vector documentation

如果有一个非常重要的原因要用数组来做,那么使用std::new,像这样:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int reading(string** str_array, int& size,  string filename) {
    ifstream infile(filename.c_str());
    if(!infile) {
        cerr << "Unable to access file!\n";
        return -1;
    }
    int n = 0;
    infile >> size;
    try{
        *str_array = new string[size];
        string str;
        for (; n < size && infile; ++n) {
            infile >> str;
            (*str_array)[n] = str;
        }

        if (n != size)
            cerr << "ERROR, read less than " << size << " strings!!\n\n";
    } catch(bad_alloc& exc) {
        return -2;
    }
    return 0;
}


int main() {
    string* str_array = NULL;
    int size;
    if(reading(&str_array, size, "test.txt")) {
        cerr << "Din't read file, exiting...\n";
        return -1;
    }
    for(int i = 0; i < size; ++i)
        cout << str_array[i] << endl;

    delete [] str_array; // DO NOT FORGET TO FREE YOUR MEMORY
    str_array = NULL;

    return 0;
}

输出:

C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
String
String2
String3

然而,你在 and you are not using an std::vector 是为了这个?

看看它有多简单:

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

int reading(vector<string>& v, string filename) {
    ifstream infile(filename.c_str());
    if(!infile) {
        cerr << "Unable to access file!\n";
        return -1;
    }
    int N = -1, n = 0;
    infile >> N;
    string str;
    for (; n < N && infile; ++n) {
        infile >> str;
        v.push_back(str);
    }

    if (n != N)
        cerr << "ERROR, read less than " << N << " strings!!\n\n";
    return 0;
}

int main() {
    vector<string> v;
    if(reading(v, "test.txt")) {
        cerr << "Din't read file, exiting...\n";
        return -1;
    }
    for(size_t i = 0; i < v.size(); ++i)
        cout << v[i] << "\n";
    return 0;
}

输出:

C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
String
String2
String3

编辑:

我们必须传递一个指向我们要修改的指针(即string*),否则我们应用的更改将不会发生。自己测试一下,传递一个 string* 作为参数而不是 string**,修改函数体,她会发生什么。

为了理解这个想法,假设我们想要写入指针,新地址,new 给了我们并保存请求的内存。我们确实在函数内部写了那个地址,但是当函数终止时,我们希望这些变化是持久的。以我的 Functions in C 为例。

使用向量

#include <vector>
#include<fstream>
#include <iostream>
using namespace std;
void reading(int & count, vector<string> * strings) {
    ifstream fd("text.txt");
    fd >> count;
    string T;
    for (int i=0; i<count; i++)
    {
        fd >> T;
        strings->push_back(T);
    }
    fd.close();
}

int main() {
    int count=0;
    vector<string> strings;
    reading(count, &strings);
    for (int i=0; i<count; i++) 
        cout << strings[i] << endl;
 }