简体   繁体   中英

What is the time complexity of judging if two vectors are equal in C++?

Does it have to compare every element one by one?
In such case it must be O(n) right?

It doesn't necessarily compare all the elements.

If the two vectors are of different lengths, it can return false based only on the difference in length, in constant time.

Otherwise, yes, it has to compare elements until it encounters a difference (all the elements if the two are equal).

when you want to compare two vectors.

  • They must be the same type, such as both of them are vector, it is not acceptable to compare two different type vectors, such as the comparison between vector and vector
  • The rules to compare two vectors follow the comparison between two strings, not necessarily to compare all elements.

Generally, in our program, the size of two vectors should already be given (not a variable as the input size) , in that situation, the comparison between two vectors is O(1) since it must finish the comparison in a fixed time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM