本文共 7562 字,大约阅读时间需要 25 分钟。
stl标准模板库
In this article, we’ll take a look at using pair in C++ Standard Template Library (STL).
在本文中,我们将研究在C ++标准模板库( STL )中使用对 。
This is quite a useful container, which serves to reduce the pain of working with a single return type.
这是一个非常有用的容器,可减轻使用单个返回类型的麻烦。
Similar to a tuple in Python, the std::pair
template class is the C++ way of having multiple objects in one variable.
与Python中的元组类似, std::pair
模板类是在一个变量中具有多个对象的C ++方法。
Let’s look at how we can use this, use some illustrative examples!
让我们看看如何使用它,使用一些说明性示例!
This is in the std
namespace, so we need to prefix the namespace name before using it.
这在std
名称空间中,因此我们需要在使用名称空间名称之前添加前缀。
This is a template class, and can take templated arguments, based on the type.
这是一个模板类,并且可以根据类型采用模板化参数。
templatestruct pair;
To declare a pair variable called my_pair
, the syntax is as follows:
要声明一个名为my_pair
的对变量,语法如下:
std::pairmy_pair;
Here, T1
and T2
can be of any type, such as int
, char
, string
, etc.
在这里, T1
和T2
可以是任何类型,例如int
, char
, string
等。
Now that we have our pair variable declared, let’s now define it.
现在已经声明了pair变量,现在让我们对其进行定义。
A pair has two elements, called first and second.
一对具有两个元素,分别称为first和second 。
my_pair.first
. 要获取/设置第一个元素,请使用my_pair.first
。 my_pair.second
. 要获取/设置第二个元素,请使用my_pair.second
。 The elements must be of the appropriate type which conforms to T1
and T2
, in the declaration.
元素必须是符合声明中T1
和T2
的适当类型。
Let’s now assign the pair elements to specific values.
现在让我们将对元素分配给特定值。
We’ll construct a pair of std::pair<int, char>
, and assign it to 2 values accordingly.
我们将构造一对std::pair<int, char>
,并相应地将其分配给2个值。
#includeint main() { // Define my_pair std::pair my_pair; // Now assign the first element to an integer my_pair.first = 10; // And the second element to a character my_pair.second = 'H'; std::cout << "First element : " << my_pair.first << std::endl; std::cout << "Second element : " << my_pair.second << std::endl; return 0;}
Output
输出量
First element : 10Second element : H
As you can observe, we can easily manipulate the pair
elements!
如您所见,我们可以轻松地操纵pair
元素!
We can also directly initialize a pair variable, using it’s constructor!
我们还可以使用其构造函数直接初始化一个对变量!
Look at the below example, which directly constructs a pair.
看下面的例子,它直接构造了一对。
Here, I have used the auto
keyword, which is very useful for automatic type inference!
在这里,我使用了auto
关键字,这对于自动类型推断非常有用!
We don’t need to write the huge std::pair<>
again and again!
我们不需要一次又一次地编写巨大的std::pair<>
!
#include#include int main() { // Initialize a pair directly! auto my_pair = std::pair (1, "Hello"); std::cout << "First element : " << my_pair.first << std::endl; std::cout << "Second element : " << my_pair.second << std::endl; return 0;}
Output
输出量
First element : 1Second element : Hello
Indeed, we were able to directly initialize the pair variable.
确实,我们能够直接初始化对变量。
Another way of initializing a pair is to use the std::make_pair(T1 a, T2 b)
function.
初始化对的另一种方法是使用std::make_pair(T1 a, T2 b)
函数。
The advantage to this way is that we do have not to explicitly have to specify the types!
这种方式的优点是我们不必显式地指定类型!
This makes writing short and concise code more easier! Look at the same example above, now rewritten using std::make_pair()
.
这使得编写简短的代码更加容易! 看上面的相同示例,现在使用std::make_pair()
重写。
#include#include int main() { auto my_pair = std::make_pair(1, "Hello"); std::cout << "First element : " << my_pair.first << std::endl; std::cout << "Second element : " << my_pair.second << std::endl; return 0;}
I never once mentioned the type name here. auto
and make_pair()
did this work for us!
我从来没有在这里提到类型名称。 auto
和make_pair()
为我们完成了这项工作!
First element : 1Second element : Hello
Let’s now look at some other things that we can do with this container class!
现在让我们看一下此容器类可以做的其他事情!
We’ll look at how we can compare two std::pair
variables using logical operators.
我们将研究如何使用逻辑运算符比较两个std::pair
变量。
=
, the first value of the first pair is assigned to the first value of the second pair. (Same for second element) 如果我们要使用=
将一对分配给另一个对变量,则将第一对的第一个值分配给第二对的第一个值。 (与第二个元素相同) !=
operator compares the first and second elements, and returns True only if any one of them are not equal. !=
运算符比较第一个和第二个元素,并且仅当其中任何一个不相等时才返回True。 ==
operator also does a corresponding comparison. 同样, ==
运算符也进行相应的比较。 <=
and >=
operators first check the first two elements of both pairs, and return the comparison. If they are equal, the second elements are compared. <=
和>=
运算符首先检查两个对的前两个元素,然后返回比较。 如果它们相等,则比较第二个元素。 To illustrate all these operators, a simple example may be easy to visualize.
为了说明所有这些运算符,一个简单的示例可能很容易可视化。
#includeint main() { std::pair pair1 = make_pair(10, 12); std::pair pair2 = make_pair(10, 14); std::cout << (pair1 == pair2) << std::endl; std::cout << (pair1 != pair2) << std::endl; std::cout << (pair1 >= pair2) << std::endl; std::cout << (pair1 <= pair2) << std::endl; return 0; }
Output
输出量
0101
We can overload certain specific operators directly, on std::pair
.
我们可以在std::pair
上直接重载某些特定的运算符。
The below operators can be overloaded in C++20.
下面的运算符可以在C ++ 20中重载。
PLEASE NOTE: All the logical operators except ==
CANNOT be overloaded in C++20. This is a major change as compared to C++17.
请注意 : C = 20中 不能重载除==
之外的所有逻辑运算符。 与C ++ 17相比,这是一个重大更改。
Let’s take an example of overloading the logical equals operator (==
).
让我们以重载逻辑等于运算符( ==
)为例。
We’ll overload this to compare values of a pair. To be equal, both the first and second values must match.
我们将对此重载以比较一对值。 为了相等,第一个和第二个值必须匹配。
templatebool operator== (std::pair &p, std::pair &q) { if (p.first == q.first && p.second == q.second) { std::cout << "Equal\n"; return true; } std::cout << "Not Equal\n"; return false;}
The complete code is shown below:
完整的代码如下所示:
#include#include template bool operator== (std::pair &p, std::pair &q) { if (p.first == q.first && p.second == q.second) { std::cout << "Equal\n"; return true; } std::cout << "Not Equal\n"; return false;}int main() { auto p = std::make_pair(1, "Hello"); auto q = std::make_pair(1, "Hello"); if (p == q) { printf("True\n"); } else { printf("False\n"); } auto r = std::make_pair(1, "Hello"); auto s = std::make_pair(1, "JournalDev"); if (r == s) { printf("True\n"); } else { printf("False\n"); } return 0;}
Output
输出量
EqualTrueNot EqualFalse
As you can see, we have indeed overloaded the ==
operator to make this work for std::pair
as well!
如您所见,我们确实已经重载了==
运算符,以使其也可以用于std::pair
!
In this article, we learned how we could use the pair container class in C++ STL. We also saw how we could use different operators on two sets of pairs.
在本文中,我们学习了如何在C ++ STL中使用配对容器类。 我们还看到了如何在两组对上使用不同的运算符。
翻译自:
stl标准模板库
转载地址:http://fklzd.baihongyu.com/