如何保存未知大小的结构(供以后检索)

How to save unknown size structure (for later retrieval)

本文关键字:结构 检索 何保存 保存 未知      更新时间:2023-10-16

我的计划是存储几十行,每行 2 个项目,两个项目都有不同的数据类型。不确定这是否是正确的方法,并且听说过使用向量,但我找不到任何样本可以接受 2 个不同类型的项目,其中有许多行(未知数量的行(,类似于我在这里尝试做的事情。以下内容无法编译

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
  string title;
  int year;
} myRecNo[];
void printmovie (movies_t movie);
int main ()
{
  string mystr;
  for (int i=0; i < 2; i++)
  {
    switch (i)
    {
    case 1:
      myRecNo[i].title = "2001 A Space Odyssey";
      myRecNo[i].year = 1968;
      cout << "Auto entry is:n ";
      printmovie (myRecNo[i]);
      break;
    case 2:
      myRecNo[i].title = "2002 A Space Odyssey";
      myRecNo[i].year = 1978;
      cout << "Auto entry is:n ";
      printmovie (myRecNo[i]);
      break;
    }
  }
  return 0;
}
void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")n";
}

这是我得到的错误:

Test1.obj||error LNK2019: unresolved external symbol "struct movies_t * myRecNo" (?myRecNo@@3PAUmovies_t@@A) referenced in function _main|

您的代码中有一些不好的做法,如果您只是要求一种方法来修改程序以使其编译和工作,请参阅以下内容:

  1. 在主函数中声明结构并创建结构变量。

    struct movies_t { string title; int year; };

然后,在您的主要功能中,movies_t myRecNo[2];

  1. 数组从索引 0 开始,而不是从 1 开始。所以你的开关应该是

    switch (i)
    {
    case 0:
        myRecNo[i].title = "2001 A Space Odyssey";
        myRecNo[i].year = 1968;
        cout << "Auto entry is:n ";
        printmovie(myRecNo[i]);
        break;
    case 1:
        myRecNo[i].title = "2002 A Space Odyssey";
        myRecNo[i].year = 1978;
        cout << "Auto entry is:n ";
        printmovie(myRecNo[i]);
        break;
    }
    // the rest of the code..
    

修改这些内容后,代码应该可以工作。

<小时 />

但是,为了获得更好的数据结构来保存配对值数组,可以使用std::vector<std::pair<string, int>> myReg来保存数据。

下面的代码应该好得多,记得#include <vector>

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
void printmovie(std::vector<std::pair<std::string, int>>);
int main()
{
    std::vector<std::pair<std::string, int>> myReg;
    myReg.push_back({ "2001 A Space Odyssey", 1968 });
    myReg.push_back({ "2002 A Space Odyssey", 1978 }); // <- if your compiler is not using c++11 standard or above, please change this line to myReg.push_back(std::pair<std::string, int>("name of the movie", int)); to use to older version of Initializer 
    printmovie(myReg);
    return 0;
}
void printmovie(std::vector<std::pair<std::string, int>> movie)
{
    for (auto itor = movie.begin(); itor != movie.end(); ++itor)
    {
        //first is the first data in the pair, which is the title
        //second is the second data in the pair, which is the year
        std::cout << (*itor).first  << " (" << (*itor).second << ")n";
    }
}

谢谢大家和@Zhou。

Zhou上面的代码可能适用于较新版本的编译器,但我使用的是Code::Blocks IDE和MS Visual C++ 2010编译器。

这是有效的矢量方法:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
void printmovie(std::vector<std::pair<std::string, int>>);
int main()
{
    std::vector<std::pair<std::string, int>> myReg;
    myReg.push_back(std::pair<std::string, int>("title of the movie", 1968));
    myReg.push_back(std::pair<std::string, int>("title of the movie2", 1978));
    //myReg.push_back({ "2001 A Space Odyssey", 1968 });
    //myReg.push_back({ "2002 A Space Odyssey", 1978 });
    printmovie(myReg);
    //or to print a single element (the 2nd row) thanks @zhou
    std::cout << myReg[1].first << " " << myReg[1].second << std::endl;
    return 0;
}
void printmovie(std::vector<std::pair<std::string, int>> movie)
{
    for (auto itor = movie.begin(); itor != movie.end(); ++itor)
    {
        //first is the first data in the pair, which is the title
        //second is the second data in the pair, which is the year
        std::cout << (*itor).first  << " (" << (*itor).second << ")n";
    }
}