如何保存未知大小的结构(供以后检索)
How to save unknown size structure (for later retrieval)
我的计划是存储几十行,每行 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|
您的代码中有一些不好的做法,如果您只是要求一种方法来修改程序以使其编译和工作,请参阅以下内容:
-
在主函数中声明结构并创建结构变量。
struct movies_t { string title; int year; };
然后,在您的主要功能中,movies_t myRecNo[2];
-
数组从索引 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";
}
}
相关文章:
- 如何在函数中检索指针结构
- 从C中的结构中检索值时编译误差
- 从保存变量数据类型的数据结构中检索值,而不指定返回类型
- 如何检索指向列表数据结构中的变量的指针?
- 如何保存未知大小的结构(供以后检索)
- 如何通过先前存储的ID从结构对象中检索值?C
- 结构"node"的迭代器不允许数据检索
- 高速缓存行检索和在大型数据结构上的性能
- 从 std::thread::id 结构中检索 Windows 线程标识符
- 将 char[] 类型化到结构中并检索结构的值
- 我应该使用哪些键值数据结构?按值快速检索和按键快速检索
- 从存储在 CTreeCtrl 的 LPARAM 中的结构中检索地址/指针 (IXMLDOMNode*) - 不起作用
- 检索数据结构对齐信息
- C++:用于高效插入和检索自定义数据的数据结构
- 从单独函数中的结构中检索数组成员
- 具有快速连续范围检索的数据结构
- 一种单一的数据结构,用于从脚本中读取任意类型的变量,以便在运行时进行检索和编辑
- 在MATLAB R2014a中使用engGetVariable检索结构体错误
- 存储和检索数据结构
- c++ STL中是否有数据结构可以对log(n)中的第k个元素进行插入、搜索和检索?