2种容器

发布于 2024-11-06 06:32:53 字数 134 浏览 4 评论 0 原文

是否有任何 C++ 容器可以存储 2 种(或更多)类型的值,例如 int 和 char?我想做一个二十一点游戏。牌组必须由整数和字符组成。我不想只用数字来初始化它(所以不要对此说什么!)。我是一个非常的初学者程序员,所以不要让它太复杂。

Is there any C++ container that can store 2 (or more) types of values, such as ints and chars? I want to make a blackjack game. The deck has to consist of both ints and chars. I don't want to initialize it with just numbers (so don't say anything about that!). I am a very beginner programmer, so don't make it too complicated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(6

行至春深 2024-11-13 06:32:53

我假设您需要一个能够存储整数或字符的容器。

首先,看一下 boost::any 数据类型="nofollow">Boost,这可能会有所帮助。然后,您可以创建一个包含 boost::any 实例的容器。

如果您不想使用 boost 或者它看起来有些过分,请使用 union 如下:

typedef struct {
    char type;
    union {
        char character;
        int integer;
    };
} my_struct;

character 的内容然后联合体中的>integer字段占用相同的内存槽。 (嗯,整数使用更多的槽,因为字符通常只有一个字节)。然后,您可以将结构体的 type 字段设置为,例如,'c'(如果您存储字符),并设置为,例如,'i ' 来存储整数,然后根据 type 的值使用 characterinteger 字段访问结构体的内容代码>.

最后,还有 Qt 的 QVariant 数据类型,其工作原理与上述第二种方法类似。

I'm assuming that you need a container which is able to store either ints or chars.

First, take a look at the boost::any datatype in Boost, that might help. You can then create a container of boost::any instances.

If you don't want to use boost or it seems overkill, use a union as follows:

typedef struct {
    char type;
    union {
        char character;
        int integer;
    };
} my_struct;

The contents of the character and the integer field in the union then occupy the same memory slots. (Well, the integer uses more slots since chars are usually only one byte). It is then up to you to set the type field of the struct to, say, 'c' if you store a character and to, say, 'i' to store an integer, and then access the contents of the struct using the character or the integer field depending on the value of type.

Finally, there's also the QVariant datatype of Qt, which works similarly to the second approach described above.

窗影残 2024-11-13 06:32:53

您也可以使用 Boost.Variant

Also you can use Boost.Variant

是伱的 2024-11-13 06:32:53

由于您是初学者,因此只需使用基本的东西:结构。

#include <vector>
#include <iostream>

struct MyStruct{
  char cval;
  int ival;
};

int main(){
  using namespace std;

  vector<MyStruct> myvec;
  MyStruct values;

  values.cval = 'S';
  values.ival = 42;
  myvec.push_back(values);

  values.cval = 'A';
  values.ival = 1337;
  myvec.push_back(values);

  values.cval = 'X';
  values.ival = 314159;
  myvec.push_back(values);

  for(int i=0; i < myvec.size(); ++i)
    cout << "myvec[" << i << "], cval: " << myvec[i].cval << ", ival: " << myvec[i].ival << "\n";
}

输出:

myvec[0], cval: S, ival: 42  
myvec[1], cval: A, ival: 1337  
myvec[2], cval: X, ival: 314159  

您可以在 Ideone 上实时查看输出。

Since you're a beginner, just use the basic stuff: a struct.

#include <vector>
#include <iostream>

struct MyStruct{
  char cval;
  int ival;
};

int main(){
  using namespace std;

  vector<MyStruct> myvec;
  MyStruct values;

  values.cval = 'S';
  values.ival = 42;
  myvec.push_back(values);

  values.cval = 'A';
  values.ival = 1337;
  myvec.push_back(values);

  values.cval = 'X';
  values.ival = 314159;
  myvec.push_back(values);

  for(int i=0; i < myvec.size(); ++i)
    cout << "myvec[" << i << "], cval: " << myvec[i].cval << ", ival: " << myvec[i].ival << "\n";
}

Output:

myvec[0], cval: S, ival: 42  
myvec[1], cval: A, ival: 1337  
myvec[2], cval: X, ival: 314159  

You can see the output live on Ideone.

嘦怹 2024-11-13 06:32:53

如果你想同时使用所有的值,你可以使用这样的东西......

std::pair<int,char> twovals;
std::pair<int,std::pair<char,float> > threevals;

If you want to use all of the values simultaneously, you can use something like this...

std::pair<int,char> twovals;
std::pair<int,std::pair<char,float> > threevals;
旧时浪漫 2024-11-13 06:32:53

使用 struct、class 或 std::pair 将不同类型分组为复合类型,然后使用适当的 STL 容器。

Use a struct, class, or std::pair to group the different types into a composite type and then use the appropriate STL container.

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