请解释这个使用std :: ignore的代码 [英] Please explain this code that uses std::ignore
问题描述
我正在阅读有关 std :: ignore
从cppreference。我发现很难理解这个对象的真正目的,而且示例代码并没有这么正义。例如,在下面的代码中,如何和为什么插入
设置为true?对我来说没有什么意义。
#include< iostream>
#include< string>
#include< set>
#include< tuple>
int main()
{
std :: set< std :: string> set_of_str;
bool inserted;
std :: tie(std :: ignore,inserted)= set_of_str.insert(Test);
if(inserted){
std :: cout<< 值已成功插入\\\
;
}
}
如果有人可以向我解释代码,谢谢。感谢。
set :: insert
返回一个对,其中第一个是迭代器到插入的元素,第二个是一个bool,说明元素是否被插入。 p>
std :: tie
创建一个左值引用的元组。当分配给 insert
的结果时,它允许您将 tie
中的变量设置为插入的结果成员 / p>
因此,基本上,这个代码忽略了元素的迭代器。 插入
并插入
到
第二
set :: insert
返回的对的成员,表示是否插入了一个元素。
I'm reading the documentation on std::ignore
from cppreference. I find it quite hard to grasp the true purpose of this object, and the example code doesn't do it much justice. For example, in the below code, how and why is inserted
set to true? It doesn't make much sense to me.
#include <iostream>
#include <string>
#include <set>
#include <tuple>
int main()
{
std::set<std::string> set_of_str;
bool inserted;
std::tie(std::ignore, inserted) = set_of_str.insert("Test");
if (inserted) {
std::cout << "Value was inserted sucessfully\n";
}
}
If someone can explain the code to me, it would be appreciated. Thanks.
set::insert
returns a pair where first is the iterator to the inserted element and second is a bool saying whether the element was inserted.
std::tie
creates a tuple of lvalue references. When assigned to the result from insert
it enables you to set the variables in the tie
to the results of the insert in the return pair
's first
and second
members.
std::ignore
is a value that can be assigned to with no effect.
So basically, this code ignores the iterator to the element where "Test"
was inserted and asigns inserted
to the second
member of the pair returned by set::insert
that indicates whether the an element was inserted.
这篇关于请解释这个使用std :: ignore的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!