Category : c++11

Could you please look at this lines of code taken from the cpp reference and translate them for me, explaining them line by line? #include <iostream> #include <type_traits> struct A { // non-POD type int avg; A (int a, int b) : avg((a+b)/2) {} }; typedef std::aligned_storage<sizeof(A),alignof(A)>::type A_pod; int main() { A_pod a,b; new (&a) ..

Read more

Question:- https://leetcode.com/problems/intersection-of-two-arrays/ This is my code:- class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector <int> result; for(int i=0;i<nums1.size(); i++){ for(int j=0;j<nums2.size();j++){ if(nums1[i] == nums2[j]){ result.push_back(nums1[i]); break; } } } for(int i=0;i<result.size();i++){ for(int j=0;j<result.size();j++){ if(result[i] == result[j] and i!=j){ result.pop_back(); } } } return result; } }; I am getting wrong output for ..

Read more

I am learning cpp and I am creating a Vector class using templates, namespaces, header files… . Now this is my code so far main.cpp #include "my_vector.h" #include <string> #include <iostream> int main(){ MY::Vector<int> vector; } my_vector.cpp #include "my_vector.h" #include <string> #include <iostream> // implementation of all the constructors template <class T> MY::Vector<T>::Vector(){ size = ..

Read more

Consider the store buffer litmus test with SC atomics: // Initial std::atomic<int> x(0), y(0); // Thread 1 // Thread 2 x.store(1); y.store(1); auto r1 = y.load(); auto r2 = x.load(); Can this program end with both r1 and r2 being zero? I can’t see how this result is forbidden by the description about memory_order_seq_cst in ..

Read more