You can use std::find
from <algorithm>
:
#include <algorithm> #include <vector> vector<int> vec; //can have other data types instead of int but must same datatype as item std::find(vec.begin(), vec.end(), item) != vec.end()
This returns a bool (true
if present, false
otherwise). With your example:
#include <algorithm> #include <vector> if ( std::find(vec.begin(), vec.end(), item) != vec.end() ) do_this(); else do_that();
Related Posts:
- How do I reverse a C++ vector?
- How to implement 2D vector array?
- Why is “using namespace std;” considered bad practice?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- Vector of Vectors to create matrix
- How do I print out the contents of a vector?
- check if a std::vector contains a certain object?
- How to navigate through a vector using iterators? (C++)
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- Replace part of a string with another string
- Debug assertion failed. C++ vector subscript out of range
- No matching member function for call to ‘push_back’ error
- Pass a vector by reference C++
- Why there is no pop_front method in C++ std::vector?
- Initializing a two dimensional std::vector
- C++ – No matching member function for call to ‘push_back’
- Debug assertion failed. C++ vector subscript out of range
- Vector is not a Template?
- Displaying a vector of strings in C++
- ‘vector’ in namespace ‘std’ does not have a template type
- Issue with std::stol – ‘std::invalid_argument’ what(): stol
- Is there a tab equivalent of std::endl within the standard library?
- Why am I getting string does not name a type Error?
- C++ error: Undefined symbols for architecture x86_64
- Vector of structs initialization
- Converting a vector
to string - vector
::size_type in C++ - Why use a new call with a C++ ‘vector’?
- Why doesn’t std::vector::push_front() exist?
- C++ delete vector, objects, free memory
- How to iterate over a vector?
- How to access the contents of a vector from a pointer to the vector in C++?
- printf with std::string?
- Insert object at index of vector c++
- How to convert vector to array
- No operator << matches these operands
- Initialize empty vector in structure – c++
- Append an int to a std::string
- splitting a string into an array in C++ without using vector
- What’s the most efficient way to erase duplicates and sort a vector?
- Best way to extract a subvector from a vector?
- Appending a vector to a vector
- cc1plus: error: unrecognized command line option “-std=c++11” with g++
- How to initialize a vector of pointers
- C++ Erase vector element by value rather than by position?
- Correct way to work with vector of arrays
- C++ error: double free or corruption (fasttop)
- error C2106: ‘=’ : left operand must be l-value
- I get this error: “glibc detected”
- How to create a vector of class objects in C++?
- “vector” was not declared in this scope
- Vector declaration “expected parameter declarator”
- Why is this vector iterator not incrementable?
- error C2065: ‘cout’ : undeclared identifier
- Why can’t I make a vector of references?
- Displaying contents of a vector container in C++
- What is the size of sizeof(vector)? C++
- c++ vector bubble sort
- clearing a vector of pointers [duplicate]
- I’m getting the error “stoi is not a member of std” in myprogramminglab [duplicate]
- “Cannot allocate an object of abstract type” error
- Initializing an array of objects
- Passing an array by reference
- What does int argc, char *argv[] mean?
- Meaning of = delete after function declaration
- Using NULL in C++?
- Error with multiple definitions of function
- What are C++ functors and their uses?
- invalid conversion from ‘const char*’ to ‘char*’
- Check if a string is palindrome
- What is an ‘undeclared identifier’ error and how do I fix it?
- Returning multiple values from a C++ function
- Eclipse C++ : “Program “g++” not found in PATH”
- no default constructor exists for class
- Press Enter to Continue
- How to trim a std::string?
- C++: std does not have member “string”
- Fatal error: ‘stdafx.h’ file not found
- How do I add the MinGW bin directory to my system path?
- “used without template parameters”
- Reading from .txt file into two dimensional array in c++
- C++ Error ‘nullptr was not declared in this scope’ in Eclipse IDE
- C++ Simple hangman game
- Error: macro names must be identifiers using #ifdef 0
- Error: free(): invalid next size (fast):
- Error: free(): invalid next size (fast):
- Fastest way to Convert String to Binary?
- Reading data from file into an array
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- casting int to char using C++ style casting
- How to print (using cout) a number in binary form?
- What is the job of autogen.sh when building a c++ package on Linux
- Using G++ to compile multiple .cpp and .h files
- LPCSTR, LPCTSTR and LPTSTR
- C++ Simple Dice roll – how to return multiple different random numbers [duplicate]
- How can I get a file’s size in C++?
- What does “-Wall” in “g++ -Wall test.cpp -o test” do?
- C++ equivalent of StringBuffer/StringBuilder?