I have just finished working on a problem on hackerrank dealing with time conversion in C++.

I'm still fairly new to programming as I only have a few semesters of programming classes under my belt and had a pretty challenging time with this program.

Below is a link to the problem itself and my code.
I solved the problem but I feel like my time complexity is unnecessarily high. It works to solve all the test cases the site provides but I am looking to become a better developer so I want to learn to write more efficient and effective code.

Input Format

A single string containing a time in 12-hour clock format (i.e.:hh:mm:ssAM or hh:mm:ssPM), where 01 <= hh <= 12.

Output Format

Convert and print the given time in 24-hour format, where 00 <= hh <= 23.

Please tell show me how I can optimize this code for a better run time.

(Note: it's open to question whether the call to mktime is really necessary here, but at worst it's still pretty harmless).

Switch Statement

Looking more specifically at your code, the thing that probably sticks out the most is the big switch statement. I'd eliminate that in favor of a tiny bit of math:

if (ampm == "PM")
hours += 12;

Reading Delimiters

Right now, the code to initially read the data is made somewhat ugly and unreadable (and arguably failure-prone) by it's somewhat poor handling of delimiters. It currently just reads a character and assumes it's a delimiter. In real life, you probably want to at least compare what you read to what you expected so ensure that the input is correctly formatted (and react appropriately if it's not).

...and the operator overload takes care of reading data, comparing to what was passed, and setting the stream's failbit if it didn't match. As it stands right now, I've also included skipping any leading white-space like normal extraction operators do, but it would be trivial to eliminate that if you didn't want it (but it shouldn't make any difference for solving the HackerRank problem).

1. HackerRank is apparently using an old/broken standard library, so it won't accept this, even when you try to tell it you want to use C++14.

\$\begingroup\$I understand you're probably trying to keep it simple, but for the get_time version, you'd probably want to initialize t. I'd omit struct and just write std::tm t{};. Also, in Real Life, we'd check std::cout.fail() to see if the conversion was successful, right? Finally, just to be pedantic, you'd first want to imbue the stream with a locale that supports AM/PM time -- not all do and without specifying, it's up to the current operating system settings.\$\endgroup\$
– EdwardMay 31 '16 at 17:40

\$\begingroup\$@Edward: You'd probably want to check cin.fail. If cout fails, chances are good you can't do much (though it may be possible for cerr to continue working even when/if cout has failed). As far as locales go, it always starts out in the "C" locale, so at least in this case (doing the reading immediately upon entry in main) there's no need for an explicit imbue. Other than that though, yes.\$\endgroup\$
– Jerry CoffinMay 31 '16 at 18:44

Declarations

Always do declarations at the top of a function, even if you do not initialize that variable right there and then. This helps you figure out what a type of a variable is more easily when reading back the code.

You are treating any string that is not "PM" as if it is "AM". Obviously any string can be entered behind your time. You should check if it is "PM" or "AM", possibly in the same if-statement as above.

Includes everywhere

You are including a lot of libraries. Only iostream and iomanip are required. In your code you probably need sstream too. You are not using vectors, or math, or c-style input/output such as fprintf, so don't include them.