I had planned to implement a kind of INDEX function for all types similar as in FORTRAN. Would this be a correct solution? ** EDIT after comments **
template <typename T>
bool contains(std::vector<T>& vec, T value){
if (std::any_of(vec.begin(), vec.end(), [value](T j) { return value == j; }))return true;
return false;
}
NB There is a pitfall with float type comparison for generic implementation!
CodePudding user response:
If the objective of your program is to find the determined value that is the correct way:
template <typename T>
bool index(std::vector<T>& vec, T value){
return std::any_of(vec.begin(), vec.end(), [value](T j) { return value == j; });
}
My program doesn't differ much from @caleth's answer as in order to write a general-purpose function that can handle various sorts of ranges, such as an std::array
, std::vector
, a C-like array
, or others, you should write a template like this:
template <typename iter, typename T>
bool any_index(iter begin, iter end, T value) {
for (; begin != end; begin) {
if (static_cast<T>(*begin) == value) {
return true;
}
}
return false;
}
usage:
int main() {
vector<int> v1{ 4, 3, 2, 1 };
vector<string> v2{ "hey", "language", "compiler", "bug" };
vector<char> v3{ 'a', 'b','c', 'd' };
array<int, 6> c{ {1,2,3,4,5,6} };
unsigned char buf[5] = {0x11, 0x22, 0x33, 0x44, 0x55};
assert(any_index(begin(v1), end(v1), 4) == true);
assert(any_index(begin(v2), end(v2), string("compiler")) == true);
assert(any_index(begin(v3), end(v3), 'd') == true);
assert(any_index(begin(c), end(c), 5) == true);
assert(any_index(begin(buf), end(buf), 0x11) == true);
return 0;
}
Full example (godbolt)
CodePudding user response:
Yes, that is a valid implementation, however I'd write it differently
template <std::ranges::input_range R, typename T>
requires std::indirect_binary_predicate<ranges::equal_to, ranges::iterator_t<R>, const T*>
bool index(R&& range, const T & value){
return std::ranges::find(range, value) != std::ranges::end(range);
}
This will work for any sequence and any value that can be compared for equality with elements of that sequence.
In C 11 there aren't concepts to be explicit about the requirements, but it goes similarly
template <typename R, typename T>
bool index(R&& range, const T& value) {
using std::begin, std::end;
return std::find(begin(range), end(range), value) != end(range);
}