Anonymous
how to return a reversed vector from any method?
try using https://github.com/ericniebler/range-v3 if you are not using C++20 (it provides some extras even when you are using C++20).
Anonymous
yes i'm using c++ 14 😅
use range-v3, eliminate array out-of-bounds from your code*. *Terms and conditions apply. Error elimination is subject to market risks, please read all scheme related documents carefully.
Shahar
Recommended blogs like internalpointers on C C++ stuff?
Francis
std::vector <int> dice{5,1,3,4,1}; unsigned cs[7] = {}; for(auto d:dice) cs[d]++;
Francis
hey guys sorry for the above, my question was what does the loop do ultimately to the elements in the cs array, I could not get the logic but apparently it is a part of an optimized solution for something I am working on
Ravi
hey guys sorry for the above, my question was what does the loop do ultimately to the elements in the cs array, I could not get the logic but apparently it is a part of an optimized solution for something I am working on
Loop is called as for each loop, as the name suggests it iterates each elements in the dice vector i.e. d obtains the value of 5, 1, 3.....at each pass.
Ravi
Loop is called as for each loop, as the name suggests it iterates each elements in the dice vector i.e. d obtains the value of 5, 1, 3.....at each pass.
Now, it will increse the ith index of cs by one, so ultimately, the cs array will become a frequency array which is storing the number of occurrences in the dice array
@𝑺𝒐𝒃𝒌𝒂
#cbookguide
Anonymous
Hi
Francis
Thanks for the save
数学の恋人
guys, yet another doubt, std::uint8_t temp = 0; std::cin >> temp; // say I give it 66 std::cout << temp << "\n"; // outputs only 6 int temp2 = 0; std::cin >> temp2; // again I give 66 std::cout << temp2 << "\n"; // I get 66 why do I get this kind of behaviour?
z
guys, yet another doubt, std::uint8_t temp = 0; std::cin >> temp; // say I give it 66 std::cout << temp << "\n"; // outputs only 6 int temp2 = 0; std::cin >> temp2; // again I give 66 std::cout << temp2 << "\n"; // I get 66 why do I get this kind of behaviour?
It's UB. The value of uninitialized local variable is undefined. Another insanity is that, std::cout treats uint8_t as character, not a number. So it prints the character corresponds to the ASCII code of your uint8_t value.
z
Still, 6 is not what it should print, also I did initialise it to 0, forgot to mention here
I misread the std::cin. Because std::uint8_t is unsigned char. It can only contain single character.
数学の恋人
Still, 6 is not what it should print, also I did initialise it to 0, forgot to mention here
I think it took 6 (the first char) as input and then printing that itself
z
std::cin will treat the input as character to uint8_t, not an integer.
Anonymous
I think it took 6 (the first char) as input and then printing that itself
The value stored in uint8_t in this case would be the ASCII value of '6'.
数学の恋人
Thank you very much
kipkones
Thanks
Vitalii
Sorry, how to read objects of the class from the file? . . . class Domino { string color; public: . . . friend istream& operator>>(istream& is, const Domino& oobj); friend ostream& operator<<(ostream& os, const Domino& obj); }; ostream& operator<<(ostream& os, const Domino& oobj) { os << oobj.color << endl; return os; } istream& operator>>(istream& is, const Domino& oobj) { is >> oobj.color; return is; } int main() { list<Domino> myList; ifstream file("text.txt", ios::in); if (file.good()) { copy(istream_iterator<Domino>(file), istream_iterator<Domino>(), back_insert_iterator<list<Domino>>(myList)); file.close(); } for (auto iter = myList.cbegin(); iter != myList.cend(); ++iter) cout << *iter << endl; }
Vitalii
there're some errors (
Vitalii
ohh, thanks
Mar!o
Your operator>> is taking a const Domino reference. How can you write to a const reference?
return is >> const_cast<Domino&>(oobj).color; 🤣
Anonymous
return is >> const_cast<Domino&>(oobj).color; 🤣
Will be undefined behavior when you write to a const object then 😉
Mar!o
Will be undefined behavior when you write to a const object then 😉
only if the source object is declared as const if you return a const reference to a generally mutable object it's not UB 😜
Mar!o
I said const object. Not const reference
yeah with const objects it's true I've seen someone const casting a string literal and modifying it :(
Mar!o
In C, you dont even need a cast 😉
yeah C has no type safety 😩
Anonymous
wait what?
char* b = "beta" is legal C code
Hanz
Whaaat
Hanz
anyway, that is good
Rahul
#include<stdio.h> #include <stdbool.h> bool prime(int numb) { int i = 2; if(numb == 0 || numb == 1) { return false; } if(numb==i) return true; if(numb%i == 0) { return false; } i++; return prime(numb); } int main() { // int numb; //scanf("%d",&numb); prime(11) ? printf("The given number is prime ") : printf("The given number is not prime"); return 0; }
Rahul
can anyone point out my mistake
Rahul
21012 segmentation fault
Rahul
21012 segmentation fault
this is what i get
Rahul
Rahul
means static int
Anonymous
should i take it static
If you make it static, it will work but you will have to reset it again to 2 for the next prime number check. Let us forget multithreading issues for now. Why should you do it recursively? Cant you use loops? If you have to do recursively, pass in the value of I as a parameter as well. Add a base case. Not efficient but.
Rahul
loop would be good thanks for you advise and your words
Hema Hariharan
this is a basic program how to write and read a file in binary i did everything on my side but the program taking inputs more than what i specified
Anonymous
Hi
Hema Hariharan
//.Kindly help me out #include<stdio.h> #include<stdlib.h> struct employee { int emp_id; char emp_name[5]; int emp_salary; }e; void main() { FILE *fp; fp=fopen("empld","wb"); printf("Enter data:"); printf("\nCrl+z to stop");printf("\n"); while(scanf("%u %s %u",&e.emp_id,e.emp_name,&e.emp_salary)!=EOF) { fwrite(&e,sizeof(e),1,fp); } fclose(fp); printf("\n"); fp=fopen("empld","rb"); while((fread(&e,sizeof(e),1,fp))!=EOF) { printf("Id = %u\nName = %s\nSalary = %u\n",e.emp_id,e.emp_name,e.emp_salary); } fclose(fp); }
stranger
Say I have a string "123" how can I generate all permutations of length n from it, where n can be greater , equal or less then the length of string itself.
Anonymous
Say I have a string "123" how can I generate all permutations of length n from it, where n can be greater , equal or less then the length of string itself.
This is pretty straightforward. Do you know how to generate permutation of length n, given a string of length n? with repetitions?
stranger
Ok, no problem.
JITU
Hey
klimi
(you can just ask you question)
Anonymous
Oh, okay 💫
°Moohaamed°
What's the better to use typing ( using namespace std) or typing (std: : ) ?!
Pavel
What's the better to use typing ( using namespace std) or typing (std: : ) ?!
std:: using namespace is good for code snippets, for real code is better to always use full namespaces (there are exceptions for some long custom namespaces, but even in this case using namespace is better to be used inside as smaller scope as possible (e.g. inside a function), and never in global namespace
Talula
That is not allowed here, please delete
Prem
Wait i need solution for this,,,i have made some afterwards i need some help
Talula
Wait i need solution for this,,,i have made some afterwards i need some help
You're posting a book question, that is your assignment, it'll not be fair if we give you answer for it.
Prem
Ok
Anonymous
What's the better to use typing ( using namespace std) or typing (std: : ) ?!
the using keyword has many more uses. it can be used in a function scope rather than globally. it can be used to bring only the things you need to a scope rather than the whole namespace. void fn() { using std::cout; cout << "Hi!\n"; } Output Hi! void fn() { using v = std::ranges::views; using fmt::format; using std::vector; vector vec{1, 2, 3, 4, 5}; format(std::cout, "{}\n", vec | v::reverse); } Output {5, 4, 3, 2, 1}
Anonymous
Say I have a string "123" how can I generate all permutations of length n from it, where n can be greater , equal or less then the length of string itself.
#include <iostream> #include <string> #include <algorithm> #include <fstream> #include <set> #include <iterator> std::set<std::string> perms; void permutations(std::string inp, int required_length, std::string gen= ""){ if (required_length == 0) { perms.insert(gen); return; } for (int j = 0; j < inp.size(); j++) { std::string tmp = gen + inp[j]; permutations(inp, required_length - 1, tmp); } return; } int main() { std::string inp = "abc"; std::sort(inp.begin(), inp.end()); //for permutations in alphabetical order permutations(inp, 7); //Generate permutations of length 7 std::fstream f("output.txt", std::fstream::out); std::ostream_iterator<std::string> iter(f, "\n"); std::copy(perms.begin(), perms.end(), iter); } This should work. It is slow for permutations greater than 11 in size but I checked the std::next_permutation algorithm which also had almost the same performance (slightly faster) though it doesnt do permutations with repetitions unlike this one. The algorithm in the standard library just does a permutation of the input string and doesnt consider repetitions of characters multiple times (more than the number of times it is present in the input) as a possiblility. The above code treats the input string as a alphabet set and generates all possible strings of the given length from them which is what you wanted I guess. So this should be ok.
Anonymous
Please Suggest me best C++ compiler for android mobile
Anonymous