【问题标题】:How to Initialize Integers + String Arrays如何初始化整数 + 字符串数组
【发布时间】:2019-01-03 10:48:48
【问题描述】:

我是 C++ 新手,需要帮助!我目前正在学习如何制作二维数组或一维数组。

我有一个文本文件,其内容如下所示,我基本上是在尝试将它们存储到一个数组或更多取决于?如果我使用 Struct,或者基于下面文件中的内容,我只能拥有 1 个数组,或者 5 个单独的数组,是真的吗?

我相信 [1, 2] 可以组成一个二维数组,但是我该如何去实现呢?

下面是我使用结构体的代码,不知道我做对了吗?

请帮忙!

=========================================================
Sample Contents in 'HelloWorld.txt' File
=========================================================
[1, 2]-3-4-Hello_World
[5, 6]-7-8-World_Hello
[9, 1]-2-3-Welcome_Back
[4, 5]-6-7-World_Bye
[8, 9]-1-2-Bye_World

=========================================================
My Sample Codes
=========================================================
struct Example()
{
    fstream file;
    file.open("HelloWorld.txt");

    vector<string> abc; 
    string line;

    while(getline(file, line, '-'))
    {
        abc.push_back(line);

        int** a = new int*[abc.size()];
        for(int i = 0; i < abc.size(); i++)

        cout << abc[i] << endl;

        delete [] a;
    }
}

我的主要目标是能够将所有 4 个整数 + 1 个字符串存储到数组中,并学习如何使用 '[1, 2]' -> 前两个整数点创建一个二维数组。

等待建议!谢谢!

【问题讨论】:

  • C++ 没有二维数组。它有数组(有时是数组数组)
  • @Wilmort 是的,我创建了这个文件。它类似于我的讲义。测试它,所以我创建了这个示例文本文件。
  • @JustaGreenie 非常复杂,即使使用空格也可以编写这些,并且可以轻松解析它。但是如果你的讲座需要这个文件,那么你需要解析它。

标签: c++ arrays c++11 multidimensional-array delimiter


【解决方案1】:

首先,使用 c++ 特性和数据结构。您可以使用向量而不是数组,它更安全且易于使用。其次,您不必使用二维数组,您可以使用向量的一维数组。我们的数据结构是您的结构向量的向量,您可以读取文件并使用这些值创建一个结构,然后放入向量中。

#include <fstream>
#include <string>
#include <vector>

struct MyData
{
    MyData():
        num1(0),
        num2(0),
        num3(0),
        num4(0),
        str("")
    { }

    int num1;
    int num2;
    int num3;
    int num4;
    std::string str;
};


void ReadFile(std::vector<MyData>& myVec)
{
    std::string fileName = "HelloWorld.txt";
    std::ifstream file(fileName, std::ios::binary);

    if (file.fail())
    {
        std::cout << "Error while reading";
        perror(fileName.c_str());
    }

    std::string line;
    while (std::getline(file, line))
    {
        //parse this line
        // store suitable values to the struct
        // MyData myStruct;
        // myStruct.num1 = ...
        // myStruct.num2 = ...
        // myStruct.num3 = ...
        // myStruct.num4 = ...
        // myStruct.str = ...
        //myVec.push_back(myStruct);
        // or you can directly use emplace_back that is more efficent
        // but is more complicated for you now..
        // you can practice...
    }
}

int main()
{
    std::vector<MyData> myVector;
    ReadFile(myVector);
    return 0;
}

【讨论】:

  • 参考您的代码:MyData myStruct; myStruct.num1 = ... myStruct.num2 = ... //(这里通常放什么代码?是用来初始化的吗?)
  • 是的,使用文件中的值进行初始化。
  • 我已经成功地初始化了我的文件,以便它删除了所有的分隔符('-'、'['、']')。并使用 stringstream 从文件中的字符串行中读取整数。所以现在文件输出将是: 1 2 3 4 Hello_World 5 6 7 8 World_Hello 。 . .但是,我仍然无法将它存储在数组中!请给任何人建议!
  • 你现在可以.. 1 2 3 4 Hello_World 5 6 7 8 World_Hello 创建这样的文件。每行有 4 个整数和 1 个字符串。空格是分隔符。因此,您可以将前 4 项存储为整数,将最后一项存储为字符串..
猜你喜欢
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 2016-06-27
  • 1970-01-01
  • 2017-04-10
  • 2023-01-31
相关资源
最近更新 更多