C++ functions Flashcards
How to check length of string?
str.size();
myString.length();
How to use sort function?
std::sort(nums.begin(), nums.end())
where nums is a vector
std:sort(demo, demo + len)
where demo is an array and len = sizeof(demo)/sizeof(demo[0]);
How to convert a vector of chars to a string?
string str(vec.begin(), vec.end());
How to check number of keys in a map?
map_name.size()
How to check size of a vector?
vector_name.size()
How to iterate through keys in a map?
for(std::map<Key,Val>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
Key k = iter->first;
//ignore value
//Value v = iter->second;
}
Methods to initialize a vector in c++?
Initializing Vector by Pushing values One by One:
vector_name.push_back(value)
Initializing Vector by Specifying Size and Initializing All Values:
vector<type> vector_name(size, default_value);</type>
Initializing Vector like Arrays:
vector<type> vector_name = {v1, v2, v3 ....};</type>
Initializing Vector from an Array :
vector<type> vector_name(arr, arr + size);</type>
Initializing Vector from Another Vector :
vector<type> vector_name(vec1.begin(), vec1.end());</type>
Initializing all Elements of Vector with a Particular Value:(use std::fill)
fill(vector_name.begin(), vector_name.end(), value);
Initialize Vector with Consecutive Numbers using std::iota:
iota(vector_name.begin(), vector_name.end(), value);
How to initialize a vector of fixed size filled with epmty vectors?
vector<vector<int>> vec(outer_size, vector<int>(inner_size));</int></int>
Function to check if the character is alphanumeric?
Function to convert to uppercase(letter or digits)?
Function to convert to lowercase(letter or digits)?
if(isalnum(‘s’) == true)
toupper(‘s’)
tolower(‘S’)
Function to check if “character” is a number:
Function to convert a numeric “string” to a number:
isdigit(‘4’) accepts characters
stoi(“454”);
extracting a character from a vector of strings: vector<string> s;(even if s is just 1 character long)</string>
s[index][0]
stack data type operations and their associated time complexities:
The functions associated with stack are:
empty() – Returns whether the stack is empty – Time Complexity : O(1)
size() – Returns the size of the stack – Time Complexity : O(1)
top() – Returns a reference to the top most element of the stack – Time Complexity : O(1)
push(g) – Adds the element ‘g’ at the top of the stack – Time Complexity : O(1)
pop() – Deletes the most recent entered element of the stack – Time Complexity : O(1)
What are the 7 most important methods for queue?(What are their time complexities?)
queue::empty() O(1)
queue::size() O(1)
queue::emplace() O(1)
queue::front() O(1)
queue::back() O(1)
queue::push(g) O(1)
queue::pop() O(1)
What are the important methods of a deqeue? (also mention time complexities)
Methods:
dq.push_back(3); O(1)
dq.push_front(3); O(1)
dq.front(); O(1)
dq.back(); O(1)
dq.at(4); O(1)
dq.size(); O(1)
dq.max_size(); O(1)
dq.pop_front(); O(1)
dq.pop_back(); O(1)
dq.empty(): O(1) to check if dq is empty
Time complexities:
Accessing Elements- O(1)
Insertion or removal of elements- O(N)
Insertion or removal of elements at start or end- O(1)
Important methods in a map along with associated Time Complexities?
Methods:
begin() – Returns an iterator to the first element in the map.
map::insert({key, value}) -> Insert elements with a particular key in the map container –> O(log n)
empty() – Returns whether the map is empty.
map.erase(keyvalue) -> Used to erase elements from the container –> O(log n)
map find() -> Returns an iterator to the element with key-value ‘g’ in the map if found, else returns the iterator to end.
end() – Returns an iterator to the theoretical element that follows the last element in the map.
map rend() -> Returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end).
map rbegin() -> Returns a reverse iterator which points to the last element of the map.