DEV Community

dinhluanbmt
dinhluanbmt

Posted on

1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay