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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:30:51+00:00 2026-05-27T01:30:51+00:00

I have a method to find number in range of iterator like below. I

  • 0

I have a method to find number in range of iterator like below. I want it has reference parameter and return reference of iterator.

// find number in range of begin and end, if number found return reference of iterator, throw exception otherwise
vector<int>::iterator &find_int(vector<int>::iterator &begin, vector<int>::iterator &end, int &number)
{
    for (; begin != end; ++begin)
    {
        if (*begin == number)
        {
            return begin;
        }
    }

    throw "not found";
}

I want to call this method as following code but it can’t compile.

vector<int>::iterator i5 = find_int(vi1.begin(), vi1.end(), 5);

I want to know how can I call find_int method ?

Update
Error message when compile the code

C:\Windows\system32\cmd.exe /c g++ c:\cpp\ch9.cpp -o c:\cpp\ch9.exe
c:\cpp\ch9.cpp: In function 'int main()':
c:\cpp\ch9.cpp:41:63: error: invalid initialization of non-const reference of ty
pe 'std::vector<int>::iterator& {aka __gnu_cxx::__normal_iterator<int*, std::vec
tor<int> >&}' from an rvalue of type 'std::vector<int>::iterator {aka __gnu_cxx:
:__normal_iterator<int*, std::vector<int> >}'
c:\cpp\ch9.cpp:30:24: error: in passing argument 1 of 'std::vector<int>::iterato
r& find_int(std::vector<int>::iterator&, std::vector<int>::iterator&, int&)'
shell returned 1
Hit any key to close this window...
  • 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-27T01:30:52+00:00Added an answer on May 27, 2026 at 1:30 am

    That function really shouldn’t be taking iterator&‘s (or the int&1) anyway. It should take the arguments by value, and return the iterator by value.

    Even if you didn’t get a compiler error because of the int literal you’re trying to pass it (5) where it expects an int&1, the reason you can’t call it the way you’re doing is because you would be passing a reference to a temporary value, then getting a reference to that temporary through the return value of the function, and by the next line of code, the reference would refer to a destroyed object.

    You can call it like this:

    vector<int>::iterator beg = vi1.begin(), end = vi1.end();
    
    vector<int>::iterator& pos = find_int(beg, end, 5);
    

    Note that &pos == &beg.

    However, if this isn’t a didactic exercise, I would recommend to do what shuttle87 suggested, which is to use std::find.


    1 Thanks Ghita for pointing this out.

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

Sidebar

Related Questions

I have a method in my class that takes a parameter that's a number.
I have a method ... where I can't find the error: public String getUsernameforID(int
For the last few days I have been attempting to find a method to
I have been unable to find any documentation on the .build method in Rails
I have a method that where I want to redirect the user back to
I have this method that receives an ID number and downloads an HTML website
I have a method that calls a stored procedure. It uses the employee number
What is the best method to find the number of digits of a positive
How can I count number of steps between different numbers. I have a method
Right now I have a 2d string array holding my data: //...find number of

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.