« first day (1529 days earlier)      last day (1324 days later) » 

5:26 AM
std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(vec));
Breaks only on character input
What's wrong?
 
 
3 hours later…
8:20 AM
What kind of messages are useful to put when assertions fail? Is it correct to think the person reading it is a programmer with the intent to debug it? For example I have assert(hi <= low, "hi must be greater than low");
 
9:02 AM
Assertions aren't active in release mode normally, so yeah, you can assume that it's a programmer reading the assertion message
 
user12057802
9:48 AM
std::string(foo::bar::function(1,2).plus(3)) What s the possible way to implement function?
 
user12057802
Also std::string(foo::bar::function(1,2)) should be valid
 
struct A{
int a;
operator std::string() const { return std::to_string(a); }
A& plus(int b){a+= b; return *this;}
};
A function(int b, int c){ return A{b+c};}
@0x00004 there you go, user defined conversions are one way
 
user12057802
10:10 AM
Hmm interesting i will try now . Thanks
 
10:21 AM
Thanks
 
 
2 hours later…
11:59 AM
Hi everyone! I was reading about copy constructors and copy assignments. In this book, they've explained these by implementing their own version of Vector.
Here's their version of the copy assignment
Vector& Vector::operator=(const Vector& a)
{
double∗ p = new double[a.sz];
for (int i=0; i!=a.sz; ++i)
p[i] = a.elem[i];
delete[] elem;
// delete old elements
elem = p;
sz = a.sz;
return ∗this;
}
And here's mine
Vector& operator=(const Vector& v)
{
sz = v.sz;
delete[] elem;
elem = new double[v.sz];
for (int i = 0; i < v.sz; ++i)
elem[i] = v.elem[i];
return *this;
}
So I've eliminated the use of another double* p
 
yeah, you can do that, but the other version leaves the vector in a valid state if a bad_alloc exception is thrown
and allocating (and deallocating) a double * on the stack is practically free unless you're in a recursive algorithm or on some embedded platform
 
So if I moved elem = new double[v.sz] to the first line of the method, would that fix that problem?
 
@ApoorvaAnand then how you would you delete the old array?
 
Ahhhh
Okay thank you so much!
 
 
4 hours later…
4:15 PM
if (*leftWallHeights.rbegin() != top) {         // mark a skyline point if top changes
        ans.push_back({w.first, top = *leftWallHeights.rbegin()});
}
why do you need the *'s there?
 
to get from the iterator to a reference to the stored element
 
 
2 hours later…
5:46 PM
Hi All, i have doubt regarding deleting object after remove from list.
A *obj = new A();
list.push_back(obj);
// After performing operations
list.remove(obj);
delete obj; // this is the correct way to delete the object
obj = nullptr;
 
@shyam why are you using raw pointers for this? 99% of the time you can use list.emplace_back() and be fine?
 
@Mgetz thank you , Actually i am using shared_ptr in implementation but i am pushing raw pointer into list , after removing from list and deleting raw pointer , it's crashing.
 
yes that's a double delete... why are you using shared_ptr?
 
that object shared data between threads.
like consumer and producer threads.
@Mgetz i am beginer in C++, can you please tell me , how it will be double delete and how we can avoid it. any link also fine.
 
6:06 PM
@shyam when you destroy the last shared_ptr that references the ptr it will delete it
so if you manually call delete on a pointer already managed by a shared_ptr you will delete it twice
 
@PeterT Thank you so much, it's saved my day.
 

« first day (1529 days earlier)      last day (1324 days later) »