How can I use c++ assert to check if a value is found in an array? I know I could use a simple for loop but I am supposed to use assert for my project.
Function signature:
template <class K, class V>
K& Map<K, V>::operator[](const V &value) const //Assert the argument is found
//in the Values array and then return the key with the given value
{
}
Update:
Is this code correct? I am not sure since it does not use assert to check if the value is found in an array.
template <class K, class V>
K& Map<K, V>::operator [] (const V &value) const
{
for (int i = 0; i < size; i++)
{
if (A2[i] == value)
return A1[i];
}
assert(false);
}
Mapis.operator[]cannot beconstif theMapworks anything likestd::map.operator[]will assert the key already exists, it is a precondition that the argument will be found. So it will not insert new elements and is not intended to behave likestd::map.assertis not a replacement forforhere. You have to some how determine ifvalueis in your map, then you canassertthat condition.