如何在c++中存储和引用大量数据

发布于 2024-12-12 04:10:45 字数 565 浏览 0 评论 0原文

我正在使用字符串向量来在内存中存储一​​些数据。数据库不是一个选择。更准确地说,是字符串向量数组。一个简单的场景:我需要存储居住在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧时模样 2024-12-19 04:10:46

我不会说指针更好是显而易见的。是的,您可以将 shared_ptr 存储在集合中,但根据您访问数据的方式,您可能也更喜欢使用 listmap 来代替。

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 use list or map instead.

这个俗人 2024-12-19 04:10:45

如果您有 C++11 并且只需要该数据的单个全局版本,则可以使用如下数据结构:

#include <unordered_map>
#include <unordered_set>
#include <string>

typedef std::unordered_multiset<std::string> name_set;
typedef std::unordered_map<std::string, name_set> city_map;

city_map city_db {
  { "Moscow", { "Ivan", "Igor", "Vladimir" } },
  { "Madrid", { "Julio", "Pedro", "Sanchez" } },
  { "Munich", { "Sepp", "Huber", "Maier" } }
};

int main()
{
  return city_db["Munich"].size(); // just as an example
}

If you have C++11 and only need a single, global version of this data, you could use a data structure like this:

#include <unordered_map>
#include <unordered_set>
#include <string>

typedef std::unordered_multiset<std::string> name_set;
typedef std::unordered_map<std::string, name_set> city_map;

city_map city_db {
  { "Moscow", { "Ivan", "Igor", "Vladimir" } },
  { "Madrid", { "Julio", "Pedro", "Sanchez" } },
  { "Munich", { "Sepp", "Huber", "Maier" } }
};

int main()
{
  return city_db["Munich"].size(); // just as an example
}
南城旧梦 2024-12-19 04:10:45

城市、人……是时候考虑数据库了。

Cities, persons.. time to consider a database.

≈。彩虹 2024-12-19 04:10:45

或者使用向量的向量?或者向量列表?

Or use a vector of vectors? or a list of vectors?

橪书 2024-12-19 04:10:45

您可以使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文