Standard Libraries Flashcards
How do you include the string library?
include
How do you convert from a string to integer?
std::stoi(my_string);
How do you convert from a string to a double?
std::stod(my_string);
How do you convert from double/integer to string?
std::to_string(my_new_int);
What is concatenating strings? How is it done?
This is combining strings.
std::string combined = string_1 + string_2 + string_3;
How do you obtain the first character from a string?
forename.front();
How do you obtain a specific character from a string?
forname[3];
How do you insert a string into another string?
combined.insert(6, sub_string);
how do you obtain a string from within another string?
combined.substr(0,3);
where 0 is the first letter and 3 is the 4th letter
How do you include the complex library?
include
How do you declare a complex variable?
std::complex c(2.0, -1.0);
How do you extract the real part of a complex variable?
c.real()
How do you extract the imaginary part of a complex variable?
c.imag()
How do you calculate the magnitude of the complex variable?
std::abs(c);
How do you calculate the phase angle of the complex variable?
std::arg(c);
How do you include vectors?
include
How do you declare a vector?
std::vector v;
How do you calculate the number of elements of the vector?
v.size();
How do you select an element in a vector?
v[2];
How do you add elements to a vector?
v.push_back(2);
How do you remove the last element from a vector?
v.pop_back();
How do you get the first element of a vector?
v.begin();
How do you get the last element of a vector?
v.end();
How do you insert an element into a vector?
v. insert(v.begin(),6);
v. insert(v.begin() + 3,6);
How do you remove elements from a vector?
v.erase(v.begin(), v.begin() + 3);
How do you sort elements in a vector in ascending order?
std::sort(v.begin(), v.end());
How do you iterate through the elements of a vector?
for(std::vector::iterator i = v.begin(); i < v.end(); i++) {
std::cout«_space;*i «_space;std::endl;
}
How do you sort elements in a vector in a descending order?
std::sort(v.begin(), v.end(), sort_descending);
What is a map?
This an array where each element is not tied to a specific number but instead tied to a string.
What is the string tied to an array position is referred to as:
Key
how do you include map?
define
How do you create a map?
std::map int_map;
How do you add elements to a map?
int_map[“Name”] = 39;
How do you erase elements from a map?
int_map.erase(“Evans”);
How do we select elements from a map?
To get the key we use: ->first
To get the value we use: ->second
How do you iterate through maps?
for(i = int_map.begin(); i != int_map.end(); i++) {
std::string key = i-> first;
int value = i->second;
}
How do you obtain te values of a map without knowing its position?
int value = int_map[“Name”];
How do you check to see if a key exists? What does it return?
int_map.count(key);
> Returns an integer number of times that that key is found
How do you include bitset?
include
How do you declare a bitset?
std::bitset<8> my_empty_byte;
How do you declare and initialise a bitset with a value?
std::bitset<8> my_byte(128);
How do you declare and initialise a bitset with a hexadecimal?
std::bitset<8> my_byte(0x0F);
How do you declare and initialise a bitset with a binary value?
std::bitset<8> my_byte(std::string(“00110011”));
How do you print bitsets?
std::cout «_space;my_byte «_space;std::endl;
How do you clear a bit toa 0?
my_byte.reset(7);
How do you set a bit to a 1?
my_byte.set(3);
How do you flip a bit>
my_byte.flip(3);
How do you access a single bit?
my_byte[5];
my_byte.test(0);
What are the bit-wise operations?
AND = & OR = | XOR = ^ NOT = ~ Example: std::bitset<4> = z = a | b;