C++ functions Flashcards

1
Q

How to check length of string?

A

str.size();
myString.length();

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

How to use sort function?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to convert a vector of chars to a string?

A

string str(vec.begin(), vec.end());

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

How to check number of keys in a map?

A

map_name.size()

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

How to check size of a vector?

A

vector_name.size()

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

How to iterate through keys in a map?

A

for(std::map<Key,Val>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
Key k = iter->first;
//ignore value
//Value v = iter->second;
}

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

Methods to initialize a vector in c++?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to initialize a vector of fixed size filled with epmty vectors?

A

vector<vector<int>> vec(outer_size, vector<int>(inner_size));</int></int>

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

Function to check if the character is alphanumeric?
Function to convert to uppercase(letter or digits)?
Function to convert to lowercase(letter or digits)?

A

if(isalnum(‘s’) == true)
toupper(‘s’)
tolower(‘S’)

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

Function to check if “character” is a number:
Function to convert a numeric “string” to a number:

A

isdigit(‘4’) accepts characters
stoi(“454”);

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

extracting a character from a vector of strings: vector<string> s;(even if s is just 1 character long)</string>

A

s[index][0]

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

stack data type operations and their associated time complexities:

A

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)

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

What are the 7 most important methods for queue?(What are their time complexities?)

A

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)

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

What are the important methods of a deqeue? (also mention time complexities)

A

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)

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

Important methods in a map along with associated Time Complexities?

A

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.

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