Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

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

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:39:57+00:00 2026-05-15T05:39:57+00:00

I have three questions regarding a homework assignment for C++. The goal was to

  • 0

I have three questions regarding a homework assignment for C++. The goal was to create a simple palindrome method. Here is my template for that:

#ifndef PALINDROME_H
#define PALINDROME_H

#include <vector>
#include <iostream>
#include <cmath>

template <class T>
static bool palindrome(const std::vector<T> &input)
{
    std::vector<T>::const_iterator it = input.begin();
    std::vector<T>::const_reverse_iterator rit = input.rbegin();

    for (int i = 0; i < input.size()/2; i++, it++, rit++)
    {
        if (!(*it == *rit)) {
            return false;   
        }
    }
    return true;
}

template <class T>
static void showVector(const std::vector<T> &input)
{

    for (std::vector<T>::const_iterator it = input.begin(); it != input.end(); it++) {
        std::cout << *it << " ";
    }
}

#endif

Regarding the above code, can you have more than one iterator declared in the first part of the for loop? I tried defining both the “it” and “rit” in the palindrome() method, and I kept on getting an error about needing a “,” before rit. But when I cut and paste outside the for loop, no errors from the compiler. (I’m using VS 2008).

Second question, I pretty much just brain farted on this one. But is the way I have my return statements in the palindrome() method ok? In my head, I think it works like, once the *it and *rit do not equal each other, then the function returns false, and the method exits at this point. Otherwise if it goes all the way through the for loop, then it returns true at the end. I totally brain farted on how return statements work in if blocks and I tried looking up a good example in my book and I couldn’t find one.

Finally, I get this warnings:

\palindrome.h(14) : warning C4018: '<' : signed/unsigned mismatch

Now is that because I run my for loop until (i < input.size()/2) and the compiler is telling me that input can be negative? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:39:58+00:00Added an answer on May 15, 2026 at 5:39 am

    can you have more than one iterator declared in the first part of the for loop?

    Yes, but they both have to be of the same type, so you can’t declare both a const_iterator and a const_reverse_iterator.

    is the way I have my return statements in the palindrome() method ok?

    Yes, though why not just compare *it != *rit?

    palindrome.h(14) : warning C4018: '<' : signed/unsigned mismatch
    

    i is signed; std::vector::size() returns an unsigned value. If i was unsigned, you would not get this warning.

    As a suggestion, though: it might be simpler to use two forward iterators. Initialize one to .begin() and the other to .end() - 1. You can then increment the first and decrement the second and your loop test simply becomes it1 < it2. Something like the following (completely untested) for-loop:

    for (iterator it1(v.begin()), it2(v.end() - 1); it1 < it2; ++it1, --it2)
    

    This way you no longer need the separate i counter and comparisons; everything is done with iterators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.