简体   繁体   English

请解释一下使用std :: ignore的代码

[英]Please explain this code that uses std::ignore

I'm reading the documentation on std::ignore from cppreference. 我正在阅读cppreference中关于std::ignore的文档。 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? 例如,在下面的代码中,如何以及为什么inserted设置为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. set::insert返回一对,其中first是插入元素的迭代器,第二个是bool,表示是否插入了元素。

std::tie creates a tuple of lvalue references. std::tie创建一个左值引用元组。 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. 当从insert结果时,它允许您将tie的变量设置为返回pair的第firstsecond成员中的insert的结果。

std::ignore is a value that can be assigned to with no effect. std::ignore是一个可以分配的值,但没有效果。

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. 所以基本上,这段代码忽略了插入"Test"的元素的迭代器,并inserted set::insert返回的对的second成员的asigns,指示是否set::insert了一个元素。

I think Dave's answer is pretty good, but I would like to explain a bit why to use such approach. 我认为Dave的答案非常好,但我想解释为什么要使用这种方法。

In other languages like Scala, Haskell or Python, you have usually the presence of tuples (being pair a tuple of two elements) and they have this idiomatic way to assign them to variables: 在Scala,Haskell或Python等其他语言中,你通常会出现元组(对两个元素的元组配对),并且它们有这种惯用的方式将它们分配给变量:

(var1,...,varN) = func_returning_tuple()

This has the purpose of expand semantic value of your code and improve his readability, where otherwise you would have a single variable with no semantics to their elements (like t.first, and so on), and in C++ to access values of tuples you would have to use: 这样做的目的是扩展代码的语义值并提高其可读性,否则你将拥有一个没有语义的单个变量(如t.first等),而在C ++中访问元组的值你必须使用:

varN = std::get<N>(my_tuple);

So, using only tie, you could make your example code easier to read as follows: 因此,只使用tie,您可以使示例代码更容易阅读,如下所示:

std::tie( element_iterator, inserted ) = set_of_str.insert("test");

And then use your isolated variables at will, this improves the way others (and even yourself) read the next statements of your code. 然后随意使用您的孤立变量,这改善了其他人(甚至您自己)阅读代码的下一个语句的方式。

The std::ignore is used when you don't care for what is returned, in some other languages you also have this resource, in Scala for example this is the underscore. 如果您不关心返回的内容,则使用std::ignore ,在Scala中,您也有此资源使用其他语言,例如这是下划线。 For example, if I use the insert function in a map and the value already exists it just returned the pair containing (iterator,false) so if I want the iterator for some key, even if I don't care if it already exists in the map, I can do it with this line: 例如,如果我在map中使用insert函数并且值已经存在,它只返回包含(iterator,false)的对(iterator,false)所以如果我想要一些键的迭代器,即使我不在乎它是否已经存在地图,我可以用这一行做到:

std::tie( element_iterator, std::ignore ) = set_of_str.insert("test");

That's the way C++ solves this readability issue of tuples and pairs. 这就是C ++解决元组和对的可读性问题的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM