How to Insert spaces in (cout << ... << args) using fold expressions? - c++17

Given
template<typename ...Types>
void print(Types&& ...args) {
(cout << ... << args);
}
// ....
print(1, 2, 3, 4); // prints 1234
How to add spaces so we get 1 2 3 4?
Update:
Correct answer:
((std::cout << args << ' ') , ...);

The usual workaround is to fold over the comma operator instead, though the simplistic approach will leave a trailing space:
((std::cout << args << ' '), ...);
Changing it to avoid the trailing space is left as an exercise for the reader.

Related

lua_pushcclosure - does not put values on the stack

Why lua_pushcclosure does not work ?
static int my_callback(lua_State* L)
{
std::cout << "my_callback" << std::endl;
std::cout << "PARAM1:" << lua_tostring(L, lua_upvalueindex(1)) << std::endl; //empty
std::cout << "PARAM2:" << lua_tostring(L, lua_upvalueindex(2)) << std::endl; //empty
return 0;
}
lua_pushstring(L, "param1");
lua_pushstring(L, "param2");
lua_pushcclosure(L, my_callback, 2);
lua_getfield(L, index, "Set_Callback");
lua_pushcfunction(L, my_callback);
int status_lua_pcall = lua_pcall(L, 1, 0, 0);
Why lua_pushcclosure does not work ?
After all, I set parameters 1 and 2 for the my_callback function. The my_callback function is called, but parameters 1 and 2 are empty.
Please explain me - how I can make a closure from - param1, param2, Set_callback, my_callback and lua_pushcclosure?
Or how do I then pass additional parameters to the my_callback function, which is called by the Set_callback method??
Here's how it works on pure Lua:
function my_callbac_func(param1, param2)
--some code...
end
param_1 = "Hello1"
param_2 = "Hello2"
my_table = func_Lua_app() -- The function returns a table in which there is a Set_callback method that needs to be called and passed as a parameter to the my_callback function itself.
my_table:Set_callback(function()my_callbac(param1, param2) end)
That is, I need to make an analog of this Lua code, only on the Lua C api.
Good, I try like this:
static int my_callback(lua_State* L)
{
std::cout << "my_callback" << std::endl;
std::cout << "PARAM1:" << lua_tostring(L, lua_upvalueindex(1)) << std::endl; //empty
std::cout << "PARAM2:" << lua_tostring(L, lua_upvalueindex(2)) << std::endl; //empty
return 0;
}
lua_getfield(L, index, "Set_Callback");
lua_pushstring(L, "param1");
lua_pushstring(L, "param2");
lua_pushcclosure(L, my_callback, 2);
int status_lua_pcall = lua_pcall(L, 3, 0, 0);
It doesn't work that way either.
Here is how each call affects the stack:
// stack: empty
lua_pushstring(L, "param1");
// stack: "param1"
lua_pushstring(L, "param2");
// stack: "param1", "param2"
// ^^^^^^^^^^^^^^^^^^
// upvalues
lua_pushcclosure(L, my_callback, 2);
// stack: the closure with upvalues
lua_getfield(L, index, "Set_Callback");
// stack: the closure with upvalues, the Set_Callback function
lua_pushcfunction(L, my_callback);
// stack: the closure with upvalues, the Set_Callback function, the cfunction without upvalues
// ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// what gets called parameter
int status_lua_pcall = lua_pcall(L, 1, 0, 0);
// stack: the closure with upvalues
You made the closure with upvalues and didn't use it. And then you called Set_Callback and the parameter you gave to Set_Callback was a different cfunction which had no upvalues. The one with upvalues is still on the stack, waiting to be used for something.
You need the closure with upvalues to be on the top of the stack when you get to lua_pcall, so that the parameter is the closure with upvalues.

Lua pattern matching problem with escaped letter

I've already had a rule that \ should be replaced with \\\\
, so the existed code is
string.gsub(s, '\\', '\\\\\\\\')
but there is some data that should not be converted, such as abc\"cba, which will be replaced with abc\\\\"cba.
How can I constraint that only \ followed without " can be replaced, such like
'abc\abc' -> 'abc\\\\abc'
'abc\"abc' -> 'abc\"abc'
I have used patterns like \\[^\"]- and \\[^\"]+- but none of them works.
Thanks
You can use
string.gsub((s .. ' '), '\\([^"])', '\\\\\\\\%1'):sub(1, -2)
See the online demo:
local s = [[abc\abc abc\"abc\]];
s = string.gsub((s .. ' '), '\\([^"])', '\\\\\\\\%1'):sub(1, -2)
print( s );
-- abc\\\\abc abc\"abc\\\\
Notes:
\\([^"]) - matches two chars, a \ and then any one char other than a " char (that is captured into Group 1)
\\\\\\\\%1 - replacement pattern that replaces each match with 4 backslashes and the value captured in Group 1
(s .. ' ') - a space is appended at the end of the input string so that the pattern could consume a char other than a " char
:sub(1, -2) - removes the last "technical" space that was added.

I need some help getting the right output stream format in decimal in C++17

I made this little currency converter program that convert dollar to franc, but when I put an amount of, for example $2000 and more, I do not have the correct format, I have this: 1.15165e+006.
I want the entire decimal amount.
Thanks
Convert USD to Franc CFA
#include <iostream>
using namespace std;
int main()
{
const double cfa_per_usd {575.825};
cout <<"**********Welcome to the USD to Franc CFA Converter************" << endl;
double cfa {0.0};
cout <<"\nEnter value in USD: ";
double dollar {0};
cin >> dollar;
cfa = dollar * cfa_per_usd;
cout << dollar <<" Dollar(s) is equivalent to " << cfa << " Francs CFA" <<endl;
return 0;
}
I found a partial answer to my problem, I have added the library , then added "fixed" key word and the "setprecision()":
cout << dollar <<" Dollar(s) is equivalent to " << fixed << setprecision(2) << cfa << " Francs CFA" <
But I realized that I only have zeros after the decimal point: just 2 “.00”, I changed the constant to 576.212 for a dollar, so if I convert $2000 I should have 1,1512,424.54 francs, but I just have 1152424.00 francs, the .54 is not there, any idea how to fix it?
I found a partial answer to my problem, I have added the library, then add "fixed" key word and the "setprecision()":
cout << dollar <<" Dollar(s) is equivalent to " << fixed << setprecision(3) << cfa << " Francs CFA" <<endl;
But I realized that I only have zeros after the decimal point: just 2 “.00”, I changed the constant to 576.212 for a dollar, so if I convert $2000 I should have 1,1512,424.54 francs, but I just have 1152424.00 francs, the .54 is not there, any idea how to fix it?

Issue with cv::Mat::zeros initialization

my problem is just astonishing. This is the code
#define NCHANNEL 3
#define NFRAME 100
Mat RR = Mat::zeros(NCHANNEL, NFRAME-1, CV_64FC1);
double *p_0 = RR.ptr<double>(0);
double *p_1 = RR.ptr<double>(1);
double *p_2 = RR.ptr<double>(2);
cout<< p_0[NFRAME-1] << endl << p_1[NFRAME-1] << endl << p_2[NFRAME-1] << endl;
And the output is: 0 0 -6.27744e+066 .
Where is that awful number come from? it seems I'm printing a pointer or something rough in memory. (uh, 0 is the value of all other elements, of course).
You are accessing after the last element of Mat. If you use NFRAME-1 for initialization then the last element has NFRAME-2 index.

setw() - adjustfield (left, right or internal)

I am going though the string functions doing tests to learn them (I am a newbie programmer)
Anyway, I am currently looking at setw() but I seam to not understand it... I think I understand the basic use and the use of setfil
here is my test code
http://ideone.com/czAXH
Anyway the cplusplus website says.. "format flag adjustfield (left, right or internal)" but doesn't say how to use this?
I assume this means I can do the above code but place the "spacing" after the word instead of before it..
How do I do that?
std::cout << std::left
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
std::cout << std::internal
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
std::cout << std::right
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
Outputs:
[1 ,-100 ]
[ 1,- 100]
[ 1, -100]

Resources