C++ Unordered Set: Storing Vectors of Integers
The code above declares an unordered_set called hash_set, which stores vector<int> objects. An unordered_set is a container that stores unique elements in no particular order. In this case, the elements stored in the unordered_set are vectors of integers.\n\nThe code does not define any operations on the unordered_set, so it is not clear what the purpose of the code is. To use the unordered_set, you would need to perform operations such as inserting elements, removing elements, or checking if an element exists in the set.\n\nHere is an example of how you could use the unordered_set:\n\ncpp\n#include <iostream>\n#include <unordered_set>\n#include <vector>\n\nint main() {\n std::unordered_set<std::vector<int>> hash_set;\n\n std::vector<int> vec1 = {1, 2, 3};\n std::vector<int> vec2 = {4, 5, 6};\n std::vector<int> vec3 = {1, 2, 3}; // Duplicate of vec1\n\n hash_set.insert(vec1);\n hash_set.insert(vec2);\n hash_set.insert(vec3); // Duplicate element, will not be inserted\n\n std::cout << "Size of unordered_set: " << hash_set.size() << std::endl;\n\n std::vector<int> vec4 = {1, 2, 3}; // Another duplicate element\n\n if (hash_set.find(vec4) != hash_set.end()) {\n std::cout << "vec4 exists in hash_set" << std::endl;\n } else {\n std::cout << "vec4 does not exist in hash_set" << std::endl;\n }\n\n return 0;\n}\n\n\nIn this example, we create an unordered_set called hash_set and insert three vectors into it. The third vector, vec3, is a duplicate of vec1, so it will not be inserted into the set. We then check if another vector, vec4, exists in the set using the find() function. Since vec4 is the same as vec1 and vec3, it will be found in the set. The output of the example would be:\n\n\nSize of unordered_set: 2\nvec4 exists in hash_set\n
原文地址: https://www.cveoy.top/t/topic/pCCo 著作权归作者所有。请勿转载和采集!