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

时间:2017-02-06 02:14:35

标签: c++ arrays vector struct

我的计划是存储几十行,每行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|

2 个答案:

答案 0 :(得分:1)

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

  1. 在main函数中声明struct并创建struct变量。

    struct movies_t { string title; int year; };

  2. 然后,在您的主要功能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..
      
    2. 修改后,您的代码应该有效。

      但是,为了更好的数据结构来保存配对值数组,您可以使用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";
          }
      }
      

答案 1 :(得分:1)

谢谢大家和@周。

上面的代码可能适用于较新版本的编译器,但我使用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";
    }
}