I want to do -in the best possible way- something like this python line, in c++ by means of std::vector
v1[v2] = 1
That is: v1 elements are set to 1 taking into account v2 elements as indexes for v1.
Where v1 is a valarray of float elements and v2 is a vector of unsigned int elements, (v1 is initialized to 0 in every element).
If I cast v2 from std::vector<unsigned int> to std::valarray<unsigned int> I get the following error:
error: no match for 'operator[]' (operand types are 'std::valarray<float>' and 'std::valarray<unsigned int>')
in the line v1[v2] = 1.
This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <valarray>
#include <iostream>
int main()
{
std::valarray<float> v1 = {0,0,0,0,0,0,0,0,0};
std::vector<unsignedint> v = {0,2,4,6,8};
std::valarray<unsignedint> v2(v.data(),v.size());
v1[v2] = 1; // <-- this is the key line
for(double d : v1) std::cout << d << ' ';
return 0;
}
On the other hand, when I try to cast v from std::vector<unsigned int> to std::valarray<size_t>:
(I do this because the line v1[v2] = 1; works fine when v2 is a std::valarray<long unsigned int>, I don't know why)
I get the following error:
error: invalid conversion from 'unsigned int*' to 'long unsigned int' [-fpermissive]
in the conversion line.
This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <valarray>
#include <iostream>
int main()
{
std::valarray<float> v1 = {0,0,0,0,0,0,0,0,0};
std::vector<unsignedint> v = {0,2,4,6,8};
std::valarray<size_t> v2(v.data(),v.size());
v1[v2] = 1;
for(double d : v1) std::cout << d << ' ';
return 0;
}
I know that std::size_t is an alias for longunsignedint in this implementation.
Yet, this does not give me a solution.
All I need is to cast from std::vector<unsigned int> to std::valarray<long unsigned int>.
I need this conversion because in the line v1[v2] = 1;, v2 has to be a
std::valarray<long unsigned int> in order to work properly.
Perhaps it is important to point out that v2 is a function parameter which brings the std::vector<unsigned int> type.
I thought there was a prefabricated way to do the conversion.
I just didn't want to reinvent the wheel.
I tested the template and it works perfectly.
Thank you very much JLBorges.