Standard Libraries Flashcards

1
Q

How do you include the string library?

A

include

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

How do you convert from a string to integer?

A

std::stoi(my_string);

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

How do you convert from a string to a double?

A

std::stod(my_string);

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

How do you convert from double/integer to string?

A

std::to_string(my_new_int);

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

What is concatenating strings? How is it done?

A

This is combining strings.

std::string combined = string_1 + string_2 + string_3;

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

How do you obtain the first character from a string?

A

forename.front();

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

How do you obtain a specific character from a string?

A

forname[3];

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

How do you insert a string into another string?

A

combined.insert(6, sub_string);

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

how do you obtain a string from within another string?

A

combined.substr(0,3);

where 0 is the first letter and 3 is the 4th letter

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

How do you include the complex library?

A

include

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

How do you declare a complex variable?

A

std::complex c(2.0, -1.0);

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

How do you extract the real part of a complex variable?

A

c.real()

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

How do you extract the imaginary part of a complex variable?

A

c.imag()

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

How do you calculate the magnitude of the complex variable?

A

std::abs(c);

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

How do you calculate the phase angle of the complex variable?

A

std::arg(c);

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

How do you include vectors?

A

include

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

How do you declare a vector?

A

std::vector v;

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

How do you calculate the number of elements of the vector?

A

v.size();

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

How do you select an element in a vector?

A

v[2];

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

How do you add elements to a vector?

A

v.push_back(2);

21
Q

How do you remove the last element from a vector?

A

v.pop_back();

22
Q

How do you get the first element of a vector?

A

v.begin();

23
Q

How do you get the last element of a vector?

A

v.end();

24
Q

How do you insert an element into a vector?

A

v. insert(v.begin(),6);

v. insert(v.begin() + 3,6);

25
Q

How do you remove elements from a vector?

A

v.erase(v.begin(), v.begin() + 3);

26
Q

How do you sort elements in a vector in ascending order?

A

std::sort(v.begin(), v.end());

27
Q

How do you iterate through the elements of a vector?

A

for(std::vector::iterator i = v.begin(); i < v.end(); i++) {
std::cout&laquo_space;*i &laquo_space;std::endl;
}

28
Q

How do you sort elements in a vector in a descending order?

A

std::sort(v.begin(), v.end(), sort_descending);

29
Q

What is a map?

A

This an array where each element is not tied to a specific number but instead tied to a string.

30
Q

What is the string tied to an array position is referred to as:

A

Key

31
Q

how do you include map?

A

define

32
Q

How do you create a map?

A

std::map int_map;

33
Q

How do you add elements to a map?

A

int_map[“Name”] = 39;

34
Q

How do you erase elements from a map?

A

int_map.erase(“Evans”);

35
Q

How do we select elements from a map?

A

To get the key we use: ->first

To get the value we use: ->second

36
Q

How do you iterate through maps?

A

for(i = int_map.begin(); i != int_map.end(); i++) {
std::string key = i-> first;
int value = i->second;
}

37
Q

How do you obtain te values of a map without knowing its position?

A

int value = int_map[“Name”];

38
Q

How do you check to see if a key exists? What does it return?

A

int_map.count(key);

> Returns an integer number of times that that key is found

39
Q

How do you include bitset?

A

include

40
Q

How do you declare a bitset?

A

std::bitset<8> my_empty_byte;

41
Q

How do you declare and initialise a bitset with a value?

A

std::bitset<8> my_byte(128);

42
Q

How do you declare and initialise a bitset with a hexadecimal?

A

std::bitset<8> my_byte(0x0F);

43
Q

How do you declare and initialise a bitset with a binary value?

A

std::bitset<8> my_byte(std::string(“00110011”));

44
Q

How do you print bitsets?

A

std::cout &laquo_space;my_byte &laquo_space;std::endl;

45
Q

How do you clear a bit toa 0?

A

my_byte.reset(7);

46
Q

How do you set a bit to a 1?

A

my_byte.set(3);

47
Q

How do you flip a bit>

A

my_byte.flip(3);

48
Q

How do you access a single bit?

A

my_byte[5];

my_byte.test(0);

49
Q

What are the bit-wise operations?

A
AND = &amp;
OR = |
XOR = ^
NOT = ~
Example:
std::bitset<4> = z = a | b;