module 6 Flashcards

(25 cards)

1
Q

The following operation \_\_\_\_\_\_\_\_ the bit:

b & 1 == b
A

reads

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The following operation \_\_\_\_\_\_\_\_ the bit:

b | 1 == 1
A

sets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The following operation \_\_\_\_\_\_\_\_ the bit:

b & 0 == 0
A

resets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The following operation \_\_\_\_\_\_\_\_ the bit:

b ^ 1 == ~b
A

flips

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a line of code that will form a 1 mask with the 1 at bit position n

A
unsigned int mask = 1u << n;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a line of code to test if a bit is set at position n of x:

unsigned int mask = 1u << n;
A

bool is_set = !!(x & mask);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a line of code to set the bit at position n of x using this mask:

unsigned int mask = 1u << n;
A
x |= mask;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a line of code to set all bits of x

A
x = ~0u;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a mask, then use that mask to set all the bits of x below position n

A
mask = (1u << n) - 1;
x &= mask;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a mask, then use that mask to set all the bits of x between position m and position n (inclusive)

A
mask = ((1u << ( n - m + 1)) - 1) << m;
x &= mask;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a mask, then use the mask to reset a single bit of x at position n

A
unsigned int mask = ~(1u << n);
x &= mask;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a line of code to flip the bit at position n of x using this mask:

unsigned int mask = 1u &laquo_space;n;

A
x ^= mask;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Extract bits m through n of x as a number

A
mask = ((1u << ( n - m + 1)) - 1) << m;
unsigned int new_num = (x & mask) >> m;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a line of code to swap the internal data of these two vectors:

vector<string> svec1(10);
vector<string> svec2(24);
A
swap(svec1, svec2);

exchanges internal data including pointers, a shallow exchange

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

adding elements

A
  • c.push_back(t) - creates an element with the value t at the end of c
  • c.emplace_back(args) - constructs an element from the args at the end of t
  • c.push_front(t) - creates an element with the value t at the front of c
  • c.emplace_back(args) - constructs an element from the args at the front of c
  • c.insert(p, t) - creates an element with the value t, and places it before the element denoted by the iterator p, returns an iterator to the added element
  • c.emplace(p, args) - constructs an element from the args, and places it before the element denoted by the iterator p, returns an iterator to the added element
  • c.insert(p, n, t) - inserts n elements with the value t, and places it before the element denoted by the iterator p, returns an iterator to the first element inserted
  • c.insert(p, b, e) - inserts elements from the range denoted by the iterators b and e, and places it before the element denoted by the iterator p, returns an iterator to the first element inserted
  • c.insert(p, i]) - i] is a braced list fo element values, inserts the given values before the element denoted by the iterator p, returns an iterator to the first element inserted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Insert the string “Hello!” at the beginning of the vector and the list:

vector<string> svec;
list<string> slist;
A
slist.insert(slist.begin(), "Hello!);
svec.insert(svec.begin(), "Hello!);
17
Q

Erasing Elements

A
  • c.pop_back() - removes the last element in c, returns void
  • c.pop_front - removes the first element in c, returns void
  • c.erase(p) - removes the element donoted by the iterator p, and returns an iterator to the element after the one deleted
  • c.erase(b, e) - removes the range of elements denoted by the iterators b and e, and returns an iterator to the element after the last one deleted
  • c.clear() - removes all elements in c, returns void
18
Q

Managing Container Size

A
  • c.resize(n) - resize c so that it has n elements
  • c.resize(n, t) - resize c so that is has n elements, any elements added have value t.
  • c.shrink_to_fit() - request to reduce capacity() to equal size() only for vector, string, and deque
  • c.capacity() - number of elements c can have before reallocation is necessary, only for vector and string
  • c.reserve(n) - allocate space for at least n elements, only for vector and string
19
Q

given the following class C, write a move constructor:

#include <iostream>
using namespace std;

class C {
public:
    C() = default;
    C(const C&) {cout << "copy constructor\n";}
    C& operator=(const C&) {
        cout << "copy assignment\n";
        return *this;
    }
};
A
C(C&&) {cout << "move constructor\n";}
20
Q

given the following class C, write a move assignment:

#include <iostream>
using namespace std;

class C {
public:
    C() = default;
    C(const C&) {cout << "copy constructor\n";}
    C& operator=(const C&) {
        cout << "copy assignment\n";
        return *this;
    }
};
A
    C& operator=(C&&) {
        cout << "move assignment\n";
        return *this;
    }
21
Q

Given the following code, what is the output of the marked line in main()?

#include <iostream>
using namespace std;

class C {
public:
    C() = default;
    C(const C&) {cout << "copy constructor\n";}
    C(C&&) {cout << "move constructor\n";}
    C& operator=(const C&) {
        cout << "copy assignment\n";
        return *this;
    }
    C& operator=(C&&) {
        cout << "move assignment\n";
        return *this;
    }
};
C g() {return C();}
void f(C) {}

int main() {
    C c;
    C c2(c);     // output?
    c = c2;
    c = g();
    f(c);
    f(g());
    f(std::move(g()));
}
A

copy constructor

22
Q

Given the following code, what is the output of the marked line in main()?

#include <iostream>
using namespace std;

class C {
public:
    C() = default;
    C(const C&) {cout << "copy constructor\n";}
    C(C&&) {cout << "move constructor\n";}
    C& operator=(const C&) {
        cout << "copy assignment\n";
        return *this;
    }
    C& operator=(C&&) {
        cout << "move assignment\n";
        return *this;
    }
};
C g() {return C();}
void f(C) {}

int main() {
    C c;
    C c2(c);
    c = c2;     // output?
    c = g();
    f(c);
    f(g());
    f(std::move(g()));
}
A

copy assignment

23
Q

Given the following code, what is the output of the marked line in main()?

#include <iostream>
using namespace std;

class C {
public:
    C() = default;
    C(const C&) {cout << "copy constructor\n";}
    C(C&&) {cout << "move constructor\n";}
    C& operator=(const C&) {
        cout << "copy assignment\n";
        return *this;
    }
    C& operator=(C&&) {
        cout << "move assignment\n";
        return *this;
    }
};
C g() {return C();}
void f(C) {}

int main() {
    C c;
    C c2(c);
    c = c2;
    c = g();     // output?
    f(c);
    f(g());
    f(std::move(g()));
}
A

move assignment

24
Q

Given the following code, what is the output of the marked line in main()?

#include <iostream>
using namespace std;

class C {
public:
    C() = default;
    C(const C&) {cout << "copy constructor\n";}
    C(C&&) {cout << "move constructor\n";}
    C& operator=(const C&) {
        cout << "copy assignment\n";
        return *this;
    }
    C& operator=(C&&) {
        cout << "move assignment\n";
        return *this;
    }
};
C g() {return C();}
void f(C) {}

int main() {
    C c;
    C c2(c);
    c = c2;
    c = g();
    f(c);        // output?
    f(g());
    f(std::move(g()));
}
A

copy constructor

25
Given the following code, what is the output of the marked line in `main()`? ``` #include using namespace std; class C { public: C() = default; C(const C&) {cout << "copy constructor\n";} C(C&&) {cout << "move constructor\n";} C& operator=(const C&) { cout << "copy assignment\n"; return *this; } C& operator=(C&&) { cout << "move assignment\n"; return *this; } }; C g() {return C();} void f(C) {} int main() { C c; C c2(c); c = c2; c = g(); f(c); f(g()); f(std::move(g())); // output? } ```
move constructor