How to populate a map using uniform initialization with insert method - c++11

I have something like this
std::unordered_map<int, std::unordered_map<int, std::string>> p;
and i am trying this
p.insert({ 0x00 ,{0x03,"Custom"} });
no instance of overloaded function ........ takes the parameter list error
Can anyone please tell me the correct syntax ?

One pair of braces is missing. The value of p is the collection - unordered_map, so by inserting at 0x00 as the key you can put the collection for example which consists of 2 items, the syntax is:
p.insert({ 0x00 , { {0x03,"Custom"},{0x04,"Custom2"} } });
^ ^brace for item ^brace for item ^
|->begin collections |-> end collections
so when the collection has one items, syntax should be:
p.insert({ 0x00 , { {0x03,"Custom"} } });

Related

Cannot understand C++ map semantics

I don't understand the constructor statement in the following code. How can the iterator to the past-of-end element be added to the map as a key?
template<typename K, typename V>
class my_map {
std::map<K,V> m_map;
public:
my_map( V const& val) {
m_map.insert(m_map.end(),std::make_pair(std::numeric_limits<K>::lowest(),val));
}
};
How can the iterator to the past-of-end element be added to the map as a key?
It's not the key. It's the position of the insertion. By passing end you're saying append to the map.
The key that you're inserting is the first part of the pair. i.e. std::numeric_limits<K>::lowest().
The value that you're inserting is the second part of the pair. i.e. val.
The docs for std::map::insert are useful.
How can the iterator to the past-of-end element be added to the map as a key?
That's an incorrect conclusion. std::map::insert has several overloads. The one that is use in your call is:
iterator insert( iterator hint, const value_type& value ); // Overload 4
which does the following:
Inserts value in the position as close as possible, just prior, to hint.

Eigen generic function to handle different data types

I am trying to write a generic function that receives a DenseBase<Derived> parameter like this:
template<class Derived>
MatrixXd Math::gradient(const DenseBase<Derived> &y, const Dimension dimension)
{
...
Derived v = dimension == COLUMNS ? y.derived() : y.derived().transpose();
...
}
I'm having an error when I call the function in this manner:
const VectorXd g = Math::gradient(curve/ peak);
The curve/ peak expression returns a CwiseBinaryOp type, which becomes the Derived type of the Math::gradient templated function.
However, the y.derived().transpose() expression returns a Transpose<MatrixType> which yields a compilation error due to the different data type with respect to the CwiseBinaryOp
I know I can call the Math::gradient function previously storing the curve/ peak in a VectorXd to solve the problem, however, how can I write the function to handle this different datatypes?
Thank you very much.
I think I have found a solution to my own question (sorry for the poor explanation of the problem).
By using the eval() method of the DenseBase<Derived> variable y, Eigen resolves the "intermediate" expression types, such as the CwiseBinaryOp or the Transpose<MatrixType> in my particular case, to a PlainObject variable type.
So a possible solution could be:
template<class Derived>
MatrixXd Math::gradient(const DenseBase<Derived> &y, const Dimension dimension)
{
...
typename Derived::PlainObject v = dimension == COLUMNS ? y.derived().eval() : y.derived().transpose().eval();
...
}
Thank you very much for your attention

C++ routine returning function

I have the following code which work fine. I am trying to understand the syntax. The return statement has std::plus<double>(). The double over here has the return value data type. But the function definition has the return type as std::function<double(double, double)> which indicates two double parameters. How do these two relate to each other?
#include <functional>
#include <iostream>
using namespace std;
std::function<double(double, double)> GetFunction()
{
return std::plus<double>();
}
int main()
{
auto operation = GetFunction();
int a = operation(1, 4);
std::cout << std::plus<>{}(1, 4) << '\n';
return 0;
}
There is an implicit conversion from std::plus<double> to std::function<double(double,double)>, because the former has a member call operator double operator()(double, double). See the documentation for std::function constructors.
In std::function<double(double, double)>:
The first double is the return type of the function. You can remember that by realizing that it's on the left, just like in a normal function definition.
The doubles in parentheses are the parameter types of the function, just like in a normal function definition; minus the parameter names. There are 2 since the plus function takes 2 doubles.
This makes sense if you think about it. The plus function/operator is a binary operator, meaning it takes 2 parameters of a type, and returns a single value of the same type. This is why you only need to specify a single type when you write std::plus<double>; the parameters and the return type must be the same type. It would be error prone and useless to force the caller to specify the same type 3 times.
If your question is, why there is only one double in the template parameter of std::plus but three in std::function, then the answer is this:
For std::plus, both parameters and the returntype always have to be the same, so you only have to specify it once.
std::function on the other hand can hold any function like object with any combination of parameters and returntypes, so you have to basically state each of those types individually.

Immutable value of type '[String]' only has mutating members named 'sort'

Reading Apple's Swift guide, more specifically the Closures chapter I've stumbled upon a problem. A String array is declared
var names = ["Cris", "Alex", "Ewa", "Barry", "Daniella"]
The purpose of this section is to pass an array of Strings (the one above) and with the function sort(_:) have the same array back but sorted using an auxiliary method by providing a closure. The closure is the method backwards
func backwards(s1: String, s2: String) -> Bool {
return s1 > s2
}
Then an array, reversed, is declared and applied the method sort to the original array, names:
var reversed = names.sort(backwards)
AFAIK reversed should be inferred as an array of Strings but when I check it is inferred as var reversed: <>. Now, reversed doesn't store anything and an error pops up:
Immutable value of type '[String]' only has mutating members named 'sort'
Later on in the chapter the closure is simplified a lot but I get the same error as now so I tried to fix it when the expression is simple enough but I have no clue on what to do.
I don't know if the book forgot something or is my mistake, help will be appreciated. Thank you in advance!

Iterating through boost ptr_vector

I have a ptr_vector list of my own objects. Something like this:
boost::ptr_vector<SomeClass> *list;
list->push_back(new SomeClass()>;
...
BOOST_FOREACH(SomeClass *tempObj, list) // [x]
{
tempObj->...
}
>‘boost::ptr_vector<SomeClass>*’ is not a class, struct, or union type
I think your problem is that you declared 'list' as a pointer to a boost::ptr_vector and are trying to use it as an automatic object.
IMHO the first line of your code snippet should read:
boost::ptr_vector<SomeClass> list;

Resources