tuples

How to use std::tuple types with boost::mpl algorithms?

女生的网名这么多〃 提交于 2019-12-03 07:43:00
The boost::mpl algorithms seem not to be able to work on std::tuple types out of the box, e.g., the following does not compile (boost-1.46.0, g++ snapshot 2011-02-19): #include <tuple> #include <boost/mpl/vector.hpp> #include <boost/mpl/contains.hpp> namespace mpl=boost::mpl; typedef mpl::vector<int,float,bool> types; static_assert(mpl::contains<types, float>::value, "vector contains bool"); typedef std::tuple<int,float,bool> types2; // the following does not compile: // error: no class template named ‘apply’ in ‘struct boost::mpl::contains_impl<boost::mpl::non_sequence_tag>’ static_assert(mpl

In Scala, is there a way to take convert two lists into a Map?

独自空忆成欢 提交于 2019-12-03 06:42:28
问题 I have a two lists, a List[A] and a List[B] . What I want is a Map[A,B] but I want the semantics of zip . So started out like so: var tuplesOfAB = listOfA zip listOfB Now I'm not sure how to construct a Map from my tuplesOfAB . As a follow-up question, I also want to invert my map so that from a Map[A,B] I can create a Map[B,A] . Can anyone hit me with a clue-stick? 回答1: In 2.8 this is really simple using the CanBuildFrom functionality (as described by Daniel) and using breakOut with a type

Unpacking tuples in a python list comprehension (cannot use the *-operator)

时光总嘲笑我的痴心妄想 提交于 2019-12-03 06:39:05
I am trying to create a list based on another list, with the same values repeated 3 times consecutively. At the moment, I am using: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> for i in range( len( my_list ) ): ... for j in range( 3 ): ... three_times.append( my_list[ i ] ) ... >>> print three_times [1, 1, 1, 2, 2, 2] But I would like to do it using a more Pythonic way, such as: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> three_times = [ (value,) * 3 for value in my_list ] >>> print three_times [(1, 1, 1), (2, 2, 2)] However, I cannot find a way to unpack the tuples. Something like

Elixir: pattern matching works differently for tuples and maps

丶灬走出姿态 提交于 2019-12-03 06:38:34
In Elixir, if I try to pattern match the following two tuples: {a} = {1, 2} I get a match error. But if I do the same for two maps: %{x: a} = %{x: 1, y: 2} It works fine, and a binds to 1. I can see why matching the two tuples gave an error, but why did matching the maps not give an error? In the first example you are attempting to match a single element tuple against a two-element tuple. In the second example you are matching on the :x key in both the left and right maps. EDIT: I should clarify the rules around data structures and pattern matching in Elixir. When matching on tuples, you need

How to create a new tuple type from an old one and a type in boost?

江枫思渺然 提交于 2019-12-03 06:38:05
I have a tuple type. I want to add a element type in it to get a new tuple type. I can do it like decltype tuple_cat(MyTuple, std::tuple<MyType>()) However, I don't find tuple_cat in boost::tuple , how to do it in boost? I assume you want all this in compile time. Here is the general explanation: concatening tuples is similar to concatening lists or arrays, is that the algorithm is the same. Here, given tuples a and b , I choosed to move the last element of a to the beginning of b , and repeat until a is empty. First: base structures. The following structure keeps a parameter pack. It can be

What is a tuple useful for?

佐手、 提交于 2019-12-03 06:28:40
问题 I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this different from a list? 回答1: Tuples are used whenever you want to return multiple results from a function. Since they're immutable, they can be used as keys for a dictionary (lists can't

Add another tuple to a tuple of tuples

耗尽温柔 提交于 2019-12-03 06:12:06
问题 I have the following tuple of tuples: my_choices=( ('1','first choice'), ('2','second choice'), ('3','third choice') ) and I want to add another tuple to the start of it another_choice = ('0', 'zero choice') How can I do this? the result would be: final_choices=( ('0', 'zero choice') ('1','first choice'), ('2','second choice'), ('3','third choice') ) 回答1: Build another tuple-of-tuples out of another_choice , then concatenate: final_choices = (another_choice,) + my_choices Alternately,

How to get the i-th element from an std::tuple when i isn't know at compile-time?

时光毁灭记忆、已成空白 提交于 2019-12-03 05:49:17
问题 I have a variable i of type std::size_t and a tuple of type std::tuple . I want to get the i -th element of the tuple. I tried this: // bindings... is of type const T&... auto bindings_tuple = std::make_tuple(bindings...); auto binding = std::tuple_element<i, const T&...>(bindings_tuple); But I get this compile error saying that the first template argument must be an integral constant expression: error: non-type template argument of type ' std::size_t ' (aka ' unsigned long ') is not an

Detect if a type is a std::tuple?

爱⌒轻易说出口 提交于 2019-12-03 05:41:00
Currently I have two functions : template<typename Type> bool f(Type* x); template<typename... List> bool f(std::tuple<List...>* x); Is there any way to merge these two functions with an extra template parameter that indicates whether the passed type is a tuple ? template<typename Type, bool IsTuple = /* SOMETHING */> bool f(Type* x); Xeo Sure, using is_specialization_of (link taken and fixed from here ): template<typename Type, bool IsTuple = is_specialization_of<Type, std::tuple>::value> bool f(Type* x); The question is, however, do you really want that? Normally, if you need to know if a

Anonymous type and tuple

谁都会走 提交于 2019-12-03 05:36:43
问题 What is the difference between anonymous type and tuple? 回答1: Anonymous types have property names which carry more information, for tuples you don't have this. You can't use anonymous types as return values and parameters though and you can with tuples. An example of when a tuple is nice is when you want to return multiple values. @Petar Minchev mentions this link which gives a good example. You may want a Find() method that returns both an index and the value. Another example would be the