How to find out if an item is present in an std vector

0 votes
All I want to do is see if an element in the vector exists or not, so I can deal with each instance individually.
May 26 in Others by Nicholas
• 2,360 points
10 views

1 answer to this question.

0 votes

The most straightforward solution is to count the total number of elements in the vector that have the specified value. 

If the count is greater than zero, we've found our element. 

This is simple to accomplish with the std::count function.

#include <iostream>

#include <vector>

#include <algorithm>


int main()

{

    std::vector<int> v = { 4, 7, 5, 2, 6, 9 };

    int key = 6;


    if (std::count(v.begin(), v.end(), key)) {

        std::cout << "Element found";

    }

    else {

        std::cout << "Element not found";

    }


    return 0;

}
answered May 27 by Damon
• 3,380 points

Related Questions In Others

0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,240 points
3,916 views
0 votes
1 answer

What is a name function in JavaScript & how to define it?

A named function declares a name as ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
771 views
0 votes
1 answer

How to store an array in localstorage?

Localstorage only supports Strings. So you can ...READ MORE

answered Jul 1, 2019 in Others by sunshine
• 1,280 points
18,456 views
+1 vote
1 answer
0 votes
0 answers

How do I reverse a C++ vector?

Is there a vector function in C++ ...READ MORE

50 minutes ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
1 answer

How to expose std::pair to python using boost::python?

The most simple example of exposing std::pair is: class_<std::pair<int, int> ...READ MORE

answered Jun 20, 2019 in Python by SDeb
• 13,300 points
573 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31 in C++ by Damon
• 3,380 points
18 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1 in C++ by Damon
• 3,380 points
34 views
0 votes
1 answer

What is this weird colon-member (" : ") syntax in the constructor?

Foo(int num): bar(num) In C++, this is known as a Member Initializer List. Simply put, it sets the value of your member bar to num. There is a significant difference between initializing a member with the Member initializer list and assigning a value to it within the function Object() { [native code] } body. When you use the Member initializer list to initialise fields, the constructors are only called once, and the object is constructed and initialised in a single operation. If you use assignment, the fields will be initialised with default constructors and then reassigned with actual values (via the assignment operator). As you can see, there is an extra overhead of creation and assignment in the latter, which may be significant for user defined classes. Cost of Member Initialization =Object ...READ MORE

answered May 27 in Others by Damon
• 3,380 points
24 views
0 votes
1 answer

Why do we need virtual functions in C++?

A virtual function is a base class member function that we expect to redefine in derived classes. In the base class, a virtual function is used to ensure that the function is overridden.  This is especially true when a pointer from a base class points to an object from a derived class. For example, consider the code below: class Base ...READ MORE

answered May 27 in Others by Damon
• 3,380 points
9 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP