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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:39:49+00:00 2026-05-22T02:39:49+00:00

I have a vector that I am trying to perform a contains function on.

  • 0

I have a vector that I am trying to perform a contains function on. I am receiving some sort of casting error and I can’t piece together a solution. I am also wanting to know whether or not what I am doing is the appropriate way to check if a vector contains a value.

Here is the code:

#include "stdafx.h"
#include <vector>

static void someFunc(double** Y, int length);
static bool contains(double value, std::vector<double> vec);

int main()
{
    double doubleArray[] = { 1, 2, 3, 4, 5 };
    double *pDoubleArray = doubleArray;
    int size = sizeof doubleArray / sizeof doubleArray[0];

    someFunc(&pDoubleArray, size);

    return 0;
}
static void someFunc(double** Y, int length)
{   
    std::vector<double> vec();

    for(int i = 0; i < 10; i++)
    {
        //error: 'contains' : cannot convert parameter 2 from 'std::vector<_Ty> (__cdecl *)(void)' to 'std::vector<_Ty>'
        if(contains(*(Y[i]), vec)) 
        {
            //do something
        }
    }

}
static bool contains(double value, std::vector<double> vec)
{
    for(int i = 0; i < vec.size(); i++)
    {
        if(vec[i] == value)
        {
            return true;
        }
    }

    return false;
}
  • 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-22T02:39:49+00:00Added an answer on May 22, 2026 at 2:39 am

    When you declare a variable with it’s default constructor, you don’t put () after it (although it’s optional when you use new to allocate space on the free store). So this line:

    std::vector<double> vec();
    

    should become

    std::vector<double> vec;
    

    If you leave it as you did, it thinks that line is a function prototype of a function called vec taking no parameters and returning a std::vector<double>, which is why you’re getting a compiler error.

    And yes, your code for finding an item will work (it’s called a linear search). Also if you want to, you can use std::find:

    if (std::find(vec.begin(), vec.end(), value) != vec.end())
        // found value in vec
    

    If your vector is in sorted order, you can also use binary_search which is much faster than find, and the usage is the same except binary_search returns a bool instead of an iterator (so you don’t need to test it against vec.end()). Make sure you include the algorithm header if you use either of these.

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

Sidebar

Related Questions

I have a vector-like class that contains an array of objects of type T
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
I have a vector that I want to insert into a set . This
I have created a BST full of WordInfo objects that have a vector to
I have a class that has a vector of another class objects as a
I have a function that I use to add vectors, like this: public static
I have a vector that stores pointers to many objects instantiated dynamically, and I'm
I have a vector class that has addition, multiplication, subtraction, division, etc operators. I'm
Here is what I'm trying to do. I have a std::vector with a certain
I have a class that creates a vector of objects. In the deconstructor for

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.