Java HashSet equals C
•
Java
If there is something similar to Java hash set in C, I'm curious I am a quick browsing data structure because I can only run it (E) Just do it Similarly, if you can enlighten me on how to make an example of any data structure you suggest I would appreciate it very much Oh, please don't publish, just look at C docs, because I've done so and find them very heavy
Solution
You can use STD:: unordered_ Set < > (standard § 23.5.6), and its find method (making a search) is taken as the average complexity of O (1):
#include <iostream>
#include <unordered_set>
int main()
{
std::unordered_set<int> example = {1,2,3,4};
auto search = example.find(2);
if(search != example.end()) {
std::cout << "Found " << (*search) << '\n';
}
else {
std::cout << "Not found\n";
}
}
Edit:
According to @ drew dormann's suggestion, you can choose to use count, with an average complexity of O (1):
#include <iostream>
#include <unordered_set>
int main()
{
std::unordered_set<int> example = {1,4};
if(example.count(2)) {
std::cout << "Found\n";
}
else {
std::cout << "Not found\n";
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
