I have a POD class and I want to make it movable for efficiency. I keep all the data in a std::array member object, and I make my public member variables references to parts of this std::array object. By doing this, now I am able to move the entire data by moving the std::array instance in the move constructor (I know that it is not literally a POD class anymore after writing constructors.).
Is this a good method of doing this? Does it actually move the data? See the code output below: After moving the std::array, I observe that both objects have the same values. It looks like it doesn't move, but it copies the data. What is the problem here?
#include <array>
class MyPodClass
{
private:
typedef double TYPE_x;
typedef double TYPE_y;
typedef double TYPE_z;
typedef int TYPE_p;
typedef int TYPE_r;
typedef int TYPE_s;
typedef char TYPE_k;
typedef char TYPE_l;
typedef char TYPE_m;
typedef float TYPE_a;
typedef float TYPE_b;
typedef float TYPE_c;
enum TypeSizes
{
STARTING_POSITION_x = 0,
STARTING_POSITION_y = STARTING_POSITION_x + sizeof(TYPE_x),
STARTING_POSITION_z = STARTING_POSITION_y + sizeof(TYPE_y),
STARTING_POSITION_p = STARTING_POSITION_z + sizeof(TYPE_z),
STARTING_POSITION_r = STARTING_POSITION_p + sizeof(TYPE_p),
STARTING_POSITION_s = STARTING_POSITION_r + sizeof(TYPE_r),
STARTING_POSITION_k = STARTING_POSITION_s + sizeof(TYPE_s),
STARTING_POSITION_l = STARTING_POSITION_k + sizeof(TYPE_k),
STARTING_POSITION_m = STARTING_POSITION_l + sizeof(TYPE_l),
STARTING_POSITION_a = STARTING_POSITION_m + sizeof(TYPE_m),
STARTING_POSITION_b = STARTING_POSITION_a + sizeof(TYPE_a),
STARTING_POSITION_c = STARTING_POSITION_b + sizeof(TYPE_b),
END_POSITION = STARTING_POSITION_c + sizeof(TYPE_c),
};
std::array<unsigned char, END_POSITION> MovableBulkData;
public:
MyPodClass()
: //x(*static_cast<TYPE_x*>(&MovableBulkData[STARTING_POSITION_x])), // ERROR: Invalid type conversion. Why?
x(*(TYPE_x*)(&MovableBulkData[STARTING_POSITION_x])),
y(*(TYPE_y*)(&MovableBulkData[STARTING_POSITION_y])),
z(*(TYPE_z*)(&MovableBulkData[STARTING_POSITION_z])),
p(*(TYPE_p*)(&MovableBulkData[STARTING_POSITION_p])),
r(*(TYPE_r*)(&MovableBulkData[STARTING_POSITION_r])),
s(*(TYPE_s*)(&MovableBulkData[STARTING_POSITION_s])),
k(*(TYPE_k*)(&MovableBulkData[STARTING_POSITION_k])),
l(*(TYPE_l*)(&MovableBulkData[STARTING_POSITION_l])),
m(*(TYPE_m*)(&MovableBulkData[STARTING_POSITION_m])),
a(*(TYPE_a*)(&MovableBulkData[STARTING_POSITION_a])),
b(*(TYPE_b*)(&MovableBulkData[STARTING_POSITION_b])),
c(*(TYPE_c*)(&MovableBulkData[STARTING_POSITION_c]))
{
}
MyPodClass(MyPodClass && RValue)
: MovableBulkData(std::move(RValue.MovableBulkData)),
x(*(TYPE_x*)(&MovableBulkData[STARTING_POSITION_x])),
y(*(TYPE_y*)(&MovableBulkData[STARTING_POSITION_y])),
z(*(TYPE_z*)(&MovableBulkData[STARTING_POSITION_z])),
p(*(TYPE_p*)(&MovableBulkData[STARTING_POSITION_p])),
r(*(TYPE_r*)(&MovableBulkData[STARTING_POSITION_r])),
s(*(TYPE_s*)(&MovableBulkData[STARTING_POSITION_s])),
k(*(TYPE_k*)(&MovableBulkData[STARTING_POSITION_k])),
l(*(TYPE_l*)(&MovableBulkData[STARTING_POSITION_l])),
m(*(TYPE_m*)(&MovableBulkData[STARTING_POSITION_m])),
a(*(TYPE_a*)(&MovableBulkData[STARTING_POSITION_a])),
b(*(TYPE_b*)(&MovableBulkData[STARTING_POSITION_b])),
c(*(TYPE_c*)(&MovableBulkData[STARTING_POSITION_c]))
{
}
const MyPodClass & operator=(MyPodClass && RValue)
{
MovableBulkData = std::move(RValue.MovableBulkData);
return *this;
}
TYPE_x & x;
TYPE_y & y;
TYPE_z & z;
TYPE_p & p;
TYPE_r & r;
TYPE_s & s;
TYPE_k & k;
TYPE_l & l;
TYPE_m & m;
TYPE_a & a;
TYPE_b & b;
TYPE_c & c;
};
int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
MyPodClass PodObject1, PodObject2;
PodObject1.y = 3.4;
PodObject1.s = 4;
PodObject1.m = 'm';
PodObject1.a = 2.3f;
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << "Moving PodObject1 to PodObject2..." << std::endl << std::endl;
PodObject2 = std::move(PodObject1);
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << "Modifying PodObject1 and PodObject2..." << std::endl << std::endl;
PodObject1.s = 5;
PodObject2.m = 'n';
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << std::endl;
_wsystem(L"timeout /t 60 /nobreak");
return 0;
}
Output:
PodObject1.y = 3.4
PodObject1.s = 4
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = -9.25596e+61
PodObject2.s = -858993460
PodObject2.m = ╠
PodObject2.a = -1.07374e+08
Moving PodObject1 to PodObject2...
PodObject1.y = 3.4
PodObject1.s = 4
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = 3.4
PodObject2.s = 4
PodObject2.m = m
PodObject2.a = 2.3
Modifying PodObject1 and PodObject2...
PodObject1.y = 3.4
PodObject1.s = 5
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = 3.4
PodObject2.s = 4
PodObject2.m = n
PodObject2.a = 2.3
This is a misuse of move semantics. Since your class contains a number of simple data members like int and float, there is really nothing to move. You'd be better off with memcpy(), which is probably close to what your compiler gives you for free if you just write the class the normal, naive way, with no std::array and no pointer gymnastics.
Move semantics would have been useful here if your class contained e.g. a std::string, because std::string uses dynamically allocated memory which can be "moved" (read: adopted) into the target of a move.
The above of course means that you could "fix" your problem by dynamically allocating the array, which would allow you to move it. But in the end this would be a baroque way to achieve the effect of using a trivial POD class with no gymnastics and storing it in a std::unique_ptr, which of course enables move semantics.
Related
I am trying to calculate the lpNorm of a vector with the Eigen library.
As it can be seen in the example below, with explicit values, such as v.lpNorm<1>(), it works. But it doesn't work inside the loop, with lpNorm<p>()
How can I fix this?
#include <iostream>
#include <Eigen/Dense>
using Eigen::VectorXd;
int main()
{
int sizev = 3;
int p;
Eigen::VectorXd v(sizev);
v(0) = 3.;
v(1) = 2.;
v(2) = 1.;
// test 1, passes
std::cout << "||v||_" << 1 << " = " << v.lpNorm<1>() << std::endl;
std::cout << "||v||_" << 2 << " = " << v.lpNorm<2>() << std::endl;
std::cout << "||v||_" << 3 << " = " << v.lpNorm<3>() << std::endl;
std::cout << "||v||_" << 4 << " = " << v.lpNorm<4>() << std::endl;
std::cout << "||v||_inf = " << v.lpNorm<Eigen::Infinity>() << std::endl;
// test 2, fails
for (int p=1; p<5; p++)
{
std::cout << "||v||_" << p << " = " << v.lpNorm<p>() << std::endl;
}
return 0;
}
On compilation, I am getting the error
error: no matching member function for call to 'lpNorm'
std::cout << "||v||_" << p << " = " << v.lpNorm<p>() << std::endl;
~~^~~~~~~~~
note: candidate template ignored: invalid explicitly-specified argument for template parameter 'p'
template<int p> EIGEN_DEVICE_FUNC RealScalar lpNorm() const;
^
1 error generated.
You cannot use variable integer as a template argument, it needs to be a compile time constant, such as
constexpr int p = 3;
v.lpNorm<p>();
However, you can still have a kind of compile-time loop using e.g. std::integer_sequence. I modified a bit the example from documentation to call a function:
template<typename T, T... ints>
void exec_constexpr_loop(std::integer_sequence<T, ints...> int_seq, Eigen::Ref<Eigen::VectorXd> v)
{
((v.lpNorm<ints>()), ...);
}
exec_constexpr_loop(std::integer_sequence<int, 1, 2, 3>{}, v);
Live demo with dummy function, works since C++17.
I would like to create new Eigen::Array using an index.
I know it is possible for Eigen::Matrix, a code is given here.
A similar question has also been posted on stackoverflow
The question is how to update the following code to work with Eigen::Array
#include <iostream>
#include <stdio.h>
#include <Eigen/Core>
using namespace Eigen;
template<class ArgType, class RowIndexType, class ColIndexType>
class indexing_functor {
const ArgType &m_arg;
const RowIndexType &m_rowIndices;
const ColIndexType &m_colIndices;
public:
typedef Matrix<typename ArgType::Scalar,
RowIndexType::SizeAtCompileTime,
ColIndexType::SizeAtCompileTime,
ArgType::Flags&RowMajorBit?RowMajor:ColMajor,
RowIndexType::MaxSizeAtCompileTime,
ColIndexType::MaxSizeAtCompileTime> MatrixType;
indexing_functor(const ArgType& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
: m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices)
{}
const typename ArgType::Scalar& operator() (Index row, Index col) const {
return m_arg(m_rowIndices[row], m_colIndices[col]);
}
};
template <class ArgType, class RowIndexType, class ColIndexType>
CwiseNullaryOp<indexing_functor<ArgType,RowIndexType,ColIndexType>, typename indexing_functor<ArgType,RowIndexType,ColIndexType>::MatrixType>
indexing(const Eigen::MatrixBase<ArgType>& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
{
typedef indexing_functor<ArgType,RowIndexType,ColIndexType> Func;
typedef typename Func::MatrixType MatrixType;
return MatrixType::NullaryExpr(row_indices.size(), col_indices.size(), Func(arg.derived(), row_indices, col_indices));
}
With #ggael's help, I made this little test program that uses Eigen master branch.
First I fetch eigen repo, that points on master branch, and includes new indexing features, that are not present in tagged version 3.3.5.
git clone https://github.com/eigenteam/eigen-git-mirror
Here is the program that I compile with g++ -O3 -isystem eigen-git-mirror -o eigen_indexing eigen_indexing.cpp
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::ArrayX3d array = Eigen::ArrayX3d::Random(8, 3);
Eigen::ArrayXi indices(5);
indices << 4, 7, 0, 2, 1;
Eigen::Array3i cols(0,1,2);
std::cout << "array = " << std::endl << array << std::endl << std::endl;
std::cout << "indices = " << std::endl << indices << std::endl << std::endl;
std::cout << "cols = " << std::endl << cols << std::endl << std::endl;
std::cout << "array(indices,cols) = " << std::endl << array(indices,cols) << std::endl << std::endl;
std::cout << "Equivalent but Eigen::placeholders::all syntax may evolve " << std::endl << std::endl;
std::cout << "array(indices, Eigen::placeholders::all) = " << std::endl << array(indices,Eigen::placeholders::all) << std::endl << std::endl;
std::cout << "Assignment also works array(indices,cols) = 0.0;" << std::endl<< std::endl;
array(indices,cols) = 0.0;
std::cout << "array = " << std::endl << array << std::endl << std::endl;
std::cout << "Assignment also works array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);" << std::endl<< std::endl;
array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);
std::cout << "array = " << std::endl << array << std::endl << std::endl;
return 0;
}
If the program can not compile, here is the version I have used
cd eigen-git-mirror
# git checkout master
git checkout a31719c75f244673c962ee65f279606bee6fc7ef
cd ..
Here is the output of the program
array =
-0.999984 0.358593 0.342299
-0.736924 0.869386 -0.984604
0.511211 -0.232996 -0.233169
-0.0826997 0.0388327 -0.866316
0.0655345 0.661931 -0.165028
-0.562082 -0.930856 0.373545
-0.905911 -0.893077 0.177953
0.357729 0.0594004 0.860873
indices =
4
7
0
2
1
cols =
0
1
2
array(indices,cols) =
0.0655345 0.661931 -0.165028
0.357729 0.0594004 0.860873
-0.999984 0.358593 0.342299
0.511211 -0.232996 -0.233169
-0.736924 0.869386 -0.984604
Equivalent but Eigen::placeholders::all syntax may evolve
array(indices, Eigen::placeholders::all) =
0.0655345 0.661931 -0.165028
0.357729 0.0594004 0.860873
-0.999984 0.358593 0.342299
0.511211 -0.232996 -0.233169
-0.736924 0.869386 -0.984604
Assignment also works array(indices,cols) = 0.0;
array =
0 0 0
0 0 0
0 0 0
-0.0826997 0.0388327 -0.866316
0 0 0
-0.562082 -0.930856 0.373545
-0.905911 -0.893077 0.177953
0 0 0
Assignment also works array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);
array =
-8.1607 5.24396 2.65277
-1.68001 -9.05071 9.82075
3.07838 -4.75094 5.12821
-0.0826997 0.0388327 -0.866316
6.92334 4.02381 4.72164
-0.562082 -0.930856 0.373545
-0.905911 -0.893077 0.177953
0.538576 8.20642 -3.43532
I am trying to load freetype chars, stuff them into a texture as subimages and then render them instanced.
While most of it seems to work, right now I have a problem with storing the texture coordinates into a glm::mat2x4 matrix.
As can be seen below each character has a struct with information I right now deem necessary, including a matrix called face, which should store the texture coordinates.
But when it comes to assigning the coordinates, after leaving the loop in which it takes place, suddenly all the values go crazy, without any (wanted/ intended) operation taking place from my side.
After creating the texture atlas with freetype and putting all my structs into the map, I assign the width and height of my texture aw & ah to a storage class called c_atlas.
I calculate the texture coordinates in the loop shown below, make the glm::mat2x4 a 0.0f matrix and then stuff them into it. Couting them into the console gives the values I want.
After leaving the for loop I start another one, browsing over the matrix and cout them into the console, which gives me more or less random values in the range of e^-23 to e^32.
All of this happens in namespace foo and is called in a constructor of a class in the same namespace (sth. like this:)
foo::class::constructor()
{
call_function();
}
int main()
{
foo::class c;
c.call_function();
}
I crafted a minimum working example, but unfortunatly I am not able to replicate the error.
So I have the following loop running (a part of call_function():
namespace foo
{
namespace alphabet
{
const char path_arial[] = "res/font/consola.ttf";
class character
{
public:
glm::vec2 advance;
glm::vec2 bearing;
glm::vec2 size;
glm::vec2 offset;
glm::mat2x4 face;
};
std::map<char, character> char_map;
FT_Library m_ftlib;
FT_Face m_ftface;
GLuint m_VBO, m_VAO;
}
c_atlas ascii;
}
void foo::call_function()
{
//creating all the charactur structs with freetype and store them in the char_map
std::ofstream f("atlas_data.csv", std::ios::openmode::_S_app);
f << "letter;topleft.x;topleft.y;topright.x;topright.y;bottomright.x;bottomright.y;bottomleft.x;bottomleft.y" << std::endl;
for(auto c : alphabet::char_map)
{
std::cout << "b4: " << c.second.offset.x;
c.second.offset /= glm::vec2(aw,ah);
std::cout << "\nafter: " << c.second.offset.x << std::endl;
glm::vec2 ts = c.second.size/glm::vec2(aw,ah);
//couts the right values
uint16_t n = 0;
c.second.face = glm::mat2x4(0.0f);
for(uint16_t i = 0; i < 4; ++i)
{
std::cout << c.first << " at init:\n";
std::cout << c.second.face[0][i] << "\n";
std::cout << c.second.face[1][i] << std::endl;
}
//couts the right values
c.second.face[0][n++] = c.second.offset.x;
c.second.face[0][n++] = c.second.offset.y;
c.second.face[0][n++] = c.second.offset.x+ts.x;
c.second.face[0][n++] = c.second.offset.y;
n = 0;
c.second.face[1][n++]= c.second.offset.x+ts.x;
c.second.face[1][n++] = c.second.offset.y+ts.y;
c.second.face[1][n++] = c.second.offset.x;
c.second.face[1][n++]= c.second.offset.y+ts.y;
for(uint16_t i = 0; i < 4; ++i)
{
std::cout << c.first << " assigned:\n";
std::cout << c.second.face[0][i] << "\n";
std::cout << c.second.face[1][i] << std::endl;
}
//still couts the right values
f << (char)c.first << ";" << c.second.face[0].x << ";" << c.second.face[0].y << ";" << c.second.face[0].z << ";" << c.second.face[0].w << ";" << c.second.face[1].x << ";" << c.second.face[1].y << ";" << c.second.face[1].z << ";" << c.second.face[1].w << std::endl;
//the file also have the right values
}
f.close();
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
//yet here all the values totally off track, i.e. e^32 or e^-23 (while they should all be between 0.01f - 1.0f)
for(auto i : alphabet::char_map)
{
std::cout << "\ntopleft:\n";
std::cout << "X: " << i.second.face[0].x << " | " << "Y: " << i.second.face[0].x;
std::cout << "\ntopright:\n";
std::cout << "X: " << i.second.face[0].z << " | " << "Y: " << i.second.face[0].w;
std::cout << "\nbotleft:\n";
std::cout << "X: " << i.second.face[1].x << " | " << "Y: " << i.second.face[1].x;
std::cout << "\nbotright:\n";
std::cout << "X: " << i.second.face[1].z << " | " << "Y: " << i.second.face[1].w;
}
}
my mwe:
#include <iostream>
#include <string>
#include "glm/glm.hpp"
#include "GL/gl.h"
#include <map>
struct bin
{
glm::mat2x4 mat;
};
int main( int argc, char *argv[] )
{
std::map<char, bin> bucket;
uint16_t r = 0;
for(uint16_t n = 0; n < 7; ++n)
{
glm::vec4 v = glm::vec4(0.12128f, 0.12412f, 0.15532f, 0.23453f);
bin b;
r = 0;
b.mat[0][r++] = v.x;
b.mat[0][r++] = v.y;
b.mat[0][r++] = v.z;
b.mat[0][r++] = v.w;
r = 0;
b.mat[1][r++] = v.x;
b.mat[1][r++] = v.y;
b.mat[1][r++] = v.z;
b.mat[1][r++] = v.w;
bucket[n] = b;
}
for(auto it : bucket)
{
r = 0;
std::cout << "0:\t" << it.second.mat[0][0] << "\t" << it.second.mat[0][1] << "\t" << it.second.mat[0][2] << "\t" << it.second.mat[0][3] << "\n";
r = 0;
std::cout << "1:\t" << it.second.mat[1][0] << "\t" << it.second.mat[1][1] << "\t" << it.second.mat[1][2] << "\t" << it.second.mat[1][3] << std::endl;
}
return 0;
}
Right now I am totally lost, especially as my mwe works fine.
I am clueless what goes wrong after leaving the for-loop, so thanks for any thought on that!
Indeed, I could just rewrite that section and hope it would work - as my mwe does. But I would like to find out/ get help on finding out what exactly happens between the "assign" for loop and the "retrieve" for loop. Any ideas on that?
I made it work for me now:
Appartenly assigning the values this way:
for(auto c : alphabet::char_map)
{
c.second.face[0][n++] = c.second.offset.x;
//and so on
}
Did not work properly (for whatever reason..)
Changing this into a for(uint16_t i = 32; i < 128; ++i) worked for me. Also it was just the assigning loop, the auto-iterating ofer the map elsewhere works just fine.
I have been searching on Google an in this forum for a while, but I could not find any answer or tip for my problem. Tutorials couldn't help me either...
I want to redistribute some points, stored in a vector p_org. (x-value is stored as double).
Therefore I have the function distribute, which is defined in maths.h
distribute_tanh(&p_org_temp,&p_new_temp,iz,spacing[0],spacing[1],l_rot[(kk+1)*iz-2],status);
The function distribute_tanh does look like this:
inline void distribute_tanh (std::vector<double> *p_org, std::vector<double> *p_new, const int n_points, double spacing_begin, double spacing_end, const double total_length, double status){
//if status == 0: FLAP, if status == 1: SLAT
std::cout << "spacing_begin: " << spacing_begin << " spacing_end: " << spacing_end << std::endl;
double s_begin = spacing_begin / total_length;
double s_end = spacing_end / total_length;
double A = sqrt(s_end/s_begin);
double B = 1 / (sqrt(s_end*s_begin)*n_points);
std::cout << "A: " << A << " B: " << B << std::endl;
std::vector<double> u (n_points);
std::vector<double> sn (n_points);
double dx;
double dy;
std::cout << "Control at the beginning: p_org: " << (p_org) << " p_new: " << (p_new) << " n_points: " << n_points << " s_begin: " << s_begin << " s_end: " << s_end << " total_length: " << total_length << std::endl;
//problem no. 1
for (int i=0;i<n_points;i++){
if (B > 1.001) {
if (B < 2.7829681) {
double Bq=B-1;
dy=sqrt(6*Bq)*(1-0.15*Bq+0.057321429*pow(Bq,2)-0.024907295*pow(Bq,3)+0.0077424461*pow(Bq,4)-0.0010794123*pow(Bq,5));
} else if (B > 2.7829681) {
double Bv=log(B);
double Bw=1/B-0.028527431;
dy=Bv+(1+1/Bv)*log(2*Bv)-0.02041793+0.24902722*Bw+1.9496443*pow(Bw,2)-2.6294547*pow(Bw,3)+8.56795911*pow(Bw,4);
}
u[i]=0.5+(tanh(dy*(i*(1.0/n_points)-0.5))/(2*tanh(dy/2)));
}
else if (B < 0.999) {
if (B < 0.26938972) {
dx=M_PI*(1-B+pow(B,2)-(1+(pow(M_PI,2))/6)*pow(B,3)+6.794732*pow(B,4)-13.205501*pow(B,5)+11.726095*pow(B,6));
} else if (B > 0.26938972) {
double Bq=1-B;
dx=sqrt(6*Bq)*(1+0.15*Bq+0.057321429*pow(Bq,2)+0.048774238*pow(Bq,3)-0.053337753*pow(Bq,4)+0.075845134*pow(Bq,5));
}
u[i]=0.5+(tan(dx*(i*(1.0/n_points)-0.5))/(2*tan(dx/2)));
}
else {
u[i]=i*(1.0/n_points)*(1+2*(B-1)*(i*(1.0/n_points)-0.5)*(1-i*(1.0/n_points)));
}
sn[i]=u[i]/(A+(1.0-A)*u[i]);
std::cout << "sn(i): " << sn[i] << std::endl;
std::cout << "p_org[n_points]: " << &p_org[n_points-1] << std::endl;
if(status==0){
//p_new[i]=p_org[0]+(total_length*sn[i]);
std::cout << "FLAP maths.h" << std::endl;
}
//Here is the problem no. 2
else if(status==1){
//p_new[i]=p_org[0]-(total_length*sn[i]);
std::cout << "SLAT maths.h" << std::endl;
}
//std::cout << "p_new in math: " << p_new << std::endl;
}
}
My problem is, that I am unable to access the value of p_org or p_new. At the beginning I would like to give out the value of p_org and p_new. If I try it with a *, the compiler is complaining: error: no operator "<<" matches these operands
operand types are: std::basic_ostream> << std::vector>
std::cout << "Control at the beginning: p_org: " << (*p_org) << " p_new: " << (*p_new) << " n_points: " << n_points << " s_begin: " << s_begin << " s_end: " << s_end << " total_length: " << total_length << std::endl;
If I leave the * off, I get the addresses of p_org and p_new.
At the end of the code I would like to write the new value to p_new. If I use * to access the value, the compiler is complaining, if I leave it off, its complaining too with the following message:
error: no operator "-" matches these operands
operand types are: std::vector<double, std::allocator<double>> - double
p_new[i]=p_org[0]-(total_length*sn[i]);
^
I tried to understand both problems, but until now I had no success.
Thanks for your advice.
Your issue with the compiler error can be cut down to a very simple program.
#include <vector>
void foo(std::vector<int>* pV)
{
pV[0] = 10; // error.
}
int main()
{
std::vector<int> v(10);
foo(&v);
}
The issue is that operator[] as done above works for objects and references, not pointers. Since pv is a pointer, you must dereference it first to obtain the object, and then apply [] to the dereferenced pointer.
void foo(std::vector<int>* pV)
{
(*pV)[0] = 10; // No error
}
The other form of calling operator[] can be also used, but is a bit more verbose:
void foo(std::vector<int>* pV)
{
pv->operator[](0) = 10; // No error
}
However, to alleviate having to do this, pass the vector by reference. Then the "normal" way of using operator[] can be used.
#include <vector>
void foo(std::vector<int>& pV)
{
pV[0] = 10; // No error.
}
int main()
{
std::vector<int> v(10);
foo(v);
}
I've got a question that should be interesting. I'd like to "forward initialize" an item in a std::unordered_map upon construction.
These are the details. I've got a hash map from std::string to a custom class prop, which in my dreams, would initialize a member variable calculating the hash of the string passed to std::unordered_map::operator[].
This is a handy code I've written, but I don't know where to start.
Why this trouble? Because I'd like to avoid something like "if the string is NOT in the container calculate the hash; do stuff with prop". Avoiding this if could be something that might affect my performances. So the constructor, as well as the hashing, will be executed only once, when the map adds a new item in the container. It would be great.
Any hints?
Thanks & Cheers!
#include <iostream>
#include <string>
#include <unordered_map>
class prop
{
public:
prop(std::string s = "") : s_(s), hash_(std::hash<std::string>()(s))
{
// Automagically forwarding the string in the unordered_map...
};
std::string s_;
std::size_t hash_;
int x;
};
int main(int argc, const char * argv[])
{
// Forward the std::string to the prop constructor... but how?
std::unordered_map<std::string, prop> map;
map["ABC"].x = 1;
map["DEF"].x = 2;
map["GHI"].x = 3;
map["GHI"].x = 9; // This should not call the constructor: the hash is there already
std::cout << map["ABC"].x << " : " << map["ABC"].s_ << " : " << map["ABC"].hash_ << std::endl;
std::cout << map["DEF"].x << " : " << map["DEF"].s_ << " : " << map["DEF"].hash_ << std::endl;
std::cout << map["GHI"].x << " : " << map["GHI"].s_ << " : " << map["GHI"].hash_ << std::endl;
std::cout << map["XXX"].x << " : " << map["XXX"].s_ << " : " << map["XXX"].hash_ << std::endl;
return 0;
}
Just use your prop class as a key, instead of string:
#include <iostream>
#include <string>
#include <unordered_map>
class prop
{
public:
prop(std::string s = "") : s_(s), hash_(std::hash<std::string>()(s))
{
// Automagically forwarding the string in the unordered_map...
};
std::string s_;
std::size_t hash_;
};
int main(int argc, const char * argv[])
{
// Forward the std::string to the prop constructor... but how?
std::unordered_map<prop, int, ...> map( ... );
prop pABC( "ABC" ), pDEF( "DEF" ), pGHI( "GHI" );
map[pABC] = 1;
map[pDEF] = 2;
map[pGHI] = 3;
map[pGHI] = 9;
std::cout << map[pABC] << " : " << pABC.s_ << " : " << pABC.hash_ << std::endl;
std::cout << map[pDEF] << " : " << pDEF.s_ << " : " << pDEF.hash_ << std::endl;
std::cout << map[pGHI] << " : " << pGHI.s_ << " : " << pGHI.hash_ << std::endl;
prop pXXX( "XXX" );
std::cout << map[pXXX] << " : " << pXXX.s_ << " : " << pXXX.hash_ << std::endl;
return 0;
}
I omitted custom hash and compare function, the idea should be clear without it.