Spread the word.

Share the link on social media.

Share
  • Facebook
Have an account? Sign In Now

Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

SIKSHAPATH

SIKSHAPATH Logo SIKSHAPATH Logo

SIKSHAPATH Navigation

  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Home/ Questions/Q 16154
Next
In Process

SIKSHAPATH Latest Questions

Pavanagg
  • 1
  • 1
Pavanagg
Asked: April 25, 20222022-04-25T19:39:14+05:30 2022-04-25T19:39:14+05:30In: C++

Write a templated class named “EveryOther” that behaves much like …

  • 1
  • 1

Write a templated class named “EveryOther” that behaves much like std::vector (in the file named “everyother.h”). It has a member function called “push_back”, but only every other call to the member function actually does anything (actually pushed the element to the end of the structure). The class should have a default constructor and a constructor that takes an initializer list (be sure that you only add every other element). The other function that EveryOther needs to support is the operator<<, see the test cases for formating.

This class is templated so that instances like EveryOther<char> and EveryOther<vector<int>> are supported.

Input for testing the constructor:

#include<string>
EveryOther<char> eo_1;
EveryOther<std::string> eo_2 {“include”,”don’t”,”keep”,”leave”};

c
  • 1 1 Answer
  • 236 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    • Oldest
    • Recent
    1. ADMIN 2
      ADMIN 2
      2022-04-25T19:51:14+05:30Added an answer on April 25, 2022 at 7:51 pm
      This answer was edited.

      filename: everyother.h

      #pragma once
      #include <iostream>
      #include <vector>
      #include <sstream>
      #include <initializer_list>
      using std::initializer_list;
      using std::vector;
      using std::ostream;
      using std::cout;
      template<typename T>
      class EveryOther;
      
      template<typename T>
      ostream & operator<<(ostream & o, const EveryOther<T>& e);
      
      template <class T>
      class EveryOther{
      public:
      vector<T>vec_;
      EveryOther()=default;
      EveryOther(initializer_list<T> c): vec_(c){};
      void push_back(T var){
      vec_.push_back(var);
      }
      
      int size(){
      vector<T>vec1;
      for(size_t i = 0; i < vec_.size(); i+=2){
      vec1.push_back(vec_[i]);
      }
      return vec1.size();
      }
      friend ostream & operator<< <T>(ostream &, const EveryOther&);
      
      };
      template<typename T>
      ostream & operator<<(ostream & o,const EveryOther<T>& e){
      o << "EveryOther(";
      for(size_t i = 0; i < e.vec_.size(); i+=2){
      o << e.vec_[i] << ", ";
      }
      o << ')';
      return o;
      }

      filename: main.cpp

      #include <iostream>
      #include "everyother.h"
      int main() 
      {
      EveryOther<char> eo_1;
      EveryOther<std::string> eo_2 {"include", "dont", "keep", "leave"};
      
      std::cout << eo_2 << std::endl;
      }

      Output EveryOther(include, keep, )

        • 1
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    store ads

    Stats

    • Questions 1k
    • Answers 1k
    • Posts 149
    • Best Answers 89
    • This Free AI Tool Translates Entire Books in Minute !
    • AI News: 🎬 Hollywood’s AI Studios, 🎓 OpenAI’s Latest Gift to Educators, 🚚 Class8 Bags $22M, 🧠 Google Gemini’s Memory Upgrade
    • AI NEWS: Legal Action Against OpenAI, $16M Paid, & Elon Musk’s Praise from Investor 🤖💰📑 | AI Boosts Cloud Seeding for Water Security 🌱💧
    • AI News: 🎬AI Video Tool Scam Exposed🤯, 🛰️ AI-Powered Drones to Ukraine 😱, Google’s $20M AI Push, Sam Altman Joins SF’s Leadership Team
    • AI News: 🤝 Biden Meets Xi on AI Talks, 💡 Xavier Niel’s Advice for Europe, ♻️ Hong Kong’s Smart Bin Revolution, 🚀 AI x Huawei

    Explore

    • Recent Questions
    • Questions For You
    • Answers With Time
    • Most Visited
    • New Questions
    • Recent Questions With Time

    Footer

    SIKSHAPATH

    Helpful Links

    • Contact
    • Disclaimer
    • Privacy Policy Notice
    • TERMS OF USE
    • FAQs
    • Refund/Cancellation Policy
    • Delivery Policy for Sikshapath

    Follow Us

    © 2021-24 Sikshapath. All Rights Reserved

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.