|
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.
|
|
|
|
|
Impossible to be sure what you are doing. Please show your code and explain what errors occur.
|
|
|
|
|
OK, here is readers digest for clarification.
The class TESTCLASS1 header is included in HF_VNA_CROSS_CPP.cpp.
This file first code line is
#define MY_DEBUG
and the cout code works in main OK
BUT not in TESTCLASS1.h UNTIL I add / redefine MY_DEBUG.
Without the
#include <iostream>
using namespace std;
added to TESTCLASS1.h
When I add ONLY #define MY_DEBUG
I get the cout error.
Basically I have two issues
1. why do I need to redefine MY_DEBUG in TESTCLASS1.h
2. why do I need to add #include <iostream>
using namespace std; to get cout to work in added class
and a minor request for help with
should I #include "TESTCLASS1.cpp" in main file instead of "header" #include "TESTCLASS1.h" file?
<pre lang="c++">//============================================================================
// Name : HF_VNA_CROSS_CPP.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#define MY_DEBUG
#include <iostream>
using namespace std;
#include <math.h> // rint round up math
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <math.h> // rint round up math
#include "/usr/local/include/wiringPiSPI.h"
#include <math.h> // rint round up math libraray lm
#include "flash_DTR.h" // flash DTR via USB port
/* CCC temp out
#include "AD9851.h" // AD9851 via SPI / I2C
#include "LCD_SPI.h" // LCD via SPI
*/
#include "MY_wiringPi.h" // GPIO for RPi
//#include "LCD.h" // TEST CLASS
//#include "TESTCLASS.h" //
//#include "LCDSPI.h" // NO SPACE
#include "TESTCLASS1.h"
int main() {
cout << "NEW *** COPY 2 TEST output HF_VNA_CROSS_CPP CPP Managed project" << endl; // prints CPP Managed project
//temp bypass CCC
// class test block start
{
#ifdef MY_DEBUG
cout << " process code block START"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
//LCD__SPI lcd; // double space INSTATIATE CLASS
//lcd.TestFunction();
TEST_CLASS_1 test_class;
test_class.TestFunction();
}
// class test block end
// */
return 43;
#ifdef MY_DEBUG
cout << "*** TRACE file " << __FILE__<<" line " <<__LINE__<< " function "<< __FUNCTION__ << endl;
//return 42;
#endif
flash();
// process code block START
{
#ifdef MY_DEBUG
cout << " process code block START"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
// process code block END
#ifdef MY_DEBUG
cout << " process code block END"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
}
/* temp bypass CCC
// class test block start
{
LCD lcd; // INSTATIATE CLASS
//lcd.TestFunction(10);
}
// class test block end
*/
}
/*
* TESTCLASS1.h
*
* Created on: Nov 28, 2017
* Author: os64
*/
#ifndef TESTCLASS1_H_
#define TESTCLASS1_H_
#define MY_DEBUG
namespace std {
class TEST_CLASS_1 {
public:
TEST_CLASS_1();
virtual ~TEST_CLASS_1();
int TestFunction(void);
};
} /* namespace std */
#endif /* TESTCLASS1_H_ */
08:42:36 **** Incremental Build of configuration Debug for project HF_VNA_CROSS_CPP2 ****
make all
Building file: ../src/TESTCLASS1.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/TESTCLASS1.d" -MT"src/TESTCLASS1.o" -o "src/TESTCLASS1.o" "../src/TESTCLASS1.cpp"
../src/TESTCLASS1.cpp: In member function ‘int std::TEST_CLASS_1::TestFunction()’:
../src/TESTCLASS1.cpp:26:4: error: ‘cout’ was not declared in this scope
cout << " process code block START"<< endl;
^
../src/TESTCLASS1.cpp:26:43: error: ‘endl’ was not declared in this scope
cout << " process code block START"<< endl;
^
make: *** [src/TESTCLASS1.o] Error 1
src/subdir.mk:39: recipe for target 'src/TESTCLASS1.o' failed
08:42:36 Build Finished (took 304ms)
</pre>
|
|
|
|
|
Why do you #define MY_DEBUG in that header file since it serves no purpose? You should write code that depends on #define statements,but only use the #define at the beginning of your source code. Something like (note the correct use of <pre> tags):
#if defined(SOME_TAG)
#endif
#define SOME_TAG // or not as the case may be
#include some .h file
#if defined(SOME_TAG)
#endif
|
|
|
|