module 6 Flashcards
(25 cards)
The following operation \_\_\_\_\_\_\_\_
the bit:
b & 1 == b
reads
The following operation \_\_\_\_\_\_\_\_
the bit:
b | 1 == 1
sets
The following operation \_\_\_\_\_\_\_\_
the bit:
b & 0 == 0
resets
The following operation \_\_\_\_\_\_\_\_
the bit:
b ^ 1 == ~b
flips
Write a line of code that will form a 1 mask with the 1 at bit position n
unsigned int mask = 1u << n;
Write a line of code to test if a bit is set at position n of x
:
unsigned int mask = 1u << n;
bool is_set = !!(x & mask);
Write a line of code to set the bit at position n of x using this mask:
unsigned int mask = 1u << n;
x |= mask;
Write a line of code to set all bits of x
x = ~0u;
Write a mask, then use that mask to set all the bits of x below position n
mask = (1u << n) - 1; x &= mask;
Write a mask, then use that mask to set all the bits of x between position m and position n (inclusive)
mask = ((1u << ( n - m + 1)) - 1) << m; x &= mask;
Write a mask, then use the mask to reset a single bit of x at position n
unsigned int mask = ~(1u << n); x &= mask;
Write a line of code to flip the bit at position n of x using this mask:
unsigned int mask = 1u «_space;n;
x ^= mask;
Extract bits m through n of x as a number
mask = ((1u << ( n - m + 1)) - 1) << m; unsigned int new_num = (x & mask) >> m;
Write a line of code to swap the internal data of these two vectors:
vector<string> svec1(10); vector<string> svec2(24);
swap(svec1, svec2);
exchanges internal data including pointers, a shallow exchange
adding elements
-
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
Insert the string “Hello!” at the beginning of the vector and the list:
vector<string> svec; list<string> slist;
slist.insert(slist.begin(), "Hello!); svec.insert(svec.begin(), "Hello!);
Erasing Elements
-
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
Managing Container Size
-
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 forvector
,string
, anddeque
-
c.capacity()
- number of elements c can have before reallocation is necessary, only forvector
andstring
-
c.reserve(n)
- allocate space for at least n elements, only forvector
andstring
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; } };
C(C&&) {cout << "move constructor\n";}
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; } };
C& operator=(C&&) { cout << "move assignment\n"; return *this; }
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())); }
copy constructor
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())); }
copy assignment
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())); }
move assignment
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())); }
copy constructor