|
With that little information it's anyone's guess what is going on. I have used AddString in the OnInitDialog method, in normal and owner drawn list boxes.
|
|
|
|
|
Ok thanks that’s helpful I’ll do some digging
|
|
|
|
|
Consider the following code
"main.cpp"
#include <stdio.h>
#include "../include/ESSolver.h"
int mu;
int lambda ;
int main(void)
{
mu = 5;
lambda = 10;
ESSolver es;
es.Init();
es.Optimize();
return 0;
}
"ESSolver.h"
extern int mu;
extern int lambda ;
class ESSolver
{
public:
const static int nVar = 4;
ESSolver();
~ESSolver(void);
void Init();
void Optimize();
int rand_r(int, int);
private:
double (*muPop)[nVar];
double *muSigma;
double (*lambdaPop)[nVar];
double *lambdaSigma;
double (*mulambdaPop)[nVar];
double *mulambdaSigma;
};
"ESSolver.cpp"
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include "../include/ESSolver.h"
using namespace std;
ESSolver::ESSolver()
{
double _muPop [mu][nVar];
muPop = _muPop;
muSigma = new double[mu];
double _lambdaPop [lambda][nVar];
lambdaPop = _lambdaPop;
lambdaSigma = new double[lambda];
double _mulambdaPop [mu+lambda][nVar];
mulambdaPop = _mulambdaPop;
mulambdaSigma = new double[mu+lambda];
return;
}
ESSolver::~ESSolver(void)
{
if (muSigma) delete muSigma;
if (lambdaSigma) delete lambdaSigma;
if (mulambdaSigma) delete mulambdaSigma;
return;
}
void ESSolver::Init()
{
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
muPop[i][j] = 50;
}
}
cout << "\n----- muPop -----\n";
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
cout << muPop[i][j] << " ";
}
cout << endl;
}
}
void ESSolver::Optimize()
{
cout << "\n----- muPop -----\n";
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
cout << muPop[i][j] << " ";
}
cout << endl;
}
}
On running the following code I am getting Output
[^]
As you can see from the image, Matrix is not getting initialized. Can anybody tell me why.
modified 29-Jan-21 21:01pm.
|
|
|
|
|
Quote: double _muPop [mu][nVar];
muPop = _muPop;
_mPop is a temporary array (a local variable), your muPop pointer is goingo to point to garbage.
|
|
|
|
|
Thanks 
modified 29-Jan-21 21:01pm.
|
|
|
|
|
|
How can we find the index of smallest 3 elements in an array. Below code finds the index of largest 3 elements in an array.
#include <vector>
#include <iostream>
using namespace std;
int main()
{
double arr[] = {0.2, 1.0, 0.01, 3.0, 0.002, -1.0, -20};
priority_queue < pair<double, int> > pQueue;
for (int i = 0; i < 7; i++)
{
pQueue.push(pair<double, int>(arr[i], i));
}
int k = 3;
for (int i = 0; i < k; ++i)
{
int ki = pQueue.top().second;
cout << ki << " ";
pQueue.pop();
}
}
modified 29-Jan-21 21:01pm.
|
|
|
|
|
See std::priority_queue - cppreference.com[^]:
Quote: A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<t> would cause the smallest element to appear as the top(). Another solution would be inverting the sign of the items pushed into the queue:
pQueue.push(pair<double, int>(-arr[i], i));
|
|
|
|
|
Where should I put
std::greater<t>
in the code.
modified 29-Jan-21 21:01pm.
|
|
|
|
|
See the link from my answer. It contains example code showing that it must be passed as 3rd template parameter. So you have to pass also the 2nd parameter.
As with any templates, T (uppercase) is a placeholder for the corresponding type which is std::pair<double, int> in your case.
So it must be (untested):
priority_queue < pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>> > pQueue;
|
|
|
|
|
Thanks it worked
modified 29-Jan-21 21:01pm.
|
|
|
|
|
Hi,
I have a value float a=1234.5578
I want to print output as 1234.55.
How to approach this ?
|
|
|
|
|
Read up on printf formatting. Google will find you lots of tutorials and references.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Are you sure you want 1234.55 as output? The number would be rounded to 1234.56.
|
|
|
|
|
Unless you are going to be programming large scale complex mathematical problems you should stay well clear of floating point numbers. There are quite a few issues to be understood which can catch you out. And given this and the question below, your time would be better spent getting hold of a good C++ study guide. Trying to learn by posting questions here will take you far too much time.
|
|
|
|
|
Try here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hi,
I am a newbee to c++.
iostream include definition of cout and cin
But if I write cout without std, It throws error, which should be corrcted to std::cout.
Why so?
|
|
|
|
|
In order to have insight on <iostream > content, feel free to check out the documentation: <iostream> - C++ Reference[^].
std is the namespace[^] of the C++ Standard Library , quoting Wikipedia[^]: "Features of the C++ Standard Library are declared within the std namespace".
In order to use <iostream> objects, e.g. cout you need either to
or
modified 29-Nov-17 6:44am.
|
|
|
|
|
Hi,
But how the std and cout is related?
|
|
|
|
|
cout belongs to std namespace.
There is no global cout , there is just std::cout .
|
|
|
|
|
I would suggest that as a "newbee" to C++ that you should just consider the answer to this question to be 'magic'.
The specifics of how this works this way and why it works is quite complicated and requires not only that you understand quite a bit of basic C++ but also that you understand a bit more about how programming works.
After that then you can go back and look at this yourself and it should then your answer should be immediately clear.
|
|
|
|
|
I have a "scaffolding printout" in main C++ function working just fine in { #ifdef MY_DEBUG ... #endif } debug code block .
I have included a test header file with a class and I have a test function there with same debug code block .
I have to redefine #define MY_DEBUG and re-include
#include <iostream>
using namespace std;
in the added header file to get the debug test printout.
I have never experienced such (odd) behavior, or is that normal? This is my first real crosscompiled CPP app if that matters.
Not looking forward including all this in every header file.
Perhaps it is a compiler / crosscompiler (gcc) options I have missed?
Thanks for any hints / help
Cheers
Vaclav
|
|
|
|
|
Vaclav_Sal wrote:
#include <iostream>
using namespace std; Shouldn't these two statements be in the project's stdafx.h file? Also in that file is where you should be defining the MY_DEBUG directive.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Quote: Shouldn't these two statements be in the project's stdafx.h file?
The OP explicitely mentioned GCC.
|
|
|
|
|
You can't re-include a header file. It is prevented by a guard definition or #pragma once with VS.
We can't help without seeing your code regarding the includes and the definitions, and where the error occurs. But the above two statements (including of iostream and using the namespace) must be before cout or a macro that contains it is used.
To be on the safe side I would always use std::cout within macros. Then you don't need to select the namespace.
|
|
|
|