DEV Community

dinhluanbmt
dinhluanbmt

Posted on

C++, speedup your function by using vector as simple map

Sometimes, we need to use map or map if the data is simple enough, such as only English characters or only '1' to '9', etc. This is especially true when the map is created repeatedly.
Using vector of char or vector of string is the best way to increase the speed of your program.

+ int vs char :0 -> 'a', 1 -> 'b',...  
vector<char> cV={'a','b','c'};
// get char c of int val  -> char c = sV[val]
+  char vs string : 'a' -> ".-", 'b' -> "-..", ... 
vector<string> sV ={".-","-.."};
//to get string of char ch  -> string code = sV[ch-'a'];
Enter fullscreen mode Exit fullscreen mode

Top comments (0)