Class Template Specialization in C++ Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Class Template Specialization in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Class Template Specialization in C++ - Hackerrank Solution

Objective

You are given a main function which reads the enumeration values for two different types as input, then prints out the corresponding enumeration names. Write a class template that can provide the names of the enumeration values for both types. If the enumeration value is not valid, then print unknown.


Input Format :

The first line contains t, the number of test cases.
Each of the t subsequent lines contains two space-separated integers. The first integer is a color value, c, and the second integer is a fruit value, f.

Constraints :

  • 1<= t <=100
  • -2*10^9 <= c <= 2*10^9
  • -2*10^9 <= f <= 2*10^9

Output Format:

The locked stub code in your editor prints t lines containing the color name and the fruit name corresponding to the input enumeration index.


Sample Input :

2
1 0
3 3

Sample Output :

green apple
unknown unknown

Explanation :

Since t = 2, there are two lines of output.

  1. The two input index values, 1 and 0, correspond to green in the color enumeration and apple in the fruit enumeration. Thus, we print green apple.
  2. The two input values, 3 and 3, are outside of the range of our enums. Thus, we print unknown unknown.
C++ Class Template Specialization in C++ – Hacker Rank Solution
#include <iostream>
using namespace std;
enum class Fruit { apple, orange, pear };
enum class Color { red, green, orange };

/* C++ Class Template Specialization in C++ - Hacker Rank Solution START */
template <typename T> struct Traits;
char s []="unknown";
char f0 []="apple";
char f1 []="orange";
char f2 []="pear";
char c0 []="red";
char c1 []="green";
char c2 []="orange";
template <>
struct Traits<Fruit>
{
    public:
    static char* name(int a)
    {
        if(a>=3 || a<0)
            return s;
        else if (a==0)
            return f0;
        else if (a==1)
            return f1;
        else
            return f2;            
    }
};

template <>
struct Traits<Color>
{
    public:
    static char* name(int a)
    {
        if(a>=3 || a<0)
            return s;
        else if (a==0)
            return c0;
        else if (a==1)
            return c1;
        else
            return c2;  
    }
};
/* C++ Class Template Specialization in C++ - Hacker Rank Solution END */

int main()
{
	int t = 0; std::cin >> t;

    for (int i=0; i!=t; ++i) {
        int index1; std::cin >> index1;
        int index2; std::cin >> index2;
        cout << Traits<Color>::name(index1) << " ";
        cout << Traits<Fruit>::name(index2) << "\n";
    }
}

954 thoughts on “Class Template Specialization in C++ Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions”

  1. You really make it seem so easy with your presentation but I find this matter to be actually one thing that I believe I would never understand. It seems too complicated and extremely large for me. I’m looking ahead to your next publish, I¦ll attempt to get the cling of it!

    Reply
  2. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

    Reply
  3. Hi this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

    Reply
  4. I believe this is among the such a lot significant info for me. And i am satisfied studying your article. But wanna observation on some general issues, The website style is ideal, the articles is in point of fact nice : D. Excellent task, cheers

    Reply
  5. Once I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get 4 emails with the same comment. Is there any method you possibly can remove me from that service? Thanks!

    Reply
  6. Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

    Reply
  7. Hi, i think that i saw you visited my web site thus i came to “return the favor”.I am trying to find things to improve my website!I suppose its ok to use a few of your ideas!!

    Reply
  8. I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to construct my own blog and would like to know where u got this from. many thanks

    Reply
  9. Great ?V I should definitely pronounce, impressed with your site. I had no trouble navigating through all the tabs and related information ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your client to communicate. Nice task..

    Reply
  10. I believe this site has some really wonderful information for everyone. “Je veux que les paysans mettent la poule au pot tous les dimanches.” by King Henry IV of France.

    Reply
  11. First off I want to say great blog! I had a quick question in which I’d like to ask if you don’t mind. I was curious to know how you center yourself and clear your thoughts before writing. I have had a difficult time clearing my mind in getting my thoughts out. I do enjoy writing but it just seems like the first 10 to 15 minutes are usually wasted just trying to figure out how to begin. Any ideas or tips? Thank you!

    Reply
  12. It is perfect time to make a few plans for the longer term and it is time to be happy. I have read this post and if I may I wish to recommend you few interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things approximately it!

    Reply
  13. My brother suggested I might like this website. He was totally right. This post actually made my day. You cann’t imagine just how much time I had spent for this information! Thanks!

    Reply
  14. I do believe all the concepts you have introduced for your post. They are very convincing and will definitely work. Still, the posts are too quick for novices. May you please prolong them a bit from next time? Thank you for the post.

    Reply
  15. Hi, I do believe this is an excellent website. I stumbledupon it 😉 I will come back once again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.

    Reply
  16. Sweet blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it

    Reply
  17. I’m impressed, I must say. Rarely do I encounter a blog that’s both educative and interesting, and let me tell you, you have hit the nail on the head. The issue is something not enough people are speaking intelligently about. I am very happy that I found this in my search for something concerning this.

    Reply
  18. It is perfect time to make a few plans for the future and it is time to be happy. I have read this post and if I may I want to recommend you few interesting things or advice. Perhaps you could write next articles referring to this article. I wish to read more things approximately it!

    Reply
  19. I have been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.

    Reply
  20. Great goods from you, man. I’ve understand your stuff previous to and you’re just too great. I really like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it entertaining and you still take care of to keep it sensible. I can not wait to read far more from you. This is actually a wonderful site.

    Reply
  21. I blog quite often and I genuinely appreciate your content. This great article has really peaked my interest. I am going to bookmark your site and keep checking for new information about once a week. I subscribed to your RSS feed as well.

    Reply
  22. An impressive share! I have just forwarded this onto a coworker who was doing a little research on this. And he in fact bought me lunch because I discovered it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending time to discuss this issue here on your site.

    Reply
  23. Hmm it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to the whole thing. Do you have any points for rookie blog writers? I’d definitely appreciate it.

    Reply
  24. You can definitely see your enthusiasm in the article you write. The arena hopes for more passionate writers like you who aren’t afraid to mention how they believe. Always go after your heart.

    Reply
  25. Hey there! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My blog looks weird when viewing from my iphone 4. I’m trying to find a theme or plugin that might be able to fix this problem. If you have any suggestions, please share. Cheers!

    Reply
  26. What i do not realize is actually how you’re now not really a lot more smartly-appreciated than you may be right now. You are so intelligent. You know therefore significantly on the subject of this topic, produced me in my opinion believe it from numerous numerous angles. Its like men and women don’t seem to be fascinated until it’s something to accomplish with Woman gaga! Your personal stuffs nice. Always deal with it up!

    Reply
  27. Hi there exceptional blog! Does running a blog like this take a lot of work? I have virtually no knowledge of computer programming but I was hoping to start my own blog soon. Anyway, if you have any recommendations or tips for new blog owners please share. I know this is off topic but I just needed to ask. Thank you!

    Reply
  28. My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am anxious about switching to another platform. I have heard excellent things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be really appreciated!

    Reply
  29. Hey There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will definitely comeback.

    Reply
  30. Thank you for any other magnificent article. Where else may anyone get that kind of information in such a perfect method of writing? I have a presentation next week, and I am at the look for such information.

    Reply
  31. you are really a good webmaster. The web site loading velocity is incredible. It sort of feels that you are doing any unique trick. Moreover, The contents are masterpiece. you have performed a great process in this matter!

    Reply
  32. Hello there! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My web site looks weird when viewing from my iphone4. I’m trying to find a theme or plugin that might be able to fix this problem. If you have any suggestions, please share. Thank you!

    Reply
  33. With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My website has a lot of completely unique content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement. Do you know any solutions to help reduce content from being ripped off? I’d truly appreciate it.

    Reply
  34. Greetings from Colorado! I’m bored to tears at work so I decided to check out your site on my iphone during lunch break. I really like the knowledge you present here and can’t wait to take a look when I get home. I’m shocked at how quick your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyhow, good site!

    Reply
  35. This is the right web site for anybody who wishes to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want toHaHa). You definitely put a brand new spin on a topic that’s been written about for many years. Great stuff, just excellent!

    Reply
  36. Hi! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any solutions to prevent hackers?

    Reply
  37. An impressive share! I have just forwarded this onto a colleague who had been doing a little research on this. And he in fact bought me breakfast because I found it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending time to discuss this topic here on your web site.

    Reply
  38. I am extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it’s rare to see a nice blog like this one nowadays.

    Reply
  39. We stumbled over here different website and thought I might check things out. I like what I see so now i am following you. Look forward to finding out about your web page for a second time.

    Reply
  40. Magnificent goods from you, man. I’ve have in mind your stuff prior to and you’re simply too magnificent. I really like what you’ve got here, really like what you’re stating and the way during which you assert it. You are making it entertaining and you still take care of to stay it smart. I can not wait to read far more from you. This is actually a terrific site.

    Reply
  41. You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

    Reply
  42. Woah! I’m really enjoying the template/theme of this site. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between superb usability and visual appearance. I must say that you’ve done a superb job with this. In addition, the blog loads extremely fast for me on Safari. Superb Blog!

    Reply
  43. I’ve been exploring for a little for any high-quality articles or blog posts in this kind of space . Exploring in Yahoo I at last stumbled upon this web site. Reading this info So i’m satisfied to exhibit that I have a very just right uncanny feeling I came upon exactly what I needed. I such a lot certainly will make certain to don?t fail to remember this web site and give it a look on a constant basis.

    Reply
  44. Хотите заказать стяжку пола в Москве, но не знаете, где искать надежного подрядчика? Обратитесь к нам на сайт styazhka-pola24.ru! Мы предоставляем услуги по устройству стяжки пола м2 по доступной стоимости, а также устройству стяжки пола под ключ.

    Reply
  45. Хотите получить идеально ровные стены в своей квартире или офисе? Обратитесь к профессионалам на сайте mehanizirovannaya-shtukaturka-moscow.ru! Мы предоставляем услуги по механизированной штукатурке стен в Москве и области, а также гарантируем качество работ и доступные цены.

    Reply
  46. Hi there would you mind stating which blog platform you’re working with? I’m looking to start my own blog in the near future but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!

    Reply
  47. Howdy I am so thrilled I found your web site, I really found you by error, while I was browsing on Aol for something else, Anyhow I am here now and would just like to say many thanks for a incredible post and a all round enjoyable blog (I also love the theme/design), I dont have time to read through it all at the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the awesome b.

    Reply
  48. Have you ever considered about including a little bit more than just your articles? I mean, what you say is fundamental and all. However think of if you added some great photos or video clips to give your posts more, “pop”! Your content is excellent but with images and video clips, this website could certainly be one of the very best in its niche. Very good blog!

    Reply
  49. What i do not realize is in fact how you’re not really a lot more well-liked than you may be right now. You are so intelligent. You know therefore significantly in terms of this topic, produced me individually believe it from numerous numerous angles. Its like men and women aren’t interested until it’s something to accomplish with Lady gaga! Your own stuffs nice. Always take care of it up!

    Reply
  50. Hello there I am so excited I found your weblog, I really found you by mistake, while I was browsing on Askjeeve for something else, Anyhow I am here now and would just like to say kudos for a fantastic post and a all round enjoyable blog (I also love the theme/design), I dont have time to look over it all at the minute but I have saved it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the superb jo.

    Reply
  51. With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My site has a lot of completely unique content I’ve either authored myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any solutions to help protect against content from being ripped off? I’d certainly appreciate it.

    Reply
  52. hey there and thank you for your information I’ve definitely picked up anything new from right here. I did however expertise some technical issues using this web site, since I experienced to reload the site a lot of times previous to I could get it to load properly. I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and can damage your high quality score if advertising and marketing with Adwords. Anyway I’m adding this RSS to my e-mail and can look out for a lot more of your respective fascinating content. Make sure you update this again soon.

    Reply
  53. I think what you postedtypedsaidthink what you postedwrotesaidthink what you postedwrotesaidbelieve what you postedtypedWhat you postedwrote was very logicala lot of sense. But, what about this?think about this, what if you were to write a killer headlinetitle?content?wrote a catchier title? I ain’t saying your content isn’t good.ain’t saying your content isn’t gooddon’t want to tell you how to run your blog, but what if you added a titlesomethingheadlinetitle that grabbed people’s attention?maybe get a person’s attention?want more? I mean %BLOG_TITLE% is a little plain. You might peek at Yahoo’s home page and watch how they createwrite post headlines to get viewers to click. You might add a video or a pic or two to get readers interested about what you’ve written. Just my opinion, it might bring your postsblog a little livelier.

    Reply
  54. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Reply
  55. Fantastic blog! Do you have any hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally confused .. Any suggestions? Cheers!

    Reply
  56. My spouse and I absolutely love your blog and find the majority of your post’s to be just what I’m looking for. Does one offer guest writers to write content for yourself? I wouldn’t mind composing a post or elaborating on most of the subjects you write about here. Again, awesome web site!

    Reply
  57. My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am worried about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be really appreciated!

    Reply
  58. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Reply
  59. Pretty component to content. I simply stumbled upon your blog and in accession capital to claim that I acquire in fact enjoyed account your blog posts. Any way I’ll be subscribing for your augment or even I fulfillment you access consistently fast.

    Reply
  60. Hey There. I found your blog the use of msn. This is an extremely smartly written article. I will be sure to bookmark it and come back to read more of your useful information. Thank you for the post. I will definitely comeback.

    Reply
  61. I simply could not leave your web site prior to suggesting that I extremely enjoyed the standard information a person supply in your visitors? Is going to be back incessantly in order to check up on new posts

    Reply
  62. I blog frequently and I truly appreciate your content. The article has really peaked my interest. I will bookmark your site and keep checking for new information about once a week. I subscribed to your RSS feed as well.

    Reply
  63. Awesome website you have here but I was wanting to know if you knew of any message boards that cover the same topics talked about in this article? I’d really love to be a part of group where I can get feed-back from other knowledgeable individuals that share the same interest. If you have any recommendations, please let me know. Kudos!

    Reply
  64. My partner and I absolutely love your blog and find many of your post’s to be just what I’m looking for. Would you offer guest writers to write content for you personally? I wouldn’t mind composing a post or elaborating on some of the subjects you write in relation to here. Again, awesome blog!

    Reply
  65. Heya just wanted to give you a quick heads up and let you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different browsers and both show the same results.

    Reply
  66. I truly love your site.. Very nice colors & theme. Did you make this site yourself? Please reply back as I’m planning to create my own website and would like to know where you got this from or exactly what the theme is called. Many thanks!

    Reply
  67. What i do not realize is in fact how you’re not really a lot more well-appreciated than you may be right now. You are so intelligent. You recognize therefore significantly in terms of this matter, produced me for my part consider it from so many numerous angles. Its like men and women don’t seem to be fascinated unless it’s something to accomplish with Lady gaga! Your own stuffs nice. All the time deal with it up!

    Reply
  68. Can I simply say what a relief to find an individual who truly knows what they’re talking about on the net. You certainly understand how to bring an issue to light and make it important. More and more people need to read this and understand this side of the story. I can’t believe you’re not more popular since you certainly have the gift.

    Reply
  69. This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your magnificent post. Also, I have shared your web site in my social networks!

    Reply
  70. An impressive share! I have just forwarded this onto a colleague who was doing a little research on this. And he in fact bought me lunch simply because I found it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending time to discuss this topic here on your web page.

    Reply
  71. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Appreciate it!

    Reply
  72. Нужна машинная штукатурка стен в Москве, но вы не знаете, какой подрядчик лучше выбрать? Обратитесь к нам на сайт mehanizirovannaya-shtukaturka-moscow.ru! Мы предоставляем услуги по механизированной штукатурке стен любой площади и сложности, а также гарантируем качество и надежность.

    Reply
  73. Excellent post. I used to be checking continuously this blog and I am inspired! Very useful information specially the remaining phase 🙂 I care for such info a lot. I used to be seeking this particular info for a long timelong time. Thank you and good luck.

    Reply
  74. I’m now not sure where you are getting your info, however good topic. I needs to spend a while studying more or understanding more. Thank you for wonderful information I used to be on the lookout for this information for my mission.

    Reply
  75. I think this is one of the such a lot significant information for me. And i’m satisfied reading your article. However want to observation on few common things, The website taste is great, the articles is really nice : D. Just right process, cheers

    Reply