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

  • SEARCH
  • Home
  • 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 6961643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:33:26+00:00 2026-05-27T15:33:26+00:00

I’m having some difficulties with the below generic function, I want this function to

  • 0

I’m having some difficulties with the below generic function, I want this function to take 2 iterators and a value. The function should iterate between the 2 iterators and check for any occurrences of the value and count the occurrences. But I get the following error:

no matching function for call to ‘count_c(int, std::vector::iterator, std::vector::iterator)’

 template<typename Container>
 int count_c(Container i, typename Container::iterator &start, typename Container::iterator &end){
     typedef typename Container::iterator Iter;
     int count;
     for(Iter p = start; p != end; p++){
       if((*p) == i){
       count = count + 1;
     }
     return count;
 }


int main(){
  vector<double> myv;
  myv.push_back(9);
  myv.push_back(10);
  count_c(9, myv.begin(), myv.end());
  return 0;
}

It’s part of an exam question:

Write a generic function count which:

  • Takes as parameters a value and two iterators of a container (where to start from and
    where to finish in the container).

  • It uses the iterators to go through the elements of the container and count the occur-
    rences of the value in the container.

  • Finally, it returns the number of occurrences of the value it has found.

The two iterators are not necessarily the same as what is returned by a con-
tainer’s begin() and end() methods!

  • 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-27T15:33:26+00:00Added an answer on May 27, 2026 at 3:33 pm

    1) Don’t pass the iterators in by reference.

    Why? Because you can’t bind references to rvalue expressions, which begin() and end() are. Besides, there’s not much point in doing so, as iterators are very small and faster to copy by value.

    2) Use the iterator type, not the container.

    9 is not a container, and so lacks an iterator type. There is no reason to need the container, as iterators were designed to work without them. Some iterators don’t even have a container, such as plain ol pointers. You can always call your function as count_c<foo::iterator>(...) if you want to specify the type of iterator.

     template<typename iter_t>
     int count_c(int i, iter_t start, iter_t end){
         int count = 0;
         for(iter_t p = start; p != end; p++){
          if((*p) == i){
          count = count + 1;
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.