I'm quite new in C++ programming. So I tried to define an alias and change it later, but I'm not sure if it is possible and (if it's possible) how to do it:
using Alpha = Alphabet::DNA
//I don' need the Alpha here, but I have to define it before the scope,
//because afaik if I define it inside the scope,
//it'll be lost outside the scope
for(int i = 0 ; i < argc ; ++i){
if(argv[i] == "-d"){
Alpha = Alphabet::DNA;
}else if(argv[i] == "-r"){
Alpha = Alphabet::RNA;
}
}
Sequence<Alpha> seq;
I need to do this because I only know from the arguments which Alpha I have to use. Alphabet is already a namespace and DNA and RNA are "subtypes" of it.
Sequence is just a template class representing the sequence of molecules fro DNA or RNA.
Thanks for your help
The answer is NO.
For every type (alias or not), cpp compiler have to determine its "real type" in compile time.
For instance,
constexpr int N = 3;
using FixedBitset = std::bitset<N>;
is valid. While
int N = 3;
using FixedBitset = std::bitset<N>;
is definitely invalid because the value N cannot be checked out in compile time.
Related
I am trying to create a simple loop that calls data from sequential channels. I have channels numbered 1-8 (named qL1 - qL8) and need to call the qL*->Draw(); command for all of them.
Is there a way to use a loop to continually update the number in the command? I believe the equivalent code in a c shell would be:
for {set i 1} {$i <= 8} {incr i} {
qL$i->Draw();
}
As stated in the title, I am trying to write a macro for ROOT. Thanks.
put everything into an array/vector
well, you can fill your channels (I assume TH1 here, but it should work similarly for TGraph, TTree, etc) into an array/vector first, since they are pointers anyhow:
TH1* qL[9]; // nine elements to have indices matching
qL[1] = qL1;
qL[2] = qL2;
qL[3] = qL3;
qL[4] = qL4;
qL[5] = qL5;
qL[6] = qL6;
qL[7] = qL7;
qL[8] = qL8;
for (int i = 1 ; i <= 8 ; i++) { qL[i]->Draw() ; }
though that probably doesn't gain you much.
use ROOT's directory mechanism and use string manipulation
What might be better suited is to work with root's directory management (if you anyhow get your channels from an input file, that should be a straight forward solution):
for (int i = 1 ; i <= 8 ; i++) {
TH1* localhist = (TH1*) gDirectory->Get(Form("qL%d",i));
localhist->Draw();
}
Here, Form(...) is a cstring and the syntax for Form is the same as for printf (google is your friend). And Get looks for an object of which the name is the cstring provided. (be aware of the room for confusion in TH1* variablename = new TH1F( NAME, TITLE , ...), what matters is the NAME and not the variablename)
preprocessor macros
you can use a preprocessor (e.g. boost) to concatenate the string part (qL) of your variable with a number and use this in a preprocessor macro which takes the number as argument.
#include <boost/preprocessor/cat.hpp>
#define qL(i) BOOST_PP_CAT(qL, i)
TH1* qL1 = new TH1F("test","test",1,0,1);
qL(1)->GetName();
The problem with this is, that the argument must be known at compile time, if you put this in a for loop, you'll get errors because qLi is not known.
Which can be done in preprocessor, though not sure if this will greatly work out on the long run
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#define HHHHH(z,n,zz) BOOST_PP_CAT(qL, n) zz
BOOST_PP_REPEAT_FROM_TO(1,3,HHHHH,->Draw();)
No. If only your channels names were an array, e.g. qL[N], where N=0, 1, ..., 7, that would be possible with something like
for (Int_t i=0; i<8; i++)
{
qL[i]->Draw();
}
I have the following:
uint16_t Hitcount[64]; //64-byte array buffer
uint16_t Br;
int StdWidth = 38;
int widthpad = 8;
int W;
uint16_t blocks;
if (W <= (StdWidth + widthpad) && W >= (StdWidth - widthpad) && blocks == 1) {
Hitcount += Br;
}
My goal is to appenf "Br" to the array "Hitcount" if "W" is within a certain range. However, "Hitcount" is indicating the error "Expression must be a modifiable lvalue". Don't I have my data types and everything in order?
Apologies if this is too close to other questions that have been posted. I looked at them but could not relate them to this scenario with my limited knowledge.
Hitcount += Br;
You cannot add value to a C style array like that. You either need to maintain elements count and add a value like this:
Hitcount[count++] = Br;
or you better use std::vector and add element by calling push_back:
std::vector<uint16_t> Hitcount;
// code skipped
Hitcount.push_back( Br );
Plain C/C++ arrays cannot be extended the way string objects can. Look into using std::vector.
I'm working on a GA and seem to be having problems with the tournament selection. I think this is due to the fact that I'm not comparing what I want to compare (in terms of fitness values)
srand(static_cast <unsigned> (time(0)));
population Pop;
vector<population> popvector;
vector<population> survivors;
population *ptrP;
for (int i = 0; i <= 102; i++)
{
ptrP = new population;
ptrP->generatefit;
ptrP->findfit;
popvector.push_back(*ptrP);
//include finding the persons "overall". WIP
}
cout << "The fit values of the population are listed here: " << endl;
vector<population> ::iterator it; //iterator to print everything in the vector
for (it = popvector.begin(); it != popvector.end(); ++it)
{
it->printinfo();
}
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); // generate a seed for the shuffle process of the vector.
cout << "Beggining selection process" << endl;
shuffle(popvector.begin(), popvector.end(), std::default_random_engine(seed));
//Shuffling done to randomize the parents I will be taking.
// I also want want to pick consecutive parents
for (int i = 0; i <= 102; i = i + 3)
{
if (popvector[i] >= popvector[i++]);
}
}
Now what I think my problem is, is that when im trying to compare the Overall values (Not found yet, working on how to properly model them to give me accurate Overall fitness values) I'm not comparing what I should be.
I'm thinking that once I find the persons "Overall" I should store it in a Float vector and proceed from there, but I'm unsure if this is the right way to proceed if I wish to create a new "parent" pool, since (I think) the "parent pool" has to be part of my population class.
Any feedback is appreciated.
srand(static_cast <unsigned> (time(0)));
This is useless: you're calling std::shuffle in a form not based on std::rand:
shuffle(popvector.begin(), popvector.end(), std::default_random_engine(seed));
If somewhere else in the program you need to generate random numbers, do it via functions / distributions / engines in random pseudo-random number generation library (do not use std::rand).
Also consider that, for debugging purpose, you should have a way to initialize the random engine with a fixed seed (debug needs repeatable results).
for (int i = 0; i <= 102; i++)
Do not use magic numbers.
Why 102? If it's the population size, store it in a constant / variable (populationSize?), document the variable use and "enjoy" the fact that when you need to change the value you haven't to remember the locations where it's used (just in this simple snippet there are two distinct use points).
Also consider that the population size is one of those parameters you need to change quite often in GA.
ptrP = new population;
ptrP->generatefit;
ptrP->findfit;
popvector.push_back(*ptrP);
Absolutely consider Sam Varshavchik's and paddy's remarks.
for (int i = 0; i <= 102; i = i + 3)
{
if (popvector[i] >= popvector[i++]);
// ...
Generally it's not a good practice to change the index variable inside the body of a for loop (in some languages, not C / C++, the loop variable is immutable within the scope of the loop body).
Here you also have an undefined behaviour:
popvector[i] >= popvector[i++]
is equivalent to
operator>=(popvector[i], popvector[i++])
The order that function parameters are evaluated is unspecified. So you may have:
auto a = popvector[i];
auto b = popvector[i++];
operator>=(a, b); // i.e. popvector[i] >= popvector[i]
or
auto b = popvector[i++];
auto a = popvector[i];
operator>=(a, b); // i.e. popvector[i + 1] >= popvector[i]
Both cases are wrong.
In the first case you're comparing the same elements and the expression is always true.
In the second case the comparison probably is the opposite of what you were thinking.
Take a look at:
Undefined behavior and sequence points
What are all the common undefined behaviours that a C++ programmer should know about?
and always compile source code with -Wall -Wextra (or their equivalent).
I'm not sure to correctly understand the role of the class population. It may be that the name is misleading.
Other questions / answers you could find interesting:
C++: "std::endl" vs "\n"
http://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/ (the section about premature pessimization)
I am almost done with my code except I need help on two thing. Here is my code: Code. For the function below, I am trying to make it so that I can use the input of "n" to initialize my array, myBits, instead of a constant, which is currently 5.
My Other question is right below that. I am trying to switch all of the right most bits to "true". I wrote the for loop in "/* .....*/" but it doesn't seem to be working. Right above it, I do it long ways for C(5,4) ....(myBit[0] = myBit[1]....etc...... (I am using this to find r-combinations of strings).... and it seems to work. Any help would be appreciated!!
void nCombination(const vector<string> &Vect, int n, int r){
bool myBits[5] = { false }; // everything is false now
myBits[1] = myBits[2] = myBits[3] = myBits[4] = true;
/* for(int b = n - r - 1; b = n - 1; b++){
myBits[b] = true; // I am trying to set the r rightmost bits to true
}
*/
do // start combination generator
{
printVector(Vect, myBits, n);
} while (next_permutation(myBits, myBits + n)); // change the bit pattern
}
These are called variable length arrays (or VLAs for short) and they are not a feature of standard C++. This is because we already have arrays that can change their length how ever they want: std::vector. Use that instead of an array and it will work.
Use std::vector<bool>:
std::vector<bool> myBits(n, false);
Then you have to change your while statement:
while (next_permutation(myBits.begin(), myBits.end()));
You will also have to change your printVector function to take a vector<bool>& as the second argument (you won't need the last argument, n, since a vector knows its own size by utilizing the vector::size() function).
As to your program: If you're attempting to get the combination of n things taken r at a time, you will need to write a loop that initializes the last right r bools to true instead of hard-coding the rightmost 4 entries.
int count = 1;
for (size_t i = n-1; i >= 0 && count <= r; --i, ++count)
myBits[i] = true;
Also, you should return immediately from the function if r is 0.
Hey all, I'm trying to write a sort function but am having trouble figuring out how to initialize a value, and making this function work as a generic template. The sort works by:
Find a pair =(ii,jj)= with a minimum value = ii+jj = such at A[ii]>A[jj]
If such a pair exists, then
swap A[ii] and A[jj] else
break;
The function I have written is as follows:
template <typename T>
void sort(T *A, int size)
{
T min =453;
T temp=0;
bool swapper = false;
int index1 = 0, index2 = 0;
for (int ii = 0; ii < size-1; ii++){
for (int jj = ii + 1; jj < size; jj++){
if((min >= (A[ii]+A[jj])) && (A[ii] > A[jj])){
min = (A[ii]+A[jj]);
index1 = ii;
index2 = jj;
swapper = true;
}
}
}
if (!swapper)
return;
else
{
temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
sort(A,size);
}
}
This function will successfully sort an array of integers, but not an array of chars. I do not know how to properly initialize the min value for the start of the comparison. I tried initializing the value by simply adding the first two elements of the array together (min = A[0] + A[1]), but it looks to me like for this algorithm it will fail. I know this is sort of a strange type of sort, but it is practice for a test, so thanks for any input.
most likely reason it fails, is because char = 453 does not produce 453 but rather different number, depending what char is (signed versus unsigned). your immediate solution would be to use numerical_limits, http://www.cplusplus.com/reference/std/limits/numeric_limits/
you may also need to think about design, because char has small range, you are likely to overflow often when adding two chars.
The maximum value of any type is std::numeric_limits<T>::max(). It's defined in <limits>.
Also, consider a redesign. This is not a good algorithm. And I would make sure I knew what I was doing before calling my sort function recursively.
I haven't put too much time reading your algorithm, but as an alternative to std::numeric_limits, you can use the initial element in your array as the initial minimum value. Then you don't have to worry about what happens if you call the function with a class that doesn't specialize std::numeric_limits, and thus can't report a maximum value.