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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:11:46+00:00 2026-05-26T10:11:46+00:00

I’m trying to check whether a string exists in an array in as little

  • 0

I’m trying to check whether a string exists in an array in as little lines as possible example:

string foo[] = {blah,blahhh,blahhhh}
string bar = "blah";

if (bar in foo){
cout << "true";
}else {
cout << "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-26T10:11:47+00:00Added an answer on May 26, 2026 at 10:11 am

    There is not this keyword nor this construct in C++.
    You have to do a loop or use the find function.

    http://www.cplusplus.com/reference/algorithm/find/

    For other people that try to do the same thing (since is a question a lot of people asked to me in the past) : char arrays and string literals (that are const char arrays) cannot be compared with equality or with built-in operators : comparing two char literals is the same as comparing two pointers.
    You can however use the std::string type or use old C style functions like strcmp.

    If you really want to avoid writing too much lines of code just write another function with more lines and call it with one line. You then can put it in a nice header file and include it when you need 🙂

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    template<typename T, size_t N>
    inline bool arraycontains(const T (&array)[N], const T& value)
    {
        for (size_t i = 0; i < N; ++i)
            if (array[i] == value)
                return true;
        return false;
    }
    
    int main()
    {
        string foo[] = { "blah", "blahhh", "blahhhh" };
        string bar = "blah";
    
        cout << (arraycontains(foo, bar) ? "true" : "false");
        return 0;
    }
    

    If you instead of an array have a pointer you should use a function where u can pass size as a parameter.

    Now, if you array is really big, i don’t think you would like to use an O(n) linear search into an array, in that case I know you would prefer to use std::map or std::hash_map (note that hash_map is not available in all versions of the STL).

    std::map uses internally a Red Black Tree (or an AVL tree, depending on the implementation), so lookup operation is O(log n) worst case, faster than O(n).

    std::hash_map instead uses internally an hashtable, giving a worst case complexity O(n) but an average complexity of O(1), very fast in real world applications.

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

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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

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.