c++11: initialize map with explicit initializer_list object [duplicate] - c++11

This question already has an answer here:
Constructing a std::map from initializer_list error
(1 answer)
Closed 7 years ago.
In C++11 I can initalize a map with an initializer_list like this:
map<string, int> mymap = {{"first", 1}, {"second", 2}};
But not like this:
initializer_list<pair<string,int>> il = {{"first", 1}, {"second", 2}};
map<string, int> mymap2{il};
Any idea why is that? Is there a different syntax for that or is it not possible at all?
Thanks.

The initializer_list constructor of map takes a list of value_type, which is a pair with first element const.
This will work:
initializer_list<pair<string const,int>> il = {{"first", 1}, {"second", 2}};
^^^^^^

Related

How to initialize multiple variables in a FOR loop in Rust [duplicate]

This question already has an answer here:
How to use multiple variables in Rust's for loop?
(1 answer)
Closed last month.
I want initialize multiple variables in a for loop, like for example something like this:
for (i, j) in (1..=4),(10..=16).step_by(2){ println!("i = {i}, j ={j}"); }
The result would be something like:
i = 1, j = 10 i = 2, j = 12 i = 3, j = 14 i = 4, j = 16
I have checked this post: How to use multiple variables in Rust's for loop?
but I havenĀ“t found anything useful for me, I know I can use a while loop, but I find the ability to set everything in just one line much clearer
The zip iterator method, mentioned in the very answer you link to, creates tuples by pairing up elements of two iterators. You can still use it all on one line; there is nothing that requires you to create the two iterators as separate statements.
for (i, j) in (1..=4).zip((10..=16).step_by(2)) {
println!("i = {i}, j ={j}");
}
(Playground)

Difference between arr and &arr if arr is an array of ints

I am just starting out with C++ and I have a question. If arr is an array or 10 ints, then arr is a pointer to the first element in the array. But what is meant by &arr? SO what is the difference bwteen a pointer to an array and a reference to an array?
then arr is a pointer to the first element in the array
No!
Arrays and pointers are two different types in C++ that have enough similarities between them to create confusion if they are not understood properly. The fact that pointers are one of the most difficult concepts for beginners to grasp doesn't help either.
So I fell a quick crash course is needed.
Crash course
Arrays, easy
int a[3] = {2, 3, 4};
This creates an array named a that contains 3 elements.
Arrays have defined the array subscript operator:
a[i]
evaluates to the i'th element of the array.
Pointers, easy
int val = 24;
int* p = &val;
p is a pointer pointing to the address of the object val.
Pointers have the indirection (dereference) operator defined:
*p
evaluates to the value of the object pointed by p.
Pointers, acting like arrays
Pointers give you the address of an object in memory. It can be a "standalone object" like in the example above, or it can be an object that is part of an array. Neither the pointer type nor the pointer value can tell you which one it is. Just the programmer. That's why
Pointers also have the array subscript operator defined:
p[i]
evaluates to the ith element to the "right" of the object pointed by p. This assumes that the object pointer by p is part of an array (except in p[0] where it doesn't need to be part of an array).
Note that p[0] is equivalent to (the exact same as) *p.
Arrays, acting like pointers
In most contexts array names decay to a pointer to the first element in the array. That is why many beginners think that arrays and pointers are the same thing. In fact they are not. They are different types.
Back to your question
what is meant by &arr
As said above, arr is not a pointer. So &arr means pointer to array. This is different from pointer to the first element of the array (&arr[0]).
SO what is the difference between a pointer to an array and a reference to an array?
Well, first of all please read What are the differences between a pointer variable and a reference variable in C++?. Second of all... well that's pretty much it.
the bulk of this answer is copied from this other answer of mine
There are 3 different concepts that you need to think about:
Pointer : the pointer is a variable that is used for storing the address of another variable.
Array: An array is a collection of variables of a similar data type
Reference : A reference variable is an alias, that is, another name for an already existing variable.
Here is a summary of how you can connect these three variables together:
arr == &arr[0]
and
*arr == arr[0]
For example
int arr[5] = {0,1,2,3,4};
arr is a pointer to pointer to the first element of array. While &arr a pointer to whole array of 5 int.
The value of arr and &arr are the same, but arr + 1 and &arr + 1 is different.
#include <iostream>
using namespace std;
int main()
{
int array[5];
cout << "array = " << array << " : &array = " << &array << endl;
cout << "array + 1 = " << array + 1 << " : &array + 1 = " << &array + 1;
return 0;
}
Result
array = 0x7fffebce1b80 : &array = 0x7fffebce1b80
array + 1 = 0x7fffebce1b84 : &array + 1 = 0x7fffebce1b94

To compare 2 integer arrays using Java 8 Features [duplicate]

This question already has answers here:
How do I get the intersection between two arrays as a new array?
(22 answers)
Closed 6 years ago.
Is it possible to do without external foreach to iterate b. Need to identify common values in 2 arays using Java 8
Integer a[]={1,2,3,4};
Integer b[]={9,8,2,3};
for(Integer b1:b) {
Stream.of(a).filter(a1 -> (a1.compareTo(b1) ==0)).forEach(System.out::println);
}
Output: 2 3
I would suggest using sets if you only want the common values (i.e. not taking duplicates into account)
Integer a[]={1,2,3,4};
Integer b[]={9,8,2,3};
Set<Integer> aSet = new HashSet<>(Arrays.asList(a));
Set<Integer> bSet = new HashSet<>(Arrays.asList(b));
aSet.retainAll(bSet);
Maybe something like this:
public static void main(String[] args) {
Integer a[] = {1, 2, 3, 4};
Integer b[] = {9, 8, 2, 3};
Stream<Integer> as = Arrays.stream(a).distinct();
Stream<Integer> bs = Arrays.stream(b).distinct();
List<Integer> collect = Stream.concat(as, bs)
.collect(Collectors.groupingBy(Function.identity()))
.entrySet()
.stream()
.filter(e -> e.getValue().size() > 1)
.map(e -> e.getKey())
.collect(Collectors.toList());
System.out.println(collect);
}
we merge two array into one stream
groupBy is counting by value
then we filter lists longer than 1, that lists contains duplicates
map to key to extract value of duplicated entry
print it.
edit: added distinct to initial streams.

Enumerate a "map" variable

I have the following "map" variables:
permutation[100]=-2;
permutation[3]=-1;
permutation[19]=0;
permutation[-20]=1;
Is there a way to enumerate all of the values?
I don't care about enumeration order.
I mean something like this (not working code):
Do[i+=3,{i,permutation}]
I tell them "map" variables inspired from C++. What is the correct name for them, to found better search results?
This is "Meta Mathematica"
Definitions for a variable or a functions are stored internally and can be extracted using DownValues[]
so, if you want the set of "input values" for which permutation is defined use
Cases[DownValues[permutation], permutation[x_] :> x, \[Infinity]]
resulting with
{-20, 3, 19, 100}
yehuda
what you have done is basically define a function of discrete values. If you maintain a list of the values you can do this:
permutation[100] = -2;
permutation[3] = -1;
permutation[19] = 0;
permutation[-20] = 1;
vals = {100, 3, 19, -20}
use like this:
f /# permutation /# vals -> {f[-2], f[-1], f[0], f[1]}
or in a loop..
Do[ Print[permutation[vals[[i]]]], {i, Length[vals]}]
there are probably better ways to define your data in the first place..
here is a sometimes useful trick (only works for posative args though)
permutation = SparseArray[{}, {200}, Null]
permutation[[100]] = -2;
permutation[[3]] = -1;
permutation[[19]] = 0;
f /# Select[ permutation, ! (TrueQ[# == Null]) &]
out: {f[-1], f[0], f[-2]}
simplest though is to just make permutation a list:
permutation={}
AppendTo[permutation,{100,2}]
AppendTo[permutation,{3,-1}]
then do
f[#[[2]]]& /# permutation -> {f[2],f[-1]}

How to do array addition in a smart way? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to sum array members in Ruby?
Lets say I have this array
#test = [1, 2, 3, 4]
Then I want to do:
#test[0] + #test[1] + #test[2] + #test[3]
Is there not a smarter, faster way of doing this?
You can do this:
#test.inject(:+)
sum = 0
#test.each { |el| sum+=el }

Resources