为什么我会使用UTILITY而不是STRUCT?

时间:2012-12-17 18:22:10

标签: c++

  

可能重复:
  What is the difference between using a struct with two fields and a pair?

我一直在查看c ++快速参考应用程序并注意到UTILITY数据结构pair< TypeName, TypeName>将一对变量存储在我认为的结构中。我很困惑为什么这会被实现而不是仅仅使用一个简单的结构,有人可以启发我为什么对可能有用吗?

pair<string, int> a("hello",3); // A 2-element struct
a.first;  //"hello"
a.second; //3

我认为使用结构将是一种更好的做法,因为您可以回顾结构类型并创建该特定结构的对象,而如果需要多个副本,pair可能容易出现简单错误。

struct Data
{
    string str;
    int num;
}

Data pair1;
pair1.str = "hello";
pair1.num = 3;

0 个答案:

没有答案