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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:37:35+00:00 2026-05-25T22:37:35+00:00

I’m new to vector in C++, and I’m using pointer in it. I’d like

  • 0

I’m new to vector in C++, and I’m using pointer in it.
I’d like to search a variable if it already exists in the vector but I’m not sure how to do it.

B.cpp

vector<Animal*> vec_Animal;
vector<Animal*>::iterator ite_Animal;

What I’m trying to compare is Animal->getID();

And I have one more question.
Is there any way to make a limit when a user inputs value?
What I mean by is that if there’s a value year then, I want it to be typed 1000~2011 only. If user puts 999, it’d be wrong.
Is it possible?

Cheers

  • 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-25T22:37:36+00:00Added an answer on May 25, 2026 at 10:37 pm

    You can use the std::find_if algorithm.

    Probably, You are using std::vector::push_back or such methods to fill up the vector, These methods do not provide any checks, but one way to do achieve this is, by writing a small wrapper function inside which you check for the valid data conditions, and if the data is good then you add that in the vector or else you just return some error or throw std::out_of_range exception from your wrapper function.


    Online Demo

    Here is a minimilastic code sample, ofcourse you will need t tweak it further to suit your need:

    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    class Animal
    {
    public: 
        int id;
    };
    
    class Ispresent
    {
        public:
        int m_i;
        Ispresent(int i):m_i(i){}
    
        bool operator()(Animal *ptr) 
        {
            cout<<"\n\nInside IsPresent:"<<ptr->id;
            return (ptr->id == m_i);
        }
    };
    
    int main()
    {
        vector<Animal*> vec_Animal;
    
        Animal *ptr = new Animal();
        ptr->id = 10;
        vec_Animal.push_back(ptr);
    
        Animal *ptr1 = new Animal();
        ptr1->id = 20;
        vec_Animal.push_back(ptr1);
    
        Animal *ptr2 = new Animal();
        ptr2->id = 30;
        vec_Animal.push_back(ptr2);
    
    
        vector<Animal*>::iterator ite_Animal = vec_Animal.begin();
        for(ite_Animal; ite_Animal != vec_Animal.end(); ++ite_Animal)
              cout<<"\nVector contains:"<< (*ite_Animal)->id;
    
    
    
        vector<Animal*>::iterator ite_search;
        /*Find a value*/
        ite_search = std::find_if( vec_Animal.begin(), vec_Animal.end(), Ispresent(20));
    
        if(ite_search != vec_Animal.end())
            cout<<"\n\nElement Found:"<<(*ite_search)->id;
        else
            cout<<"\n\nElement Not Found";
    
        return 0;
    }
    

    Note that the sample is just an example of how to get find_if working, it does not follow the best practices.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.