C++ Data Structures & STL library methods
Hope below list saves your revision time.
Cheatsheet for Interview Questions
STRING | VECTOR | STACK | QUEUE | DEQUEUE | MAP | SET | MIN-HEAP | MAX-HEAP |
string str=”byteman” char a = ‘T’; cout<<std:string(1,a);//”T” | vector<string> v = {“a”,”b”} vector<int> v(5) // vector of size 5 //v.size() vector<int>v(5,-1) //size 5 default val= -1 vector<vector<int>> arr(10 , vector<int>(50 , 3)); //10 – rows with 50 column having default value 3. | stack<int> st ; | queue<char> q | deque<string> dq (5) // size 5 | ma p<char,int> mp; | set<int> s; unordered<int> s = {10,11,12,13,14,15,16,17,18,19,20}; | priority_queue<int, vector<int>, comp> minHeap; struct comparator { bool operator()(int a, int b) { return a > b; } }; | priority_queue<int, vector<int>, comp> maxHeap; struct comparator { bool operator()(int a, int b) { return a < b; } }; |
1.)string to int : stoi( str ) 2.)int to string : to_string(a) 3.)character to string : std:string(1,a) 4.)str.substr(startIndex,length); 5.)str.find(“TEXT_TO_FIND”); | ||||||||
str.insert(START_POSITION,str2); START_POSITION=> index number example here | v.push_back(“cool”) // insert at back v.pop_back() // pop from last v.insert( v.begin()+ 2 , 14) // (pos , value) | st.push(2) st.pop() | q.push(2) q.pop() | dq.push_back(2) dq.push_front(8) dq.pop_back() dq.pop_front() | mp.insert( pair<char,int>(‘c’,3) ); or mp[‘c’] = 3; | s.insert(2) | minHp.push(2) minHp.pop() | maxHp.push(2); maxHp.pop() |
str.length() str.size() | v.size() | st.size() | q.size() | dq.size() | mp.size() | s.size() | ||
st.top() | q.front() // direct reference to first element q.back() // direct reference to last element | dq.front() += 5 // (direct reference) val of front incremented in deque by 5 dq.back() // direct reference to last element dq.at(4) // position = 4 , throws exception if out of range | minHp.top() | maxHp.top(); | ||||
s.count(15) // 1 s.count(77) // 0 | ||||||||
v.clear() | s.clear() //eg : s.size() = 0 after this | |||||||
v.erase(arr1.begin() + 3) // delete 4th element OR v.erase(arr1.begin()) | s.erase(12) or s.erase( s.begin() + 2 ) // erase “12” | |||||||
sort(str.begin() , str.end()); | sort(v.begin() , v.end()); or //custom sort function sort(arr.begin() , arr.end() ,[]( const int &a , const int &b ) { return a < b; } ); | |||||||
for(pair<string , int> p : v) { cout<<p.first; cout<<p.second; } | ||||||||
// check if alphaNumeric (alphabets + number) isalnum // to Lower & upper case tolower(CHAR_TO_CHANGE); toupper(CHAR_TO_CHANGE); //convert complete line transform(str.begin(), str.end(), str.begin(), ::toupper); | Remove Element from Vector by given value int ELEM_VAL = 8; v.erase(std::remove(v.begin(), v.end(), ELEM_VAL), v.end()); | Sort by Value eg: map<string , int> phoneNum; 1.) vector<pair<string , int>> vecData; for(auto &it : phoneNum){ vecData.push_back(it); } sort(vecData.begin() , vecData.end() , []( pair<string,int> a , pair<string,int> b ){ return a.second < b.second; }); |
Java Syntax
String | Array | STACK | QUEUE | DEQUEUE | MAP | SET | MIN-HEAP | MAX-HEAP |
// size , a.length int[] a = new int[4] int[] b = new int[a.length] int[] c = {1,2,3,4,5} int[] d = Arrays.copyOf(c , c.length) Arrays.fills(b , -1); |
Pingback: Cheat sheet Question List - ByteWayne Tech:Unleashing the Batman Within