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 6358907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:26:55+00:00 2026-05-24T23:26:55+00:00

So recently I got into a debate about how to solve a problem, the

  • 0

So recently I got into a debate about how to solve a problem, the problem specifically was: How do I find all the pallindromes between 1 and 1 million. I said, “Use atoi to make a string, use a for loop to reverse the string, the use strcmp to compare the string(s) in question.

A few minutes later someone asked “Why would you use a C-style solution in C++.” I found myself confused of a simple, more “C++” way of solving this with code as direct and easy to understand. Anyone care to illuminate me on this one?

edit: itoa not atoi

  • 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-24T23:26:56+00:00Added an answer on May 24, 2026 at 11:26 pm

    Example C++ solution:

    #include <algorithm>
    #include <iterator>
    #include <iostream>
    #include <boost/iterator/counting_iterator.hpp>
    #include <boost/lexical_cast.hpp>
    
    namespace {
       bool is_palindrome(unsigned int i) {
         const std::string& s = boost::lexical_cast<std::string>(i);
         return std::equal(s.begin(), s.end(), s.rbegin());
       }
    
       const unsigned int stop = 1000000;
    }
    
    int main() {
       std::remove_copy_if(boost::counting_iterator<unsigned int>(1),
                           boost::counting_iterator<unsigned int>(stop),
                           std::ostream_iterator<unsigned int>(std::cout, "\n"),
                           std::not1(std::ptr_fun(is_palindrome)));
    }
    

    (I used std::remove_copy_if to make up for the lack of std::copy_if which is in C++0x)

    For completeness sake I implemented a version that generates the palindromes rather than testing candidates against a predicate:

    #include <boost/lexical_cast.hpp>
    #include <boost/iterator/counting_iterator.hpp>
    #include <boost/iterator/transform_iterator.hpp>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    #include <cassert>
    
    namespace {
      template <int ver>
      unsigned int make_palindrome(unsigned int i) {
         std::string s = boost::lexical_cast<std::string>(i);
         assert(s.size());
         s.reserve(s.size()*2);
         std::reverse_copy(s.begin(), s.end()-ver, std::back_inserter(s));
         return boost::lexical_cast<unsigned int>(s);
      }
    }
    
    int main() {
       typedef boost::counting_iterator<unsigned int> counter;
       std::merge(boost::make_transform_iterator(counter(1), make_palindrome<1>),
                  boost::make_transform_iterator(counter(999), make_palindrome<1>),
                  boost::make_transform_iterator(counter(1), make_palindrome<0>),
                  boost::make_transform_iterator(counter(999), make_palindrome<0>),
                  std::ostream_iterator<unsigned int>(std::cout, "\n"));
    }
    

    (I could have used boost.range I think instead of std::merge for this)

    The discussion point from this I guess then is “is this a better* way to write it?”. The thing I like about writing problems like the palindrome in this style is you get the “if it compiles it’s probably correct” heuristic on your side. Even if there is a bug it’ll still get handled sensibly at run time (e.g. an exception from lexical_cast).

    It’s a markedly different way of thinking from C programming (but strangely similar to Haskell in some ways). It brings benefits in the form of lots of extra safety, but the compiler error messages can be terrible and shifting the way you think about problems is hard.

    At the end of it all though what matters is “is it less work for less bugs?”. I can’t answer that without some metrics to help.

    * For some definition of better.

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

Sidebar

Related Questions

Recently I got into a discussion with my Team lead about using temp variables
I got into a mini-argument with my boss recently regarding project failure. After three
I recently got into Java. I have a background in dynamic languages and I'm
I recently got into Silverlight development. So far I've managed to create a single
I am programming in PHP mysql. I have recently got into OOP programming. So
I've dabbled with Haskell in the past, and recently got back into it seriously,
I have been working with python for a while now. Recently I got into
I recently got a notification from a McAfee service (what used to be called
I recently got involved in a Java project at work: we're using MyEclipse for
I just recently got my first mac. I do lots of programming on windows

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.