C and C++ titbits
C and C++ titbits
struct point
int x;
int y;
};
printf("x=%d\n", p->x);
printf("y=%d\n", p->y);
int main(void) {
p1.print(&p1);
p2.print(&p2);
String
Modifiers:
pop_back
int main ()
std::cout << str2 << ' ' << str3 << '\n'; return 0;
Thread
std::queue<int> dataq;
pthread_mutex_t m1;
pthread_mutexattr_t attr;
while(1)
pthread_mutex_trylock(&m1);
dataq.push(i);
pthread_mutex_unlock(&m1);
while(!dataq.empty())
pthread_mutex_trylock(&m1);
dataq.pop();
pthread_mutex_unlock(&m1);
int main() {
pthread_t pid1;
pthread_t pid2;
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_init(&m1, &attr);
sleep(1);
pthread_exit(NULL);
pthread_exit(NULL);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdatomic.h>
atomic_flag f = ATOMIC_FLAG_INIT;
int main(void)
bool r = atomic_flag_test_and_set(&f);
r = atomic_flag_test_and_set(&f);
atomic_flag_clear(&f);
r = atomic_flag_test_and_set(&f);
Queue
empty
size
back
push
emplace
pop
swap
v.erase(v.begin()+1, v.begin()+3)
st.erase(first, last);
Priority_Queue
return true;
return false;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
it=mymap.find('b');
it=mymap.find ('e');
mp.erase(it);
N in binary
cout << n << " in binary is " << bitset<32>(n) << endl;
cout << n << " in binary is " << bitset<32>(n) << endl;
Num&((pow of 2) - 1)
Eg 5%2 = 1
101&1 = 1
5%8 = 5
101&111 = 5
Maximum of 2 numbers
Multiply 2 numbers
3) Return 'res'.