Check for char array

Ok, this is what I was trying to do, please correct me if I am wrong. I am trying to check if myarray contains

char* myarray[] = { "hello", "wooorld", "hi"}; if(myarray->Contains(abcd)) { //do stuff } 

My question is: is there a better way to do this?

+4
source share
2 answers

One way is to use std::string and std::vector with the std::find algorithm:

  std::vector<std::string> strs{"hello","wooorld","hi"}; std::string toFind = "abcd"; if (std::find(strs.begin(), strs.end(), toFind) != strs.end()) { std::cout <<" abcd exist in vector of strings"; } 
+6
source

If you sort an array of C style strings , there are other β€œbetter” ways to search for text.

To name a few:
Binary search
Fibonacci Search

You need to clarify the term "better." For small containers, linear search works better than binary search.

0
source

Source: https://habr.com/ru/post/1488575/


All Articles