« first day (250 days earlier)      last day (2609 days later) » 

3:39 PM
can i know how to define custom key in cpp object.
obj["name"] = "name";
Like JavaScript do
 
user1804599
You can't.
 
user1804599
You need to declare it in the class.
 
user1804599
If you want to pair an existing object with some other data, use std::pair or std::tuple.
 
ok
 
7 messages moved from Lounge<C++>
 
nwp
4:00 PM
@DININDU there are some things you can do, such as keeping a template<class T> std::map<std::string, T> to store objects, but they don't really live inside obj and you need to know the type of the thing you want and deallocation is annoying
 
can't resist laughing at JS calling "hash maps" "objects"
well, that's how they approach the problem
 
nwp
alternatively you can restrict the allowed and required operations and do some type-erasure
 
if you want a key-value mapping, then yes, std::map or std::unordered_map is the right answer
 
class person {
	public:
	    string name;
	    int age;
};

person user;

template <class T>
T GET(T value) {
	std::cin >> user.value;
	return user.value;
}

GET(name);
this is what im really doing here
 
looks fairly pointless
 
nwp
4:06 PM
you could get something like this to work through GET(user, &Person::name)
 
as a pointer?
 
nwp
I don't think you can get rid of the type repetition without macro hackery
pointer to member, yes
 
ok ill try
 
better tell what is the problem you're trying to solve
 
nwp
wait, GET(user.name) or just std::cin >> user.name; would make so much more sense
 
4:10 PM
The person class should be dynamic. So which should be create custom key where i want
var obj = {};

obj["name"] = "name";
obj["age"] = "age";
like that
 
and what, others will just assign whatever they want in the other fields
 
nwp
why not do std::cin >> obj["name"]?
 
also, again, why do want such dynamic behaviour
 
nwp
because it makes the transition from JS easier :P
 
Yeah, :) im new to cpp and im expert in js
:)
so im trying to do js stuff here :)
with cpp
 
nwp
4:15 PM
c++ is pretty bad at being JS
 
well, ok. just be aware that JS approaches to the problem will make your code non-idiomatic C++
 
Just understand it is difficult with cpp
 
nwp
you can get something like that working, but you will lose the advantages of c++ and at that point you might as well stick to JS
 
:) maybe
 
nwp
std::map<std::string, std::any> obj; combined with some visitor application might implement the desired behavior
 
4:19 PM
I see you're handing the rope now
 
Hmm...My new project doing with node.js which needs some c++ addons. so thats why im learning c++.ohh god it seems much difficult
 
nwp
Maybe the tour can bring you up to speed with minimal time investment, once you accept that the c++ way of things works better in c++ than the js way of things.
 
@nwp Thanks you so much :). Ill take the tour now
 
5:12 PM
what improvements can I make?
 
nwp
int trials_per_percent = trials / 100; loses precision
 
I use that to print dots to tell me how far the program has progressed.
I don't want it to be exact.
I just need to know if the program will finish execution within a minute.
Those dots will tell that.
 
nwp
you forgot error handling for cin
 
:|
added
 
nwp
you should never use std::mersenne_twister_engine directly, use std::mt19937 or std::mt19937_64 instead
 
5:17 PM
I thought I'd keep it more portable
can change the data type to handle large numbers by changing one typedef
that was the goal
well, that won't work with the existing code yet
but I'll add appropriate replacements for other template parameters of m_t_e
nvm I think I am overthinking
mt19937 will always use uint_fast32_t?
 
nwp
you are supposed to use std::uniform_int_distribution to get the proper range
 
My range is from 0 to UINT 32 MAX
so I don't need a uniuform distribution?
I need a range of 0 to INFINITY. The best I can get is 0 to INT MAX.
 
nwp
@YashasSamaga true, you don't
 
The RNG gives me in that range by default.
 
nwp
gdc could be made non-recursive, but it probably doesn't matter
 
5:24 PM
you were suggesting a loop, right?
 
nwp
yes
 
Hi. I am a college student and I have an assignment in which I have to print a 2d array of 10,000 rows and 10,000 columns in row major order and then in column major order. I then have to calculate the program execution time to prove that iterating through row major order is faster than through column major order. But while compiling I am getting "Segmentation fault: 11" error. Following is the link to the code: gist.github.com/shreezan123/88b6a1a5b6d6b546aea579d21f6ed748 .
 
using recursive function clearly shows the algorithm
that's one advantage
but I guess the stack would grow too much for very large numbers :/
maybe I should use a loop
 
nwp
@ShrijanAryal the stack is typically limited to about 1MB, you are creating a 400MB array on it
you should put it on the heap
 
@ShrijanAryal Use std::vector
 
5:37 PM
that is how it is now
 
nwp
apparently division by 0 is UB for doubles
@YashasSamaga only big problems left that I can see are the lack of error checking for cin and division by 0 for 0 trials
 
it's UB for ints too
 
nwp
for ints you immediately get a crash, but doubles give you nans, but apparently that is not guaranteed
 
it's not crash, it's UB. if the rest of your code will get optimized out, well then
return x/x; most likely will be optimized out to return 1; and you won't get a crash
 
6:29 PM
what c++ book should I read after accelerated c++
 
 
1 hour later…
7:29 PM
@exitcode For learning more C++ or what?
Probably you should read Stroustrup's book, The C++ Programming Language. I also like his so-called "beginner's book", Programming Principles and Practice in C++.
 

« first day (250 days earlier)      last day (2609 days later) »