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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:05:56+00:00 2026-05-11T19:05:56+00:00

Working on an algorithm to look at a STL container of STL strings (or

  • 0

Working on an algorithm to look at a STL container of STL strings (or other strings, making it general)

Basically it loops through something like a std::list and returns the length of the longest beginning in common. It’s for processing lists of files, like this:

C:\Windows\System32\Stuff.exe
C:\Windows\Things\InHere.txt
C:\Windows\Foo\Bar.txt

This should return 11, because “C:\Windows\” is in common.

Never written a templatized function before, and my compiler is complaining. Here’s my code:
Header:

// longestBegin.h -- Longest beginning subsequence solver
template <typename SequenceSequenceT, typename SequenceT, typename T >
size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates);

Implementation:

// longestBegin.cpp -- Longest beginning subsequence solver
#include <stdafx.h>

template <typename SequenceSequenceT, typename SequenceT, typename T >
size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates)
{
    SequenceT firstString = *firstCandidates;
    size_t longestValue = firstString.length();
    firstCandidates++;
    for(size_t idx = 0; idx < longestValue; idx++)
    {
        T curChar = firstString[idx];
        for(InputIterator curCandidate = firstCandidates;curCandidate != lastCandidates; curCandidate++)
        {
            if ((*curCandidate)[idx] != curChar)
                return idx - 1;
        }
    }
    return longestValue;
}

I have a funny feeling I’m missing something fundamental here……

The compiler bombs with the following error:

error C2998: 'size_t longestBegin' : cannot be a template definition

Any ideas? Thanks!

Billy3

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

    Your parameter names in the template line need to include any types of function parameters or return types. This means that you need to mention InputIterator in your template parameter list. Try changing your function declaration to:

    
    template <typename InputIterator>
    size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates)
    

    Your next problem is: how does the compiler know what SequenceT is? The answer is that it’s the result of dereferencing an InputIterator. Iterators that aren’t pointers have a nested typedef called reference, which is just what you need here. Add this to the start of your function so the compiler knows what SequenceT is:

    
    template <typename InputIterator>
    size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates)
    {
        typedef typename InputIterator::reference SequenceT;
    [etc.]
    

    You could have kept SequenceT as a template parameter, but then the compiler couldn’t guess what it is from looking at the arguments, and you’d have to call your function by typing e.g. longestBegin<string>(arguments), which isn’t necessary here.

    Also, you’ll notice that this doesn’t work if InputIterator is a pointer — pointers don’t have nested typedefs. So you can use a special struct called std::iterator_traits from the <iterator> standard header that can sort out these problems for you:

    
    //(At the top of your file)
    #include <iterator>
    
    template <typename InputIterator>
    size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates)
    {
        typedef typename std::iterator_traits<InputIterator>::reference SequenceT;
    [etc.]
    

    Finally, unless the first string is always the longest, you could end up accessing a string past the end of its array inside the second for loop. You can check the length of the string before you access it:

    
    //(At the top of your file)
    #include <iterator>
    
    template <typename InputIterator>
    size_t longestBegin(InputIterator firstCandidates, InputIterator lastCandidates)
    {
        typedef typename std::iterator_traits<InputIterator>::reference SequenceT;
        SequenceT firstString = *firstCandidates;
        size_t longestValue = firstString.length();
        firstCandidates++;
        for(size_t idx = 0; idx < longestValue; idx++)
        {
            T curChar = firstString[idx];
            for(InputIterator curCandidate = firstCandidates;curCandidate != lastCandidates; curCandidate++)
            {
                    if (curCandidate->size() >= idx || (*curCandidate)[idx] != curChar)
                            return idx - 1;
            }
        }
        return longestValue;
    }
    

    Also note that the function returns (size_t)(-1) if there is no common prefix.

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

Sidebar

Related Questions

I am working on an algorithm that will try to pick out, given an
I need a good stemming algorithm for a project I'm working on. It was
I am working on a community detection algorithm for analyzing social network data from
I am working on an algorithm that performs a global thresholding of an 8-bit
What is the most general and simple approach to make look fatter a face
I've been working on a graph traversal algorithm over a simple network and I'd
I'm working on a pagination algorithm in PHP. I can guess that it needs
I'm working on a project where I would like to reconstruct the 3D locations
I'm working with a couple of AI algorithms at school and I find people
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch

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.