-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf.hpp
69 lines (65 loc) · 1.41 KB
/
elf.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* elf.hpp
* Useful functions for Competitive Programming.
* @author Dante Calderón<@dantehemerson>
**/
#include <bits/stdc++.h>
/**
* Convert string to uppercase.
*
* @param s The string to convert.
* @return A new string in uppercase.
* COMPLEXITY = O(n), n = |s|
**/
std::string toUpperCase(std::string s) {
transform(s.begin(), s.end(), s.begin(), std::ptr_fun <int, int>(toupper));
return s;
}
/**
* Convert string to lowercase.
*
* @param s The string to convert.
* @return A new string in lowercase.
* COMPLEXITY = O(n), n = |s|
**/
std::string toLowerCase(std::string s) {
transform(s.begin(), s.end(), s.begin(), std::ptr_fun <int, int>(tolower));
return s;
}
/**
* Convert an integer in string to integer.
*
* @param numString The number in string to convert.
* @return numString in int.
* COMPLEXITY = O(1)
**/
int parseInt(const std::string& numString, int base = 10) {
std::istringstream istr(numString);
int value;
istr >> value;
return value;
}
/**
* Convert an integer to string.
*
* @param number The number to convert.
* @return The number n in string.
* COMPLEXITY = O(n), n = length of number
**/
std::string toString(int number) {
if (number == 0) return "0";
size_t pos = 0;
std::string value;
if(number < 0) { // Negative
value = "-";
pos = 1;
number *= -1;
}
char mod;
while(number > 0) {
mod = number % 10;
value.insert(pos, 1, mod + 48);
number /= 10;
}
return value;
}