Leetcode C++ STL Helper Functions

String

1
2
3
4
5
6
7
8
isupper : is upper alphabetic? {A, B, ..., Z}
islower : is lower alphabetic? {a, b, ..., z}
isdigit : is digits? {0, 1, ..., 9}
isalpha : is alphabetic? (islower || isupper)
isalnum : is alphanumeric? (isalpha || isdigit)

tolower
toupper

Sort

1
2
// sort into ascending order
std::sort (vec.begin(), vec.end(), comp);

Random

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
std::random_device rd;
std::mt19937 gen;
std::uniform_int_distribution<> dist;
public:
Solution() {
dist = uniform_int_distribution<>(0, 100);
gen = std::mt19937(rd());
}

int foobar() {
return dist(gen);
}
};

Map

1
2
3
std::map<int, int> M = {
{-3,0}, {-1,0}, {0,0}, {4,0}, {5,0}, {8,0}
};

bounds

1
2
3
4
5
6
// -1, 0, 4, 5 
auto lower = M.lower_bound(-2);
auto upper = M.upper_bound(6);
for (auto it = lower; it < upper; it++) {
std::cout << it->first << “, “;
}