lundi 27 juillet 2020

muti index container match by string

i wanna use string as a key for muti index, but i find that examples always show the demo with std::string, if I want to use const char*, what should I do to make it nocase-matched?

typedef struct  {
    std::string name;
}student;
 
struct by_name{};
 
using namespace boost::multi_index;
 
typedef
boost::multi_index_container<
        student,
        indexed_by<
                ordered_unique<tag< by_name >, member<student, std::string, &student::name>>,
                
        >
> StuContainer;

Why is this sum of floats rounding?

I've written a simple C++ macro for use with the CERN ROOT data analysis framework. It takes in a data file (essentially a spreadsheet) with day, hour, minute, second, and sub-second columns. My goal here is to convert that data into a file of timestamps, in seconds. Everything works fine up to adding the sub seconds. For some reason, it seems to be rounding the result. For example, one timestamp is:

804267 + 0.5606663227081298828125 = 804267.5625

Another is:

155034 + 0.0958281949540391919293 = 155034

ROOT::RDataFrame d("N", "mydata1.root");
TFile *rfout = new TFile("./mydata2.root", "recreate");
TNtuple *N = new TNtuple("N","N","TIMESTAMP");

vector<float> timestamp;
int i;

d.Foreach([&](float day){timestamp.push_back(day*86400.00);},{"day"});
d.Foreach([&](float hr){timestamp.at(i) = timestamp.at(i)+(hr*3600);i++;},{"hr"});
i=0;
d.Foreach([&](float min){timestamp.at(i) = timestamp.at(i)+(min*60);i++;},{"min"});
i=0;
d.Foreach([&](float sec){timestamp.at(i) = timestamp.at(i)+sec;i++;},{"sec"});

i=0;
float j;
d.Foreach([&](float sub){
    while(sub > 1){
        sub = sub/10;
    }
    j = sub + timestamp.at(i);
    N->Fill(j);
    std::cout << std::setprecision(100) << j << " " << sub <<std::endl;
    i++;
},{"subsecond"});

rfout->Write();
rfout->Close();
abort();

}`

How to store and call a variable argument function and a vector of values

In Python, it is possible to store a function pointer with different number of arguments and store the arguments in a list and then unpack the list and call that function like:

def Func(x1, x2):
   return x1+x2
ArgList = [1.2, 3.22]
MyClass.Store(Func)
MyClass.FuncPtr(*ArgList)

Is it possible in c++ to do similar thing? For example, store the function with variable number of inputs and the values in a std::vector and call function pointer by that vector? I don’t want to define argument list as vector.

dimanche 26 juillet 2020

Cursor remains at the end when I try to give user input in getline

The following is the the code for asking a name and then returning a statement which has your name in it.

#include <iostream>
#include <string>

int main(){
    std::string name;
    std::cout << "What is your name:\n";
    std::getline(std::cin, name);
    std::cout << "Your name is " << name << ".\n";
    return 0;
}

When I run this program and type the name, sometimes you type the input in wrong manner and hence you use the arrow keys to go left or right. However, when I use the left arrow key, instead of the cursor going to the left, ^[[D is printed. And when I use the right arrow key, ^[[C is printed.

My question is how can I avoid this and actually move the cursor to the right or left.

Why std::fetch_add returns the old value?

What are the design purposes or technical restrictions that make the return value of std::fetch_add is the one before changed?

C++ how to change pointer type of a pointer to a vector of struct

I have a vector of struct that I need to change like so:

struct Something {
    int i = 0;
}

std::vector<Something>* vec;

I need to change the struct Something to Class::Something.

std::vector<class::Something>* vec;

I have tried reinterpret_cast but it doesn't work. I need to know if that is possible because im lost.

Btw the struct Something is copied in two .h files, one is in a class, the other is declared outside a class because it need too be exported out of a dll.

What happens when std::move is called on a rvalue reference?

Consider the following c++ programs:

string construct(string&& s) {
    // Passing a r-value reference as an argument to the assignment operator
    string constructed = s;
    return constructed;
}

int main() {
    string original = "Hello";
    string temp1 = construct(std::move(original));

    printf("%s\n", original.c_str()); // Prints "Hello", so original has not changed
    return 0;
}

Now a small change that I perform is calling std::move on an r-value reference argument:

string constructWithMove(string&& s) {
    // Passing a cast to an r-value reference using r-value reference as an argument.
    string constructed = std::move(s);
    return constructed;
}

int main() {
    string original = "Hello";
    string temp = constructWithMove(std::move(original));

    printf("%s\n", original.c_str()); // Prints "", original is set to the empty string, WHY???
    return 0;
} 

So it looks like casting an r-value reference to an r-value reference induces something peculiar. Why in the first case the original string retains its value but not in the second?