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

Creating array after getting variable in other function

本文关键字:变量 创建 数组 获取 其他 函数      更新时间:2023-10-16

文本文件如下所示:

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;

字符串的数目写入文本文件的第一行。如何创建一个恰好包含该数字的数组?我需要它能够在主要/其他功能中访问它。(例如,用于写入另一个文本文件的函数)。

如果有一个非常重要的原因需要对数组执行此操作,那么请使用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!!nn";
    } 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

然而,您使用的是c++,而不是使用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!!nn";
    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给我们的新地址,并保存所请求的内存。我们确实在函数中写入了该地址,但当函数终止时,我们希望更改是持久的。请参阅我在C中的函数作为示例。

在堆上分配一个数组,如下所示:

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

并且不要忘记在main()的末尾删除

delete stringArr[];

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

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

stringArr→阵列的名称

new→分配新内存

std::string[size]→说明分配的金额

我看到了一些关于"矢量"的答案。如果你想使用它们,你可以看看这个页面了解更多信息!→矢量文档

使用矢量

#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;
 }