如何在c++中存储和引用大量数据
我正在使用字符串向量来在内存中存储一些数据。数据库不是一个选择。更准确地说,是字符串向量数组。一个简单的场景:我需要存储居住在 256 个城市的人的姓名。
示例
NewYork: John, Bod, ...
London: Jim, Bill...
我使用的此要求的
vector<std::string> city[256];
一个新要求是创建一个新的“人员类”,该类将在每个项目中保存更多数据。
class person {
string name;
string surname;
string email;
int age;
};
我正在解决此问题,以便找到存储和共享这些数据的最佳方式。
vector<class person> city[256];
显然,使用指向对象的指针更好。 Shared_ptr 在这里适用吗?我们有TR1 安装在系统中,但我们无法使用 boost 库。
I am using a vector of strings in order to store some data in memory. Database is not an option. More precisely an array of vector of strings. A simple scenario: I need to store the names of people living in 256 cities.
Example
NewYork: John, Bod, ...
London: Jim, Bill...
for this requirement I used
vector<std::string> city[256];
A new requirement came to create a new "class Person" that will hold more data per item
class person {
string name;
string surname;
string email;
int age;
};
I am addressing this issue in order to find the optimal way to store and shared those data.
vector<class person> city[256];
Obviously is better to use a pointer to objects. does shared_ptr applies here? We have TR1
installed in the system but we cannot use boost libs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不会说指针更好是显而易见的。是的,您可以将
shared_ptr
存储在集合中,但根据您访问数据的方式,您可能也更喜欢使用list
或map
来代替。I wouldn't say it's so obvious that pointers are better. Yes, you can store
shared_ptr
in collection, though depending on how you access the data you may also prefer to uselist
ormap
instead.如果您有 C++11 并且只需要该数据的单个全局版本,则可以使用如下数据结构:
If you have C++11 and only need a single, global version of this data, you could use a data structure like this:
城市、人……是时候考虑数据库了。
Cities, persons.. time to consider a database.
或者使用向量的向量?或者向量列表?
Or use a vector of vectors? or a list of vectors?
您可以使用shared_ptr或本机指针,或存储为值。这取决于。您想要最快的查找速度吗?你想节省内存吗?您想要连续内存中的数据吗?你想支持线程吗?没有普遍的正确答案。
256 个对象并不是一个巨大的数据量。
You can use shared_ptr, or a native pointer, or store as value. It depends. Do you want the quickest look ups? Do you want to save on memory? Do you want data in contiguous memory? Do you want to support threading? There is no general correct answer.
256 objects is not a huge amount of data.