stringstream ss; while(getline(ss, symbol, ',')) {...}; // get every part split by seperator. stoi(char); // into int.
基本上就是说不希望caller改变return的值,因此返回一个reference。
例如: const unordered_set<string> & dict
It returns a reference to the private member.
push: 直接将元素append进数据结构。只允许一个argument。对于stack<pair<int, char>>
的情况,应该在argument处使用constructor: stack.push(std::make_pair(3, 'a'))
,以保证只有一个argument。
emplace: 将元素放进数据结构,同时call constructor,允许多个argument,相当于简化了上述必须在argument处所做的处理: stack.emplace(3, 'a')
即可。
https://stackoverflow.com/questions/4303513/push-back-vs-emplace-back
In addition to what visitor said :
The function void emplace_back(Type&& _Val) provided by MSCV10 is non conforming and redundant, because as you noted it is strictly equivalent to push_back(Type&& _Val).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.geeksforgeeks.org/stack-data-structure-introduction-program/ | |
// usually implemented with array or linked list implementation. | |
// Or direcly using STL library: `#include <stack>`. | |
// following is an example of re-writing stack class, with extra function `max()`, returns the current max element of the stack. | |
#include <stack> | |
template <typename T> | |
class Stack { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Then sugguests that using only O(1) space complexity, meaning only `swap` is recommended! | |
// 很多关于linked list的技巧基本都和two pointer同时iterate有关。 | |
// 1. using two pointer technique(slower and fast pointer) to find the middle point of the list. | |
// Q: check if a linked list is palindrome: two pointer iterate elements one by one and check if equal. | |
// Q: zip a linked list: interleaving the first and second halves of the linked list. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Node{ | |
int data; | |
Node* next; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node* reverse(node* L) { | |
node* prev = NULL, curr = L; | |
node* tmp = NULL; | |
while(curr) { | |
tmp = curr.next; | |
curr.next = prev; | |
prev = curr; | |
curr = tmp; | |
} | |
return prev; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Q: given a list <l0, l1, l2, ...>, modify it into <l0, l2, l4,..., l1, l3...> a even-odd merged list. | |
// 多指针同时遍历的技巧。prev的使用。以及corner case针对even\odd的讨论。一个技巧是,根据已有的变量是否会seg fault来判断需要新增的case,最常见的遗漏就是对是否为null的讨论(针对linked list是否为空等等)。 | |
node* even_odd_merge(node* L) { | |
node* prev_even = NULL; | |
node* even = L, odd = L.next; | |
node* odd_start = L.next; | |
while(even && odd) { | |
prev_even = even; | |
even.next = odd.next; | |
even = even.next; |
NewerOlder