Hussein
mobile application and games
And it supports mobile apps if I’m not mistaken
Hussein
you might get banned if you haven’t talk to the admin about posting this
Hussein
check the rules ☝️
Andrey
hmm how can i drop my code?
Painter👨
hmm how can i drop my code?
ctrl + c & ctrl + v
Andrey
I have a template class Type Sort(Type pt, int n); How can I check that the pt variable accepts a certain data type, for example, char?
Pavel
I have a template class Type Sort(Type pt, int n); How can I check that the pt variable accepts a certain data type, for example, char?
Do you want to have a separate specialization for specific type? Or you want to prohibit of using other types?
Andrey
Do you want to have a separate specialization for specific type? Or you want to prohibit of using other types?
string proverkaper = ""; proverkaper = typeid(pt).name(); if (proverkaper == "int *" || proverkaper == "double *") I was able to determine the data type this way.
Andrey
I can't figure out how to return an array from a template
Error C2440 return: unable to convert "Type*" to "Type" There is such an error If I do return *pt; Only the first element of the array is returned
Pavel
You can determine a type at compile time instead of runtime, but it would be good to know why you need to know the type
Andrey
Can you show your template declaration?
template <class Type> Type Sort(Type *pt, int n) {} int main() { double* arr_double = new double[count]; int* ar_int_1 = new int[count]; char* ar_char_2 = new char[count]; if (fin) { for (int i = 0; i != count; i++) { fin >> arr_double[i]; ar_int_1[i] = (int)arr_double[i]; } } Sort(ar_char_2, count); }
Andrey
What should Sort return? It doesn't seem that you use the returned value
Type Sort(Type *pt, int n) { string proverkaper = ""; proverkaper = typeid(pt).name(); if (proverkaper == "int *" || proverkaper == "double *") { for (int startIndex = 0; startIndex < n - 1; ++startIndex) { int smallestIndex = startIndex; for (int currentIndex = startIndex + 1; currentIndex < n; ++currentIndex) { if (pt[currentIndex] < pt[smallestIndex]) smallestIndex = currentIndex; } std::swap(pt[startIndex], pt[smallestIndex]); } } else { cout << "Simvoli nelazya razmestit po vosrastaniu esli tolko ne preobrazovat ih"; } return *pt; }
Pavel
I have too many questions to this code, but I would like you to answer at least some of my previous questions first :)
Andrey
sorry return *pt;
Andrey
What should happen if some other type has been passed? Do you want this code to still compile but return runtime error?
If the program receives a char array. There should be a message that it cannot be sorted
Pavel
sorry return *pt;
You can make Type* as return type, but I would rather make it void and not return anything, since you returning the array passed as the argument anyway
Pavel
If the program receives a char array. There should be a message that it cannot be sorted
Why don't you want to make it a compile error? Since the type is always known at compile time you can save time to the person who tries to use your function in a wrong way
Pavel
Yes, I would do that, but the task is)
Ok, there's std::is_same_v<Type, int*>, that I think will return true only if the type is the same
Pavel
Ok, there's std::is_same_v<Type, int*>, that I think will return true only if the type is the same
There are caveats (what if the real type a reference to int* or int * const), so if you want to process these, you may need to decay your type. But for starters something like this should work I think
Hi, can pleas someone help me, I have to lists(with numbers one vector and one int. I will compare each number and wenn numbers are the same, save the same numbers into another int. How can I make it ?
Pavel
Yes, I would do that, but the task is)
Here are some examples https://en.cppreference.com/w/cpp/types/is_same
Anonymous
с++)
Oh, can i ask what was the purpose of putting '*' in the double type?
Andrey
Then change the return type from Type to Type*
im change, but it return only the first element of the array is returned
Andrey
Oh, can i ask what was the purpose of putting '*' in the double type?
double* arr_double = new double[count]; int* ar_int_1 = new int[count]; char* ar_char_2 = new char[count]; if (fin) { for (int i = 0; i != count; i++) { fin >> arr_double[i]; ar_int_1[i] = (int)arr_double[i]; } } fin.close();
Anonymous
Yea what was the purpose of '*'?
Im new in c++ so i dont know what is it
Andrey
Yea what was the purpose of '*'?
So that the array can be converted to another type.
Abiola Jeremiah O.
Im new in c++ so i dont know what is it
It's a pointer to a double value
Pavel
Im new in c++ so i dont know what is it
that's C syntax, not C++ it just C-style array here why * is used in the types in the template in the code above I don't know since the array of doubles is not the same as array of double*
Pavel
im change, but it return only the first element of the array is returned
If you return pointer and the pointer is to the first element it is the same as returning the array
Anonymous
Ohhhh
Andrey
Pavel
And how do I get it back completely? I need to write it to a file
you get it completely, with the same type as you pass so, let's say your Type is double (not double*); double* arr_double = new double[count]; then you call it as double* same_arr_double = Sort(arr_double, count); and the change is to add this * to this line Type* Sort(Type* pt, int n)
Pavel
not sure why you need to return it since it's the same pointer, but that is not for me to judge
Anonymous
Since cpp2x modules are in experimental state and compilers don’t support it completely, is it safe to write my small project on it? “Because, it’s been a long while I’m waiting for it and what if I prepare my codebase for it?”
Anonymous
Since cpp2x modules are in experimental state and compilers don’t support it completely, is it safe to write my small project on it? “Because, it’s been a long while I’m waiting for it and what if I prepare my codebase for it?”
Writing your own modules is well supported by all major compilers. It is only when it comes to using the std module that major compilers are found lacking. If you write and use your own modules and continue using the standard headers then you are safe for now. But most of the people and organisations still use C++14 only with very few having moved to C++17. So if you intend your library to be used by a majority of folks, then using C++20 features is not a good idea. It would be another 2 or 3 years before major organizations even think of adopting C++20
ʙʀʜᴏᴏᴍ ⑇
Hello everyone Is there anything similar to getch() that used for string instead of char!?
ʙʀʜᴏᴏᴍ ⑇
std::getline() maybe?
I want something similar to getch() Because getch doesn't show the character And I want to do the same thing on string or for a line
ʙʀʜᴏᴏᴍ ⑇
My English is not very good so I hope that you understand me correctly 😁
Anonymous
I want something similar to getch() Because getch doesn't show the character And I want to do the same thing on string or for a line
getch() is a non-standard function for DOS based apps which is located in conio.h there's no standard version of it. you can implent/write your own version of it. an example: https://stackoverflow .com/a/7469410
Anonymous
When should you throw on overflow detection And when should you fail in a way that does not throw, on overflow detection
Anonymous
anyone can give me a document or video about C++11 lambda?
Евгений
anyone can give me a document or video about C++11 lambda?
bro, everything in Google. you think is group has something special?😁
Strife
#include <bits/stdc++.h> using namespace std; int max(int a, int b) { return (a > b)? a : b; } int knapSack(int W, int wt[], int val[], int n) { if (n == 0 || W == 0) return 0; if (wt[n-1] > W) return knapSack(W, wt, val, n-1); else return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-1), knapSack(W, wt, val, n-1) ); } int main() { int val[] = {60, 100, 120}; int wt[] = {10, 20, 30}; int W = 50; int n = sizeof(val)/sizeof(val[0]); cout<<knapSack(W, wt, val, n); return 0; }
Strife
Is your English good enough to understand what sizeof means?
Honestly, this is my whole problem if (wt [n-1]> W) return knapSack (W, wt, val, n-1); else return max (val [n-1] + knapSack (W-wt [n-1], wt, val, n-1), knapSack (W, wt, val, n-1));
%Nikita
Hello, guys. Is the .code/.text section of exe file on Windows actual machine code? I am trying to execute machine code in C with casting from array to function: unsigned char arr[] = { here is binary code }; /* here is memory mapping, protecting, etc. */ void (*f)(void) = (void (*) (void)) arr; (*f)(); //actually running it So the question is, will it run if in arr will be data from .code section of executable file, or it can't be done. I have tried to run basic small machine code, it works. But when I run a code, that is readed from exe file ( .text ) section, it doesn't work. Am I reading it wrong maybe?
محمود
Hello, My code cannot read input because of getline() but number of "pair" words is unknown, any suggestions on how to fix it? // test #include <bits/stdc++.h> using namespace std; int main () { int n; cin >> n; // no. of "int" keywords cout << n << "\n"; string s; getline(cin, s); cout << s << "\n"; return 0; } /* in: 3 pair pair int int int */
Sylvester Lim
can anyone tell me why this outputs 0? double x,a,b; a=50,b=60; x= 11/100*a; cout << x;
Anonymous
can anyone tell me why this outputs 0? double x,a,b; a=50,b=60; x= 11/100*a; cout << x;
double x,a,b; a=50,b=60; double n1,n2; n1 = 101; n2 = 100; x= n1/n2*a; cout << x ;
Sylvester Lim
11 / 100 = 0 0 * 50 = 0
Why is 11/100 not 0.11
Sylvester Lim
double x,a,b; a=50,b=60; double n1,n2; n1 = 101; n2 = 100; x= n1/n2*a; cout << x ;
Sorry but I don't get why you turned my values into 101 and 100
%Nikita
Why is 11/100 not 0.11
Because 11 and 100 have type “int” by default. (int) / (int) = (int) If you want to get result type as double you have to cast one or both of operands to “double”: (double) / (int) = (double) (int) / (double) = (double) (double) / (double) = (double) So double x,a,b; a=50,b=60; x= (double)11/100*a; cout << x;
%Nikita
5.5 as result
Talula
can anyone tell me why this outputs 0? double x,a,b; a=50,b=60; x= 11/100*a; cout << x;
double x,a,b; a=50,b=60; x = 11.0 / 100.0 * a; cout << x; You either have to cast it with (double) or add .0 to it to tell the compiler it is a double or float rather than int.