如何在 C++ 中存储一对数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2164942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I store a pair of numbers in C++?
提问by Mithrax
I'm trying to learn C++ and right now I'm writing a program that needs to output a list of pairs of integers.
我正在尝试学习 C++,现在我正在编写一个需要输出整数对列表的程序。
What is the best way to handle this? I don't have the boost library available on our linux computers at school, so I don't believe I can use boost::tuple.
处理这个问题的最佳方法是什么?我在学校的 linux 计算机上没有可用的 boost 库,所以我不相信我可以使用 boost::tuple。
Any suggestions?
有什么建议?
回答by Mahmoud Al-Qudsi
Have a look at std::pair<object, object>
EDIT:
编辑:
It's standard C++ and part of what is known as the STL (Standard Template Library). It's a collection of nice data structures that are generic (i.e. may be used to store any C++ object type). This particular structure is used to store a "tuple" or a pair of numbers together. It's basically an object with members "first" and "second" that refer to the first and second objects (of any type!) that you store in them.
它是标准 C++ 和所谓的 STL(标准模板库)的一部分。它是一组很好的通用数据结构(即可用于存储任何 C++ 对象类型)。这种特殊的结构用于将一个“元组”或一对数字存储在一起。它基本上是一个具有成员“first”和“second”的对象,它们引用您存储在其中的第一个和第二个对象(任何类型!)。
So just declare an array of pair<int, int>
, or better yet, use another STL type called the "vector" to make a dynamically-sized list of pair<int, int>
: vector<pair<int, int> > myList
.
因此,只需声明一个数组pair<int, int>
,或者更好的是,使用另一种称为“向量”的 STL 类型来创建一个动态大小的pair<int, int>
:列表vector<pair<int, int> > myList
。
Hey what do you know! A dynamically-sized list of pairs already exists and it's called a map! Using it is as simple as #include <map>
and declaring a map<int, int> myMap
!!!
嘿,你知道什么!一个动态大小的对列表已经存在,它被称为地图!使用它就像#include <map>
声明一个map<int, int> myMap
!!!
EDIT:
编辑:
Yes, as pointed out, a map well "maps" one object to another, so you cannot have repeated lefthand-side values. If that's fine then a map is what you're looking for, otherwise stick to the vector of pair.... or take a look at multimaps.
是的,正如所指出的,地图很好地将一个对象“映射”到另一个对象,因此您不能有重复的左侧值。如果没问题,那么地图就是你要找的,否则坚持成对的向量......或看看多地图。
回答by Mithrax
Use std::pair?
使用 std::pair?
#include <utility>
#include <iostream>
int main() {
std::pair <int, int> p = std::make_pair( 1, 2 );
std::cout << p.first << " " << p.second << std::endl;
}
You can make a vector of pairs:
您可以制作成对的向量:
typedef std::pair <int, int> IntPair;
...
std::vector <IntPair> pairs;
pairs.push_back( std::make_pair( 1, 2 ) );
pairs.push_back( std::make_pair( 3, 4 ) );
回答by shura
While std::pair is the best approach to use, I'm surprised nobody mentioned pre-stl solution:
虽然 std::pair 是最好的使用方法,但我很惊讶没有人提到 pre-stl 解决方案:
struct Pair {
int first;
int second;
};
It is worrying that people think that they need boost for such a trivial problem.
令人担忧的是,人们认为他们需要为这样一个微不足道的问题进行推动。