Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / In C++ check if std::vector contains a certain value [duplicate]

In C++ check if std::vector contains a certain value [duplicate]

August 23, 2021 by James Palmer

You can use std::find as follows:
if (std::find(v.begin(), v.end(), “abc”) != v.end())
{
// Element in vector.
}

To be able to use std::find: include .

If your container only contains unique values, consider using std::set instead. It allows querying of set membership with logarithmic complexity.
std::set s;
s.insert(“abc”);
s.insert(“xyz”);
if (s.find(“abc”) != s.end()) { …

If your vector is kept sorted, use std::binary_search, it offers logarithmic complexity as well.

If all else fails, fall back to std::find, which is a simple linear search.

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in