« first day (887 days earlier)      last day (1970 days later) » 

10:05 AM
Hey
I have a
struct data{
std::shared_ptr<QVector<std::shared_ptr<data> > data;
}
How can I properly copy that object? if I want to recursively copy it I mean, like... deep copy?
 
nwp
Deep-copy all the shared_ptrs with std::make_shared<T>(*sp);.
 
hmmm
so I have to write recursive function, to copy the vector and create copies myself?
overload assignment operator = as well as constructor if I am to pass the same object as argument and then recursively re-create the shared_ptr items
 
nwp
It's not recursion, just a loop. But basically yes.
 
I take if I want to deepcopy an object that is a result of few inheritance levels, I take i have to virtualize the function and create clone for each subclass?
 
nwp
Pretty much.
 
10:20 AM
@nwp hmm, well data is a vector of data, so if I am to deepcopy it, I have to go over each item in vector, and deepcopy that and its children to have full deep copy hierarchy ?
wow
 
nwp
Although I wonder why you even want to make a deep copy. No way to avoid that?
 
so copying objects with * are quite ..."tricky" then
well if I want to duplicate an object
like folder in windows
that all needs deepcopying dont it
wow windows sure got a lot to do when copying stuff o.o
well he just copies bytes, but hmm
 
nwp
Folders are not kept in shared pointers.
 
yea
different stuff, nvm
but I see that copying a inherited object
is tricky
I take on each inherited clone()
I would call obj *clone = base::clone();
clone->x = new x(x)
and so on so on ?
so I would traverse all the way to top of the base class, and then slowly from to bottom clone all objects myself
 
@Dariusz are you really, really, REALLY sure you need to deep copy all that?
 
10:23 AM
yes
I need 2 copies of 1 object, each of them has dynamically allocated stuff
 
nwp
@Dariusz You don't need to traverse anything. You call base->clone() and that's it.
And you are not supposed to use new. Use std::make_unique instead.
 
if I call base clone, that would return *obj with cloned values only for base class, I then have to add to it inherited class data no ?
its like destructor
each inherited class has a destructor for newly allocated members
 
nwp
No. virtual handles that.
Same as for destructors.
 
so if I am to deep clone, I have to provide clone function for each inherited class to add to cloned objects its darta
 
nwp
And you are not supposed to have newly allocated members. It makes things a lot easier.
 
10:27 AM
what do you mean ?
 
nwp
Never use new and memory management becomes much easier.
 
hmmm
that means never use shared_ptr?
I dont think I use any "new", only shared_ptrs
 
nwp
shared_ptr should be used very sparingly, but no. You still have std::make_unique.
 
hmm
aer you saying that "a lot " of shared_ptrs is a bad thing?
 
nwp
shared_ptr is a sometimes necessary evil because you can't figure out how long your objects live. You are supposed to use it as a last resort.
Most code can be written in a way where you know how long your objects live.
 
10:33 AM
hmm
would you say this is a correct way to copy stuff
struct testStructCopy {
    QString mId = "";
    QString mName = "";
    int mType = 0;
    int mSubType = 0;

    icOrderedMap<QString, QString> mMap;
    icOrderedMap<QString, std::shared_ptr<testStructCopy >> mChildren;

    testStructCopy(testStructCopy*ptr) {
        mId = ptr->mId;
        mName = ptr->mName;
        mType = ptr->mType;
        mSubType = ptr->mSubType;
        mMap = ptr->mMap;
        mChildren.copy(ptr->mChildren); /// provided by library
    }

    virtual std::shared_ptr<testStructCopy> clone() {
 
nwp
Are you sure mChildren.copy does what you want?
 
looking at lib yeah
well "I think it does"
he copies what he can via =, and the rest is made unique
"not shared ptr nique" just re-created from data
like loop
so seem to do right stuff
well
no
I think it copies keys
but objects
are pointers
crap :/
 
nwp
I would expect you have to loop over mChildren because icOrderedMap doesn't know that it must call ->clone().
Also clone() should be const.
And arguably return a std::unique_ptr.
 
mhmm
changed it to
for (auto child:ptr->mChildren.keys()) {
mChildren[child] = ptr->mChildren[child]->clone();
}
 
nwp
testStructCopy(testStructCopy*ptr) is weird. It should be a regular copy-constructor like testStructCopy(const testStructCopy &other).
@Dariusz That looks like it should do what you want.
 
10:46 AM
hmm
interesting
thanks!
looks like learning copying is going to be quite interesting part of my journey :- )
 
nwp
You probably should iterate over your children instead of doing a lookup for every key. It probably also makes a copy of all the keys.
 
hmm ?
not sure how, copying maps seem to always be tricky. Vectors I can manage. I just do vec(size) vec[x] = other[x]. But maps I always struggle with
 
nwp
It depends on how bad icOrderedMap is. If it's like std::map then you can simply do for (auto &key_value : ptr->mChildren) { mChildren[key_value.first] = key_value.second->clone(); }.
If it's like QMap you are screwed because it only gives you access to keys, not values.
 
its qmap based
 
nwp
rip sanity
 
10:52 AM
its linked list and qhash
 
nwp
I would avoid Qt containers. They are pre-C++11 and lack a lot of features.
 
perhaps I should dump it now
sigh. I googled soo many times whats better std or QT and most of the answers I got were that QT were more "modern". This is depressing.
 
nwp
Qt tends to have more stuff, like a .contains function which just sucks in the standard library. But you can still write a .contains function yourself. On the other hand QVector<std::unique_ptr<int>> does not compile because they don't understand move-semantics and there is no workaround besides giving up move-semantics or Qt containers.
 
hmmm
ok given this
testStructCopy(const testStructCopy &other) {}
and this
std::shared_ptr<testStructCopy> c = std::make_shared<testStructCopy>(this);

I get this error
error C2664: 'testStructCopy ::testStructCopy (const testStructCopy &)': cannot convert argument 1 from '_Ty' to 'const testStructCopy &'

whats wrong here?
 
nwp
this is a testStructCopy* and it doesn't know how to convert that to a const testStructCopy &.
 
11:01 AM
do I have to pass &this instead or something?
im lost
 
nwp
No, you have to dereference the pointer.
 
yeh just had a feeling about it
im still getting lost with *x and &x sigh
 
yeah, dealing with symbols that mean different things in different scenarios can be a real pain in the ass
even worse if two different symbols mean the same thing in different contexts (like char* and &c (adress-of-operator))
 
@_@
 
11:51 AM
Please someone tell me how to stop reading automatically in this problem
I am not asking to solve it, but how to take input, as no of input is not specified, nor it is specified how to stop taking input number
 
is there a way to make a switch that can do or statement?
switch (int):{
case (10||12):{}?
meh got it nvm
 
12:06 PM
@jeea see this link, codechef.com/wiki/tutorial-inputoutput basically you need to read from stdin. In C++ you can do that by just using std::cin
 
are thread_local objects created for threads which do not even use it?
I have a bunch of X threads and Y threads. The X threads use a thread_local object A. Will instances of A be created even for Y threads?
 
12:24 PM
@jeea I think you're supposed to use std::cin.eof() to detect when no input is left.
 
12:37 PM
@DexterCD can i use while(cin >> n)
 
@jeea Yeah that's fine, (bool)cin>>n is false when std::cin reaches the end of file.
 
1:09 PM
its showing runtime error, let me try other things
 
1:31 PM
finally iam getting time limit exceeded for this:
#include <iostream>
using namespace std;

unsigned long transact(unsigned long n) {
	if (n == 0)
		return 0;

	else {
		unsigned long temp = n/2 + n/3 + n/4;
		if (temp < n)
			return n;
		else
			return transact(n/2) + transact(n/3) + transact(n/4);
	}
}

int main()
{
	unsigned long n;
	while (cin >> n)
		cout << transact(n) << '\n';
}
I notice that it takes much time for n = 1e+9
so thats why tle is given
@DexterCD thanks!!!!
 
nwp
You should probably solve this with math.
Figure out a closed formula instead of recursion.
 
1:55 PM
@nwp should i store the value so that it doesnt recalculate
 
nwp
Maybe. Worth a try.
 
are values of map by default 0
 
nwp
2:18 PM
Basically yes.
 
#include <iostream>
#include <map>
using namespace std;
map<unsigned long, unsigned long> map1;

unsigned long transact(unsigned long n) {
	if (n == 0)
		return 0;
	else if (map1[n] == 0)
		return map1[n] = max(n, transact(n/2) + transact(n/3) + transact(n/4));
	else
		return map1[n];
}

int main()
{
	unsigned long n;
	while (cin >> n)
		cout << transact(n) << '\n';

}
finally working. does map have any advantage over vector
in this case, n can be lower than 1e+9
 
depends, what do you need a map for?
 
here instead should i use vector<unsigned int> v(1000000000, 0) instead of map1
?
 
@jeea I don't know which one you should use because I don't know what want to use it for
if you only need to perform sequential operations on a container, then vector is the way to go, I guess (or std::array, if the size doesn't change)
 
 
1 hour later…
3:56 PM
Would having a non-template class definition in a .cpp file and the class declaration in a .hpp file with template member functions comply with the standard?
g++ seems to accept it
 
@jeea std::vector<unsigned int> v(1000000000, 0); will attempt to allocate 4 GB of memory
so it's unacceptable for sparse arrays
 
4:19 PM
why isn't there a std::string_view overload for std::ifstream?
 
there could be, but isn't. propose it.
that said, I expect there's one problem why wouldn't you want it
native Linux and Windows APIs that operate on the files expect a null-terminated string
so if you were to pass it a std::string_view, it would have to convert it to std::string anyway, in order to make sure that there is a null-character afterwards
 
so there's your answer then
 
5:27 PM
In the second loop am I reassigning candB vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
int n = edges.size();
vector<int> parent(n+1, 0), candA, candB;
// step 1, check whether there is a node with two parents
for (auto &edge:edges) {
if (parent[edge[1]] == 0)
parent[edge[1]] = edge[0];
else {
candA = {parent[edge[1]], edge[1]};
candB = edge;
edge[1] = 0;
}
}
// step 2, union find
for (int i = 1; i <= n; i++) parent[i] = i;}
 

« first day (887 days earlier)      last day (1970 days later) »